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>
}
Subash
  • 7,098
  • 7
  • 44
  • 70
Splycd
  • 161
  • 2
  • 3
  • 15
  • 2
    So what's the difficulty there? (Don't say you want an example. There are billions of them all around. See the manual.) – mario Aug 27 '15 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. – scunliffe Aug 27 '15 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. – RiggsFolly Aug 27 '15 at 23:28
  • Come back when you've fixed the syntax errors. In the meantime I'm voting to close this question. – Mike Aug 27 '15 at 23:52
  • possible duplicate of [PHP PDO with foreach and fetch](http://stackoverflow.com/q/15385965) – mario Aug 28 '15 at 11:00

4 Answers4

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>';
}
Maverick
  • 905
  • 8
  • 23
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

Subash
  • 7,098
  • 7
  • 44
  • 70
1
foreach($result as $key=>$val){
     var_dump($val) ;
}

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

imdba
  • 46
  • 1
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.

WaduNoop
  • 65
  • 1
  • 2
  • 14