-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  |
-------|
2
  • @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 :) Commented Jan 25, 2014 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. Commented Jan 26, 2014 at 0:10

1 Answer 1

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

*/
Sign up to request clarification or add additional context in comments.

4 Comments

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 <code> $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 "<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>"; </code>
@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: stackoverflow.com/a/16525482/3235793 ... additionally its better to check examples "how to query with mysqli and process the result set"
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) <code> $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 "<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>"; </code>
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.

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.