php array to html table sort by columns -


i have php array output table. need sort table first column, second, third , fourth. im not sure if should use function or ksort. have below. seem have notice: use of undefined constants when run it.

<!doctype html> <html> <body> <?php $state=array ( array('alabama', 'montgomery', 4779736, 23), array('alaska', 'juneau', 710231, 47), array('arizona', 'phoenix', 6329017, 18), array('arkansas', 'littlerock', 2915918, 32), array('california', 'sacramento', 37253956, 1), array('colorado', 'denver', 5029196, 22), array('connecticut', 'hartford', 3518288, 29), array('delaware', 'dover', 897934, 45), array('florida', 'tallahassee', 18801310, 4), array('georgia', 'atlanta', 9687653, 9), array('hawaii', 'boise', 1360301, 42) );  echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th>' . state. '</th>');    echo('<th>' . capital. '</th>');    echo('<th>' . population. '</th>');    echo('<th>' . rank. '</th>');  echo'</tr>';  ($i=0; $i<11; $i++) {echo('<tr>');   ($j=0; $j<4; $j++)   {echo ('<td>' . $state[$i][$j] . '</td>');   }   echo('</tr>'); } echo "</table>"  ?> </body> </html> 

this other try @ sort:

<html>     <head>         <meta charset="utf-8">         <title></title>     </head>     <body>         <?php $states[] = array('state' => 'georgia', 'capital' => 'atlanta', 'rank' => 9); $states[] = array('state' => 'alaska', 'capital' => 'juneau', 'rank' => 47); $states[] = array('state' => 'alabama', 'capital' => 'montgomery', 'rank' => 23); $states[] = array('state' => 'hawaii', 'capital' => 'boise', 'rank' => 42);  $capital = array(); foreach ($states $key => $row) {     $capital[$key] = $row['capital']; } echo \array_multisort($capital, \sort_desc, $states);  ?>     </body> </html> 

this have far, reason, numbered columns (population , rank) arent sorting correctly in tables:

<?php  $states=array ( array('state' =>  'alabama', 'capital' => 'montgomery', 'population' => 4779736, 'rank' =>  23), array('state' => 'alaska', 'capital' =>  'juneau', 'population' => 710231, 'rank' =>  47), array('state' => 'arizona',  'capital' => 'phoenix',  'population' =>6329017, 'rank' =>  18), array('state' => 'arkansas',  'capital' => 'littlerock',  'population' =>2915918, 'rank' =>  32), array('state' => 'california', 'capital' =>  'sacramento', 'population' => 37253956, 'rank' =>  1), array('state' => 'colorado',  'capital' => 'denver', 'population' => 5029196,  'rank' => 22), array('state' => 'connecticut', 'capital' =>  'hartford', 'population' => 3518288, 'rank' =>  29), array('state' => 'delaware',  'capital' => 'dover',  'population' =>897934, 'rank' =>  45), array('state' => 'florida',  'capital' => 'tallahassee', 'population' => 18801310, 'rank' =>  4), array('state' => 'georgia', 'capital' =>  'atlanta', 'population' => 9687653,  'rank' => 9), array('state' => 'hawaii',  'capital' => 'boise', 'population' => 1360301, 'rank' =>  42) );   //create index rows foreach ($states $row) {   foreach ($row $key => $value){     ${$key}[]  = $value; //creates $volume, $edition, $name , $type arrays.   }   } echo '<h2>below initial table</h2>'; echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';   foreach($states $k => $val){      echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";   } echo "</table>";   echo '<h2>below state (first column) sort table - ascending</h2>'; echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';  \array_multisort($state, \sort_asc, $states);  foreach($states $k => $val){      echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";   } echo "</table>";   echo '<h2>below capital (second column) sort table - descending</h2>'; echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';  \array_multisort($capital, \sort_desc, $states);  foreach($states $k => $val){      echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";   } echo "</table>";   echo '<h2>below population (third column) sort table - ascending</h2>'; echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';  \array_multisort($population, \sort_asc, $states);  foreach($states $k => $val){      echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";   } echo "</table>";   echo '<h2>below rank (fourth column) sort table - descending</h2>'; echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';  \array_multisort($rank, \sort_desc, $states);  foreach($states $k => $val){      echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";   } echo "</table>"; ?> </body> </html> 

use code , think want ..

<?php  $states=array ( array('state' =>  'alabama', 'capital' => 'montgomery', 'population' => 4779736, 'rank' =>  23), array('state' => 'alaska', 'capital' =>  'juneau', 'population' => 710231, 'rank' =>  47), array('state' => 'arizona',  'capital' => 'phoenix',  'population' =>6329017, 'rank' =>  18), array('state' => 'arkansas',  'capital' => 'littlerock',  'population' =>2915918, 'rank' =>  32), array('state' => 'california', 'capital' =>  'sacramento', 'population' => 37253956, 'rank' =>  1), array('state' => 'colorado',  'capital' => 'denver', 'population' => 5029196,  'rank' => 22), array('state' => 'connecticut', 'capital' =>  'hartford', 'population' => 3518288, 'rank' =>  29), array('state' => 'delaware',  'capital' => 'dover',  'population' =>897934, 'rank' =>  45), array('state' => 'florida',  'capital' => 'tallahassee', 'population' => 18801310, 'rank' =>  4), array('state' => 'georgia', 'capital' =>  'atlanta', 'population' => 9687653,  'rank' => 9), array('state' => 'hawaii',  'capital' => 'boise', 'population' => 1360301, 'rank' =>  42) );   //create index rows foreach ($states $row) {   foreach ($row $key => $value){     ${$key}[]  = $value; //creates $volume, $edition, $name , $type arrays.   }   } array_multisort($state, sort_asc, $capital, sort_asc, $population, sort_asc, $rank , sort_asc,  $states);  echo "<table border=\"5\" cellpadding=\"10\">"; echo'<tr>';    echo('<th> state </th>');    echo('<th> capital </th>');    echo('<th> population </th>');    echo('<th> rank </th>');  echo'</tr>';   foreach($states $k => $val){       echo "<tr> <td>".$val['state']."</td>     <td>".$val['capital']."</td>     <td>".$val['population']."</td>     <td>".$val['rank']."</td></tr>  ";    }  echo "</table>"  ?> </body> </html> 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -