PHP/HTML/HTML Table

Материал из Web эксперт
Перейти к: навигация, поиск

Creating an HTML table from array elements

   <source lang="html4strict">

<?

   $languages = array ("a" => "A",
                       "b" => "B",
                       "q" => "Q");
   
print ""; reset ($languages); $hd1 = key ($languages); $hd2 = $languages[$hd1]; print "";
   next($languages);
   
   while ( list ($ctry,$lang) = each ($languages)) :
print "";
   endwhile;
   
print "
$hd1$hd2
$ctry$lang
";

?>

 </source>
   
  


Output a HTML table

   <source lang="html4strict">

<html> <head> <title>Table Example</title> </head> <body>

<?php $Cars = array ("Ford","Dodge","Chevy"); $CarId = array ("K","J","F"); for($x=0; $x < count($Cars); $x++){ print ""; print "\n";
   } 

?>

$CarId[$x]$Cars[$x]

</body> </html>

 </source>
   
  


Separate the city and state name in the array so we can total by state

   <source lang="html4strict">

<? $population = array("New York" => array("state" => "NY", "pop" => 8008278),

                   "San Antonio"  => array("state" => "TX", "pop" => 1144646),
                   "Detroit"      => array("state" => "MI", "pop" => 951270));

$state_totals = array(); $total_population = 0;

print "\n";

foreach ($population as $city => $info) {

   $total_population += $info["pop"];
   $state_totals[$info["state"]] += $info["pop"];
print "\n";

} foreach ($state_totals as $state => $pop) {

print "\n";

}

print "\n"; print "
CityPopulation
$city, {$info["state"]}{$info["pop"]}
$state$pop
Total$total_population
\n";

?>

 </source>
   
  


Use for each to loop through an array and create a table

   <source lang="html4strict">

$population = array("New York, NY" => 8008278,

                   "Los Angeles, CA" => 3694820,
                   "Chicago, IL" => 2896016);

$total_population = 0;

print "\n";

foreach ($population as $city => $people) {

   $total_population += $people;
print "\n";

}

print "\n"; print "
CityPopulation
$city$people
Total$total_population
\n";
 </source>