-1

I need to display pictures from MySQL like this:

-------|-------|-------|-------|
 Pic 1 | Pic 2 | Pic 3 | Pic 4 |
-------|-------|-------|-------| ----> there can be more then 4 this way>
 Pic 5 | Pic 7 | Pic 8 | Pic 9 |
-------|-------|-------|-------|

There can not be more then two rows but there can be unlimited amount of columns to the right.

I think i need to use a foreach loop. Does some one have a code that will do this?

My code so far:

 if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT title FROM donuts");
while ($row = mysqli_fetch_assoc($result)) { 

for ($x=0; $x<=2; $x++)
{
    if($x==0) 
    {
    echo "<tr>";  
    }
    else
    {
    echo "<td>".$row['title']."</td>";
    $x++;
    if($x==1) {
    echo "</tr>";
    }

        } 
    }
}

What i get

-------|
pic 1  |
-------|
-------|
pic 2  |
-------|
-------|
pic 3  |
-------|
-------|
pic 4  |
-------|
-------|
pic 5  |
-------|
-------|
pic 6  |
-------|
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kelly
  • 93
  • 9
  • @user3236300, I've formatted the code for you since you're new to SO. So the next step would be to run the code your code, post the results (in your question) if you think it's relevant, and tell us how it's different from what you're trying to get at, (and perhaps throw out a few guessses of what you think the problem might be) P.S. Welcome to SO :) – asifrc Jan 25 '14 at 22:44
  • 1
    What you'll need to do is figure out how many items are in your result set, and divide that by 2. Then print out the opening part of your table. Loop through the result set, counting how many you've printed; when you've gotten to the value in step one, print out a close table row and open a new one. Loop through the rest of the pictures, and then print the closing part of the table. – andrewsi Jan 26 '14 at 00:10

1 Answers1

2

This code will calculate number of the column automatically from the number of the result set. And output them in two rows.

$result = array ("Pic 1","Pic 2","Pic 3","Pic 4","Pic 5","Pic 6","Pic 7","Pic 8" );

$rows = 2; // define number of rows
$cols = ceil(count($result)/$rows);// define number of columns

echo "<table border='1'>";
$i=0; 
for($tr=1;$tr<=$rows;$tr++){

    echo "<tr>";
        for($td=1;$td<=$cols;$td++){
                            if (isset($result[$i])) {   
                                 echo "<td>".$result[$i]."</td>";
                                 $i++; 
                            }
        }
    echo "</tr>";

}

echo "</table>"; 
/*
OUTPUT:

-------|-------|-------|-------|
 Pic 1 | Pic 2 | Pic 3 | Pic 4 |
-------|-------|-------|-------| 
 Pic 5 | Pic 6 | Pic 7 | Pic 8 |
-------|-------|-------|-------|

*/
optional
  • 92
  • 6
  • here is the problem i have now Fatal error: Cannot use object of type mysqli_result as array in C:\xampp\htdocs\lakeside\index.php on line 39 $result = mysqli_query($con,"SELECT title FROM donuts"); while($data = mysqli_fetch_assoc($result)){ $result[] = $data; } $result=$result; $rows = 2; $cols = ceil(count($result)/$rows); echo ""; $i=0; for($tr=1;$tr<=$rows;$tr++){ echo ""; for($td=1;$td<=$cols;$td++){ if (isset($result[$i])) { echo ""; $i++; } } echo ""; } echo "
    ".$result[$i]."
    ";
    – Kelly Jan 27 '14 at 04:51
  • @user3236300 You get a query result as "object" and trying to process it as "array". There is a lot of good answers here in stackoverflow about to solve this. for example check this: http://stackoverflow.com/a/16525482/3235793 ... additionally its better to check examples "how to query with mysqli and process the result set" – optional Jan 27 '14 at 06:07
  • here is what i got now!.... as a test i have it just displaying text as the image so right now it puts a single letter in each box but it only does 2 (ie $rows=2) $result = mysqli_query($con,"SELECT title FROM donuts"); $rows = 2; $cols = ceil(count($result)/$rows); $row = mysqli_fetch_array($result); echo $result=$row['title']; echo ""; $i=0; for($tr=1;$tr<=$rows;$tr++){ echo ""; for($td=1;$td<=$cols;$td++){ if (isset($result[$i])) { echo ""; $i++; } } echo ""; } echo "
    ".$result[$i]."
    ";
    – Kelly Jan 28 '14 at 04:00
  • its very hard to read your code. as I understand, after this code (echo $result = $row['title'];) your result set is not an array anymore. You make it a simple variable from one item of you array. This not related your main question. open new question and ask about handling mysqli result set. you do not know and need help about working mysqli result set. plus I recommend to read basic example about mysqli and result set. your mistake is very basic level. – optional Jan 28 '14 at 15:29