Thun Thun - 7 months ago 46
HTML Question

PHP while loop number of rows

im struggling with php while loop. The goal is to get the quote from the database and display it with 3 columns on each row. As it look now the while loop is displaying one column each row. How should i correct this problem?

<?php

$servername = "localhost";
$username = "";
$password = "";
$dbname = "";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$row = $ip['id'];
$row = $ip['quote'];
$row = $ip['topic'];
$row = $ip['author'];
$nr = 0;


$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$result = $conn->query($sql);


if ($result->num_rows > 0) {



while($row = $result->fetch_assoc() ) {
$nr++;


echo
"<div class='container row'>
<div class='col s12 m6 l4 z-depth-1'>
<div class='card-panel grey darken-4 white-text center'><h5>Citat: ". $row["id"] ."</h5></div> <pre class='flow-text black-text' wrap='soft'>" ."<p class=''>Författare: ". $row["author"] ."</p>"
. "<p class=''>Citat: ". $row["quote"] ."</p>" . $row["topic"] ."</pre>

<div class='content_wrapper'>
<h4>Vote </h4>

<div class='voting_wrapper' id='". $row["id"] ."'>
<div class='voting_btn'>
<div class='up_button'>&nbsp;</div><span class='up_votes'>0</span>
</div>
<div class='voting_btn'>
<div class='down_button'>&nbsp;</div><span class='down_votes'>0</span>
</div>
<br>
</div>


</div>
</div>
</div>";

}
}else {
echo "0 results";
}

$conn->close();
?>

Answer

You messed up with the variables. Don't need to declare $row below:

    $row = $ip['id']; 
    $row = $ip['quote'];  
    $row = $ip['topic'];
    $row = $ip['author'];

As stated here Why shouldn't I use mysql_* functions in PHP? don't use mysql_* functions.

You need to fetch 3 rows and place them inside one container.

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

$db = new PDO("mysql:host=$servername;dbname=$dbname;charset=UTF-8", 
              $username 
              $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

$sql = "SELECT * FROM quotes ORDER BY date DESC limit 10";
$nr = 0;

$stmt = $db->query('SELECT * FROM table');

while($row1 = $stmt->fetch(PDO::FETCH_ASSOC)) {
   $row2 = $stmt->fetch(PDO::FETCH_ASSOC);
   $row3 = $stmt->fetch(PDO::FETCH_ASSOC);
   $rows[] = $row1;
   if ($row2) $rows[] = $row2;
   if ($row3) $rows[] = $row3;

   echo "<div class='container row'>\n"

   foreach($rows as $row) {
      $nr++;
      $id_     = $row["id"];
      $author_ = $row["author"];
      $quote_  = $row["quote"];
      $topic_  = $row["topic"];

      echo 
        "<div class='col s12 m6 l4 z-depth-1'>
         <div class='card-panel grey darken-4 white-text center'><h5>Citat:$id_ </h5></div> 
           <pre class='flow-text black-text' wrap='soft'>
              <p class=''>Författare: $author_</p> 
              <p class=''>Citat: $quote_</p>
              $topic_
           </pre>

        <div class='content_wrapper'>
           <h4>Vote </h4> 
           <div class='voting_wrapper' id='$id_'>
            <div class='voting_btn'>
                <div class='up_button'>&nbsp;</div><span class='up_votes'>0</span>
            </div>
            <div class='voting_btn'>
                <div class='down_button'>&nbsp;</div><span class='down_votes'>0</span>
            </div>
            <br />
        </div>
        </div>
        </div>";
    }

    echo "</div>\n";
    $rows = null;
}

if ($nr == 0){
   echo "0 results";
}
?>
Comments