-1

I am trying to make a webpage that will add a card filled with data from a database if there is a row of data there. I have a <div class> that formats the card. Is there a way to programmatically add the <div class> so each <div class> is a row of data?

This is the PHP I have, it does read all the rows properly:

//SQL SELECT statement
$result = $conn->prepare("SELECT userid, pName, pDesc, dDate FROM test");
$result->execute();
// assign returned array elements to variables
for($i=0; $row=$result->fetch(); $i++){
   $pName = $row['pName'];
   $pDesc = $row['pDesc'];
   $dDate = $row['dDate'];
}

Here is the HTML, it currently only displays the last row of data:

<h1>Project Dashboard</h1>
<div class="project-container">
  <label>Project Owner:</label>
  <span><?php echo $pName; ?></span><br>
  <label>Project Description:</label>
  <span><?php echo $pDesc; ?> </span><br>
  <label>Project Due Date:</label>
  <span><?php echo $dDate; ?> </span><br>
  <div class="progress-bar">
    <div id="myBar" class="container purple" style="height:24px;width:25%">
    </div>
  </div>
</div>
showdev
  • 28,454
  • 37
  • 55
  • 73
Kip242
  • 7
  • 3
  • 2
    well you write over the variables on each iteration of the loop.... – epascarello Dec 20 '17 at 19:00
  • 1
    The variables are going to be whatever the last run of the loop sets them as. You'll need to have your HTML within the loop before it changes the variable again, so that it's creating new divs each time it runs through the array. – Caitlyn Dec 20 '17 at 19:02
  • Creating the html for the div should be done inside the loop. – James Dec 20 '17 at 19:02
  • You need to output the HTML on each iteration. – Iker Vázquez Dec 20 '17 at 19:04
  • Also, your question is missing some text that has been filtered out because it's wrapped in angle brackets, ie `
    `. I was going to edit it but it would have changed the meaning of your question.
    – James Dec 20 '17 at 19:04
  • Possible duplicate of [While Loop using PHP with a MySQL server](https://stackoverflow.com/questions/9395941/while-loop-using-php-with-a-mysql-server) – showdev Dec 20 '17 at 19:47

1 Answers1

0

As mentioned in the comments, the PHP values are overwritten upon each iteration of the for loop, so only the last row of values will be displayed. For example, $pName can only hold one value at a time. So after the loop, $pName is defined as the last row's pName value.

I suggest using a while loop to output your HTML rows. Below, I'm assuming each .project-container is a row. I'm also using PDO.

<?php

while ($row=$result->fetch(PDO::FETCH_ASSOC)) {

  ?><div class="project-container">
    <label>Project Owner:</label>
    <span><?=$row['pName']?></span><br>
    <label>Project Description:</label>
    <span><?=$row['pDesc']?></span><br>
    <label>Project Due Date:</label>
    <span><?=$row['dDate']?></span>
  </div><?php

}

Alternatively, you could fetch all the rows into a single PHP array and then loop through the array.

$rows=$result->fetchAll(PDO::FETCH_ASSOC);

foreach ($rows as $row) {
  // output row's HTML, just like above
}

See PDO::fetchAll vs. PDO::fetch in a loop for performance considerations.

showdev
  • 28,454
  • 37
  • 55
  • 73