Prev: How to Force IE to download text file?
Next: SharePoint
From: Nick Balestra on 30 Apr 2010 18:57 hello everybody here is my array(s) $us_census = array('NY' => array('New York' => 8008278), 'CA' => array('Los Angeles' => 3694820, 'San Diego' => 1223400), 'IL' => array('Chicago' => 2896016), 'TX' => array('Houston' => 1953631, 'Dallas' => 1188580, 'San Antonio' => 1144646), 'PA' => array('Philadelphia' => 1517550), 'AZ' => array('Phoenix' => 1321045), 'MI' => array('Detroit' => 951270)); print "<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>"; // $state is the key and $states is the value foreach ($us_census as $state => $cities) { // $state is the key and $habitant is the value foreach ($cities as $city => $habitants){ print "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>"; } } Now i also want to be able to count the total population per state, i am stucked...
From: Piero Steinger on 30 Apr 2010 19:23 Am 01.05.2010 00:57, schrieb Nick Balestra: > hello everybody here is my array(s) > > > $us_census = array('NY' => array('New York' => 8008278), > 'CA' => array('Los Angeles' => 3694820, > 'San Diego' => 1223400), > 'IL' => array('Chicago' => 2896016), > 'TX' => array('Houston' => 1953631, > 'Dallas' => 1188580, > 'San Antonio' => 1144646), > 'PA' => array('Philadelphia' => 1517550), > 'AZ' => array('Phoenix' => 1321045), > 'MI' => array('Detroit' => 951270)); > > > > print "<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>"; > > > // $state is the key and $states is the value > foreach ($us_census as $state => $cities) { > > // $state is the key and $habitant is the value > foreach ($cities as $city => $habitants){ > > > > print "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>"; > > > } > } > > > Now i also want to be able to count the total population per state, i am stucked... > array_sum() should do it :) foreach ($us_census as $state => $cities) { $population_per_state = array_sum($cities); }
From: Nick Balestra on 30 Apr 2010 20:33 thanks Piero! i was trying to solve an excercise on "learning php5" (O'reilyl) book. I am happy abotut his solution with the array_sum funtion you suggested, and my multidimensional array make much more sense to mee then they suggested solution that also much more line of code comapred... look: my solution (with Piero suggeston): and ont he bottom the book solution. what do u say is the best one? why? i am learning so i am interested in understanding why a solution can be better then an other... $us_census = array('NY' => array('New York' => 8008278), 'CA' => array('Los Angeles' => 3694820, 'San Diego' => 1223400), 'IL' => array('Chicago' => 2896016), 'TX' => array('Houston' => 1953631, 'Dallas' => 1188580, 'San Antonio' => 1144646), 'PA' => array('Philadelphia' => 1517550), 'AZ' => array('Phoenix' => 1321045), 'MI' => array('Detroit' => 951270)); print "<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>"; foreach ($us_census as $state => $cities) { foreach ($cities as $city => $habitants){ $tothabitants += $habitants; print "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>"; } } print "<tr><td></td><td></td><td></td><td>$tothabitants</td></tr></table>"; foreach ($us_census as $state => $cities) { $population_per_state = array_sum($cities); print "$state $population_per_state<br>"; } -------------------------- the book solution: $population = array('New York' => array('state' => 'NY', 'pop' => 8008278), 'Los Angeles' => array('state' => 'CA', 'pop' => 3694820), 'Chicago' => array('state' => 'IL', 'pop' => 2896016), 'Houston' => array('state' => 'TX', 'pop' => 1953631), 'Philadelphia' => array('state' => 'PA', 'pop' => 1517550), 'Phoenix' => array('state' => 'AZ', 'pop' => 1321045), 'San Diego' => array('state' => 'CA', 'pop' => 1223400), 'Dallas' => array('state' => 'TX', 'pop' => 1188580), 'San Antonio' => array('state' => 'TX', 'pop' => 1144646), 'Detroit' => array('state' => 'MI', 'pop' => 951270)); $state_totals = array( ); $total_population = 0; print "<table><tr><th>City</th><th>Population</th></tr>\n"; foreach ($population as $city => $info) { $total_population += $info['pop']; $state_totals[$info['state']] += $info['pop']; print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n"; } foreach ($state_totals as $state => $pop) { print "<tr><td>$state</td><td>$pop</td>\n"; } print "<tr><td>Total</td><td>$total_population</td></tr>\n"; print "</table>\n";
From: Programming Guides on 1 May 2010 12:11 On Fri, Apr 30, 2010 at 7:33 PM, Nick Balestra <nick(a)beyounic.com> wrote: > thanks Piero! > > i was trying to solve an excercise on "learning php5" (O'reilyl) book. > > I am happy abotut his solution with the array_sum funtion you suggested, > and my multidimensional array make much more sense to mee then they > suggested solution that also much more line of code comapred... > > look: my solution (with Piero suggeston): and ont he bottom the book > solution. what do u say is the best one? why? i am learning so i am > interested in understanding why a solution can be better then an other... > > $us_census = array('NY' => array('New York' => 8008278), > 'CA' => array('Los Angeles' => 3694820, > 'San Diego' > => 1223400), > 'IL' => array('Chicago' => 2896016), > 'TX' => array('Houston' => 1953631, > 'Dallas' => > 1188580, > 'San > Antonio' => 1144646), > 'PA' => array('Philadelphia' => 1517550), > 'AZ' => array('Phoenix' => 1321045), > 'MI' => array('Detroit' => 951270)); > > > > "<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>"; > > > foreach ($us_census as $state => $cities) { > > foreach ($cities as $city => $habitants){ > > $tothabitants += $habitants; > > "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>"; > } > } > > print "<tr><td></td><td></td><td></td><td>$tothabitants</td></tr></table>"; > > > foreach ($us_census as $state => $cities) { > $population_per_state = array_sum($cities); > print "$state $population_per_state<br>"; > } > > -------------------------- > the book solution: > > > $population = array('New York' => array('state' => 'NY', 'pop' => 8008278), > 'Los Angeles' => array('state' => 'CA', 'pop' => 3694820), > 'Chicago' => array('state' => 'IL', 'pop' => 2896016), > 'Houston' => array('state' => 'TX', 'pop' => 1953631), > 'Philadelphia' => array('state' => 'PA', 'pop' => 1517550), > 'Phoenix' => array('state' => 'AZ', 'pop' => 1321045), > 'San Diego' => array('state' => 'CA', 'pop' => 1223400), > 'Dallas' => array('state' => 'TX', 'pop' => 1188580), > 'San Antonio' => array('state' => 'TX', 'pop' => 1144646), > 'Detroit' => array('state' => 'MI', 'pop' => 951270)); > > $state_totals = array( ); > $total_population = 0; > print "<table><tr><th>City</th><th>Population</th></tr>\n"; > foreach ($population as $city => $info) { > > > $total_population += $info['pop']; > > $state_totals[$info['state']] += $info['pop']; > print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n"; > } > > foreach ($state_totals as $state => $pop) { > print "<tr><td>$state</td><td>$pop</td>\n"; > } > print "<tr><td>Total</td><td>$total_population</td></tr>\n"; > print "</table>\n"; > > > > > I actually prefer your solution - it's easier to read and understand. On the other hand the solution the book offers has the advantage of being more extensible in that more pieces of information can be added per city. One thing I dont like about both solutions is that they both intertwine computation logic with presentation. A *much* better approach in this case is to first calculate all population data you need and put together one data structure that has all of that. Only after you have that ready do you begin to output HTML. And while outputting HTML the only PHP you should need is to iterate over your data structure and output. -- http://programming-guides.com
From: Nick Balestra on 1 May 2010 14:54
Thanks! I'll agree with you abotu ur points, i just started php few days ago..so i am in the first phase of learnign it...and this list is so gr8! thanks evrybody cheers, Nick On May 1, 2010, at 6:11 PM, Programming Guides wrote: > On Fri, Apr 30, 2010 at 7:33 PM, Nick Balestra <nick(a)beyounic.com> wrote: > thanks Piero! > > i was trying to solve an excercise on "learning php5" (O'reilyl) book. > > I am happy abotut his solution with the array_sum funtion you suggested, and my multidimensional array make much more sense to mee then they suggested solution that also much more line of code comapred... > > look: my solution (with Piero suggeston): and ont he bottom the book solution. what do u say is the best one? why? i am learning so i am interested in understanding why a solution can be better then an other... > > $us_census = array('NY' => array('New York' => 8008278), > 'CA' => array('Los Angeles' => 3694820, > 'San Diego' => 1223400), > 'IL' => array('Chicago' => 2896016), > 'TX' => array('Houston' => 1953631, > 'Dallas' => 1188580, > 'San Antonio' => 1144646), > 'PA' => array('Philadelphia' => 1517550), > 'AZ' => array('Phoenix' => 1321045), > 'MI' => array('Detroit' => 951270)); > > > > print "<table><tr><th>State</th><th>City</th><th>Population</th><th>Total</th></tr>"; > > > foreach ($us_census as $state => $cities) { > > foreach ($cities as $city => $habitants){ > > $tothabitants += $habitants; > > print "<tr><td>$state</td><td>$city</td><td>$habitants</td><td></td></tr>"; > } > } > > print "<tr><td></td><td></td><td></td><td>$tothabitants</td></tr></table>"; > > > foreach ($us_census as $state => $cities) { > $population_per_state = array_sum($cities); > print "$state $population_per_state<br>"; > } > > -------------------------- > the book solution: > > > $population = array('New York' => array('state' => 'NY', 'pop' => 8008278), > 'Los Angeles' => array('state' => 'CA', 'pop' => 3694820), > 'Chicago' => array('state' => 'IL', 'pop' => 2896016), > 'Houston' => array('state' => 'TX', 'pop' => 1953631), > 'Philadelphia' => array('state' => 'PA', 'pop' => 1517550), > 'Phoenix' => array('state' => 'AZ', 'pop' => 1321045), > 'San Diego' => array('state' => 'CA', 'pop' => 1223400), > 'Dallas' => array('state' => 'TX', 'pop' => 1188580), > 'San Antonio' => array('state' => 'TX', 'pop' => 1144646), > 'Detroit' => array('state' => 'MI', 'pop' => 951270)); > > $state_totals = array( ); > $total_population = 0; > print "<table><tr><th>City</th><th>Population</th></tr>\n"; > foreach ($population as $city => $info) { > > > $total_population += $info['pop']; > > $state_totals[$info['state']] += $info['pop']; > print "<tr><td>$city, {$info['state']}</td><td>{$info['pop']}</td></tr>\n"; > } > > foreach ($state_totals as $state => $pop) { > print "<tr><td>$state</td><td>$pop</td>\n"; > } > print "<tr><td>Total</td><td>$total_population</td></tr>\n"; > print "</table>\n"; > > > > > > I actually prefer your solution - it's easier to read and understand. On the other hand the solution the book offers has the advantage of being more extensible in that more pieces of information can be added per city. > > One thing I dont like about both solutions is that they both intertwine computation logic with presentation. A *much* better approach in this case is to first calculate all population data you need and put together one data structure that has all of that. Only after you have that ready do you begin to output HTML. And while outputting HTML the only PHP you should need is to iterate over your data structure and output. > > -- > http://programming-guides.com |