views:

72

answers:

3

Hi all I would like to display my Query from Mysql on the table. However, my data is like this:

Array([0]=>data1, [1]=>data2, [2]=>data3,[3]=>data4,[4]=>pic1,[5]=>pic2,[6]=>pic3,[7]=>pic4);

I want to display my table as

|data1 | data2 |data3 |data4
|pic1  | pic2  |pic3  |pic4

I know how to display the data in the single ling like td or tr but not sure how to do tr and td on the same loop. Any helps? Thanks in advance!

while($query=mysql_fetch_array($dataQuery)){
//not sure what to do here.
}
+2  A: 
<?php
$rows = mysql_fetch_array($dataQuery);
echo "<table><tr>";
foreach($rows as $num => $row){
    echo "<td>".$row[$num]."</td>"; // here we echo every object in array
    if($num%2 == 0){
        echo "</tr><tr>"; // if we get to a number witch can be divided by 2 we add a new table row
    }
}
echo "</tr></table>";
?>

i think this should work for you.

Mihai Iorga
Won't this produce `|data1 | data2` as the first row and `|data3 | data4` as the second, `|pic1 | pic2` as the third and `|pic3 | pic4` as the fourth row?
Josh
Josh. I updated my question to show 4 columns. The original question was 2 columns only. :D I am working on his solution now.
Jerry
it will produce EXACTLY what he asked the first time, now he remade his question
Mihai Iorga
Mihai. The $num variable is string (query from mysql..ex:name, age) and cannot be divided by numbers(all comes out zero). Not sure if I am missing something....
Jerry
and about that you should change "$num%2" to "$num%4"
Mihai Iorga
@Jerry: That explains it :-) I would take the `$columns` from @PerroVerd's answer and replace the `$num%2` in @Mihai's answer with: `$num%$columns`
Josh
@Mihai: Now that I realize what's going on, I've upvoted your answer.
Josh
that num is autogenerated by the while statement.the variable that extracts from database is "$row"http://www.php.net/manual/en/control-structures.foreach.php
Mihai Iorga
Mihai. I have voted +1 for u. but I eventually use Tom's solution. It suits my case. (My real case is more complicated than my question). Thanks your help though.
Jerry
+2  A: 

If you have lineal data but you know the number of columns you should try with a counter.

$columns = 2;
$ct = 0;
while($query=mysql_fetch_array($dataQuery)) {

    if ($ct % $columns == 0) echo "<tr>";

    echo '<td>' . query_data_to_extract . '</td>';
    $ct++;

    if ($ct % $columns == 0) echo "</tr>";

}
PerroVerd
PerroVerd. I don't know how many columns I will have. Its based on my Mysql data. Your solution works on known columns. Thanks though.
Jerry
@PerroVerd: What is `query_data_to_extract`?
Josh
+1 to you PerroVerd. Thanks.
Jerry
@Josh with mysql_fetch_array you could get an associative array or an integer indexed array. I tried a generic solution, you should replace query_data_to_extract with $query[0] or $query['foobar'] depending on your data
PerroVerd
@PerroVerd: That explanation helps. Without it your code looks invalid :-)
Josh
+2  A: 

It's probably easier to assemble the two rows as strings first and then output them betwen table rows. You know how many items you have (number of rows), so divide that by two. Then subtract one from that number (you might want to check that it's even so you know you haven't made a mistake) to account for the fact arrays are zero-based. Loop over the whole array. Until you hit your halfway point, you're assembling your title row. After that you're assembling your picture row. Then echo the results between table row tags. Here's a rough (untested) shot at the code:

$rows = mysql_fetch_array($dataQuery);
$total_rows = mysql_num_rows($rows);
// check total_rows is even here?
$midpoint = $total_rows / 2 - 1;
$title_row = '';
$photo_row = '';
foreach($rows as $num => $row)
{
    $new_row = sprintf("\n\t<td>%s</td>", $row[0]);
    if ($num < $midpoint)
    {
        $title_row .= $new_row;
    }
    else
    {
        $photo_row .= $new_row;
    }
}

echo sprintf("\n<tr>%s</tr>", $title_row);
echo sprintf("\n<tr>%s</tr>", $photo_row);
Tom
+1: This is how I would solve it.
Josh
Thanks for the help Tom.
Jerry
No problem. I feel like I have a bug in there somewhere in the code I stole from other examples), but glad I could help.
Tom