Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Foreach php loop to display html unordered list of mysql table

i’m trying to display unordered lists made with sql table columns but i get empty lists which i don’t get because when i type

echo $stories[0]['title'];

i get the value expected (jason).

I have tried with and without echo but the result is the same..

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

here is my table :

mysql> select * from story;

+----+-------+-------------------------+---------+ 
| id | title | content                 | author  |
+----+-------+-------------------------+---------+ 
|  1 | jason | histoire des argonautes | guigous | 
|  2 | jason | histoire des argonautes | guigous |

and my html :

<?php
require 'connec.php';
$pdo = new PDO(DSN,USER,PASS);

$query ='select * from story';
$statement = $pdo->query($query);
$stories= $statement->fetchAll();
?>

<!DOCTYPE html>
<body>

<?php 
echo $stories[0]['title'];
for ($i=0; count($stories)>$i; $i++) :
?>  
    <ul>
        <li> <? echo $stories[$i]['title']?></li>
        <li><? echo $stories[$i]['content']?></li>
        <li><? echo $stories[$i]['author']?></li>
    </ul> 
<?php endfor; ?>
</body>

thanks you for your time !

>Solution :

Can you try this code, not sure is correct or not, also it will be great if you can provide exported SQL table so we can replicate the environment

But meanwhile you can try this code:

<?php
for(int $i = 0; $i < count($stories); $i++) {
  ?>
   <ul>
     <li><?php echo $stories[$i]['id']; ?></li>
     <li><?php echo $stories[$i]['title']; ?></li>
     <li><?php echo $stories[$i]['content']; ?></li>
     <li><?php echo $stories[$i]['author']; ?></li>
   </ul>
<?php
}
?>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading