0

I want to loop through each result from the mysql table and generate html div for each rows. The database table looks like following:

--------------------------------
  id   |  color  | width | img
--------------------------------
   1   |  red    |  550  | url
--------------------------------
   2   |  black  |  650  | url
--------------------------------

I have an idea of how this might work but not sure about the syntax. Below is my attempt.

<?php
// connect to the database

$rows = // get rows from the db table

foreach ($rows) {
<div style="background-color:$color;width:$width;">
    <img src="$img">
</div>
}
5
  • 2
    So what's the difficulty there? (Don't say you want an example. There are billions of them all around. See the manual.) Commented Aug 27, 2015 at 23:17
  • Unless your sample was intentionally oversimplified, you need to echo(); the content in the for each loop to get it to output to the screen. Commented Aug 27, 2015 at 23:21
  • @scunliffe I would image that an sql command and a function call to issue that query to the database server and a function call to retrieve the result set would also come in quite handy as well. Commented Aug 27, 2015 at 23:28
  • Come back when you've fixed the syntax errors. In the meantime I'm voting to close this question. Commented Aug 27, 2015 at 23:52
  • possible duplicate of PHP PDO with foreach and fetch Commented Aug 28, 2015 at 11:00

4 Answers 4

4

This is a simple example with a simple query.

<?php

$mysqli = new mysqli("db_host", "db_user", "db_password", "db_name");

// check connection
if ($mysqli->connect_errno) {
    die("Connect failed: ".$mysqli->connect_error);
}

$query = "SELECT * FROM imgs";
$result = $mysqli->query($query);

while($row = $result->fetch_array()){
echo '<div style="background-color:'.$row[color].';width:'.$row[width].';"><img src="'.$row[img].'"></div>';
}
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming that the output of the database is same as $rows, your code should look like follows. Obviously you can improve on the concatenation of the echo statements,.

<?php

$rows =  [
    'row1' => [
        'color' => 'red',
        'width' => '550px',
        'img' => 'http://www.google.com.au/images/srpr/logo11w.png'
    ],
    'row2' => [
        'color' => 'black',
        'width' => '650px',
        'img' => 'http://www.google.com.au/images/srpr/logo11w.png'
    ],
];

foreach ($rows as $key => $row) {
    $color = $row['color'];
    $image = $row['img'];
    $width = $row['width'];

    echo "<div style=\"background-color:$color;width:$width;\">";
    echo "<img src=\"$image\">";
    echo "</div>";
}

?>

Here is a working phpfiddle

Comments

1
foreach($result as $key=>$val){
     var_dump($val) ;
}

You can try it ; and you must be sure mysql $row is array !

Comments

0

Here is a simple loop that will display images inside a div.

<?php
for($i = 0; $i < 3; $i++){
    echo "<div style='border: 1px solid black;'>";
    echo "<img src='http://screenrant.com/wp-content/uploads/James-Earl-Jones-Darth-Vader-Star-Wars-Rebels.jpg'>";
    echo "</div>";
}?>

Advise : Beware when to use double quotes and single quotes in a echo statement when you want to display a HTML script using echo or else it wouldn't display correctly. Also, use the correct syntax when handling result queries using foreach.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.