Echo mysql results in a loop?
Posted
by
Roy D. Porter
on Stack Overflow
See other posts from Stack Overflow
or by Roy D. Porter
Published on 2013-10-31T04:43:32Z
Indexed on
2013/10/31
9:54 UTC
Read the original article
Hit count: 246
I am using turn.js to make a book.
Every div
within the 'deathnote' div
becomes a new page.
<div id="deathnote"> //starts book
<div style="background-image:url(images/coverpage.jpg);"></div> //creates new page
<div style="background-image:url(images/paper.jpg);"></div> //creates new page
<div style="background-image:url(images/paper.jpg);"></div> //creates new page
</div> //ends book
What I am doing is trying to get 3 'content' (content being a name and cause of death) divs
onto 1 page, and then generate a new page.
So here is what i want:
<div id="deathnote"> //starts book
<div style="background-image:url(images/coverpage.jpg);"></div> //creates new page
<div style="background-image:url(images/paper.jpg);"></div> //creates new page
<div style="background-image:url(images/paper.jpg);"> //creates new page but leaves it open
<div> CONTENT </div>
<div> CONTENT </div>
<div> CONTENT </div>
</div> //ends the page
</div> //ends book
Seems simple enough, however the content is data from a MySQL DB, so i have to echo it in using PHP. Here is what i have so far
<div id="deathnote">
<div style="background-image:url(images/coverpage.jpg);"></div>
<div style="background-image:url(images/paper.jpg);"></div>
<div style="background-image:url(images/paper.jpg);"></div>
<div style="background-image:url(images/paper.jpg);"></div>
<div style="background-image:url(images/paper.jpg);"></div>
<div style="background-image:url(images/paper.jpg);"></div>
<?php
$pagecount = 0;
$db = new mysqli('localhost', 'username', 'passw', 'DB');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
$sql = <<<SQL
SELECT *
FROM `TABLE`
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
//IGNORE ALL OF THE GARBAGE ABOVE. IT IS SIMPLE CONNECTING SCRIPT THAT I KNOW WORKS
//THE METHOD I AM HAVING TROUBLE WITH IS BELOW
$pagecount = 0;
while($row = $result->fetch_assoc()){ //GETS THE VALUE (and makes sure it isn't nothing
echo '<div style="background-image:url(images/paper.jpg);">'; //THIS OPENS A NEW PAGE
while ($pagecount !== 3) { //KEEPS COUNT OF HOW MUCH CONTENT DIVS IS ON THE PAGE
while($row = $result->fetch_assoc()){
//START A CONTENT DIV
echo '<div class="content"><div class="name">' . $row['victim'] . '</div><div class="cod">' . $row['cod'] . '</div></div>';
//END A CONTENT DIV
$pagecount++; //UP THE PAGE COUNT
}
}
$pagecount=0; //PUT IT BACK TO 0
echo '</div>'; //END PAGE
}
$db->close();
?>
<div style="background-image:url(images/backpage.jpg);"></div> //BACK PAGE
</div>
At the moment i seem to be causing and infinite loop so the page won't load. The problem resides within the while
loops.
Any help is greatly appreciated. Thanks in advance guys. :)
© Stack Overflow or respective owner