1

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.
}
3
  • Can you explain more about how you know to break that specific example into two lines? i.e. Why is pic1 and pic2 on separate lines? Is is just that you only want two elements per row or is it because some the elements are "data" and the other elements are "pic"? Commented May 20, 2010 at 14:11
  • The elements are all string. Pic1 is just an example. I want to display at least 3 data per row. 2 rows total. The table will be like the updated version above...Thanks Commented May 20, 2010 at 14:17
  • PerroVerd's answer seems the best in that case. You could simply get the total number of elements, divide by two, then set $columns to that number, thus producing two rows no matter the number of results. Commented May 20, 2010 at 14:32

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

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.
2
<?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.

9 Comments

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. I updated my question to show 4 columns. The original question was 2 columns only. :D I am working on his solution now.
it will produce EXACTLY what he asked the first time, now he remade his question
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....
and about that you should change "$num%2" to "$num%4"
|
2

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>";

}

4 Comments

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.
What is query_data_to_extract?
@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
That explanation helps. Without it your code looks invalid :-)

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.