3

Here is my associative array:

$req_data1[]=array(
    'depart1'=>$_REQUEST['to'],
    'd_time1'=>$d_time5,
    'stop'=>"",
    'leave_stop'=>"",
    'arrival1'=>$_REQUEST['from'],
    'a_time1'=>$end_time5,
    'price1'=>intval($final_price),
    'air_line'=>"xxxxx");

Here is my sorting algorithm:

foreach ($req_data as $key => $row) {
    $depart[$key]  = $row['depart'];
    $d_time[$key] = $row['d_time'];
    $stop[$key]  = $row['stop'];
    $leave_stop[$key] = $row['leave_stop'];
    $arrival[$key]  = $row['arrival'];
    $a_time[$key] = $row['a_time'];
    $price[$key] = $row['price'];
}

array_multisort($price,SORT_ASC, $req_data);

I am displaying the data after sorting:

foreach($req_data as $key=>$row) {
    echo "</br>";

    foreach($row as $key2=>$row2){
        echo $row2;
    }
}

My problem now is that I want to put that array into an HTML table but dont know how. This is my code which I tried so far but its not working:

$cols = 5; 

echo "<table border=\"5\" cellpadding=\"10\">"; 

for ($r=0; $r < count($row2); $r++) { 
    echo "<tr>"; 

    for ($c=0; $c<$cols; $c++) { 

        ?> <td> <?php $input[$r+$c] ?> </td> <?php 
        }

    echo "</tr>"; 
    $r += $c; 
}

echo "</table>";
?>

Could any one tell me what is wrong with my code, or how I can display this sorted data into a table? Thanks.

1
  • 1
    Can you please format your code so it's more readable? That will make it much easier for people to help you. Commented Jul 27, 2011 at 4:52

5 Answers 5

5

Why not just modify the loop you already use to display the data?

echo "<table>";
foreach($req_data as $key=>$row) {
    echo "<tr>";
    foreach($row as $key2=>$row2){
        echo "<td>" . $row2 . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
Sign up to request clarification or add additional context in comments.

Comments

1
$toOutput = '<table>';
$showHeader = true;
$memberData = $reportObj->getMemberData();
while($row = mysql_fetch_assoc($memberData))
{
    $toOutput .= '<tr>';

    //Outputs a header if nessicary
    if($showHeader)
    {
        $keys = array_keys($row);
        for($i=0;$i<count($keys);$i++)
        {
            $toOutput .= '<td>' . $keys[$i] . '</td>';
        }
        $toOutput .= '</tr><tr>';
        $showHeader = false;
    }

    //Outputs the row
    $values = array_values($row);
    for($i=0;$i<count($values);$i++)
    {
        $toOutput .= '<td>' . $values[$i] . '</td>';
    }

    $toOutput .= '</tr>';
}
$toOutput .= '</table>';

echo 'Test page';
echo $toOutput;

Sorry for the necro, but I was actually looking to see if there was a build-in function for this as I was writing this.

3 Comments

Are you actually asking a question in your answer field?
Did you find a builtin function?
No, I never found a nice built-in function. I imagine this is handled nicely in modern frameworks like Laravel. However, I have since moved on from PHP development.
1
     function DumpTable($array_assoc) {
        if (is_array($array_assoc)) {
            echo '<table class="table">';
            echo '<thead>';
            echo '<tr>';
            list($table_title) = $array_assoc;
            foreach ($table_title as $key => &$value):
                echo '<th>' . $key . '</th>';
            endforeach;
            echo '</tr>';
            echo '</thead>';
            foreach ($array_assoc as &$master):
                echo '<tr>';
                foreach ($master as &$slave):
                    echo '<td>' . $slave . '</td>';
                endforeach;
                echo '</tr>';
            endforeach;
            echo '</table>';
            return;
        }
    }

1 Comment

Hey, welcome to StackOverflow! Could you please explain your code and why it works?
0
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
    echo "<tr>";
    for ($c=0; $c<$cols; $c++) { ?>
        <td> <?php $input[$r+$c] ?> </td>
    <?php }

    echo "</tr>";
    $r += $c;
}
echo "</table>";?>

Try something like this

echo "<table>";
for($r=0;$r<count($row2);$r++){
echo "<tr>";
for($c=0;$c<$cols;$c++){
echo "<td>".[VARIABLE YOU WANT TO PRINT]."</td>";
}
echo "</tr>";
}
echo "</table>";

Comments

0
you can try the following:

echo "The associative array<br>";
$computer=array("brand"=>"hp", "price"=>"800", "cpu"=>"core i7");
$keys=array_keys($computer);
echo "<table><tr>";
foreach($keys as $row){
    echo "<th style=\"border: solid 2px green\">".$row."</th>";
}echo "</tr><tr>";
foreach($computer as $col){
    echo "<td style=\"border: solid 2px blue\">".$col."</td>";
}echo "</tr></table>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.