0

我正在使用 turn.js 制作一本书。div“死亡笔记”中的每一个都div成为一个新的页面。

<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

我正在做的是尝试将 3 个“内容”(内容是名称和死因)divs放到 1 个页面上,然后生成一个新页面。

所以这就是我想要的:

<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

看起来很简单,但是内容是来自 MySQL 数据库的数据,所以我必须在使用 PHP 时回应它。这是我到目前为止所拥有的

<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>

目前我似乎正在导致无限循环,因此页面不会加载。问题存在于while循环中。

任何帮助是极大的赞赏。提前谢谢各位。:)

4

2 回答 2

0

请先在您的本地实例中运行以下代码,并确认以下代码是您预期的概念。之后,您可以更改结果集并告诉我,

  <?php 

       $Numbers = range(1, 25); // change your result set here 
       $Limit = 3;
       $i=0;
       foreach($Numbers as $number){

            if($i==$Limit){
                    echo "<hr>"; // new page
                    $i=0; // reset the limiter
            }

            echo $number; // Values to be displayed

            $i++;
        }             
      ?>
于 2013-10-31T05:40:45.720 回答
0

我已经为你重写了循环,现在循环遍历你的整个数据库并在需要时放置开始 div 和关闭 div。

<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>

<?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 . ']');
}

$pagecount=0;
while($row = $result->fetch_assoc()){
if($pagecount==0){ echo '<div style="background-image:url(images/paper.jpg);">';}
echo '<div class="content"><div class="name">'  . $row['victim'] . '</div><div class="cod">'  . $row['cod'] . '</div></div>';
if($pagecount==2){echo '</div>';}
$pagecount++;
if($pagecount==3){$pagecount=0;}
}
//this page count closes the last div (page) if you have less than three entries in it
if($pagecount==1||$pagecount==2){echo '</div>';}
$db->close();
?>
    <div style="background-image:url(images/backpage.jpg);"></div> //BACK PAGE
</div>
于 2013-10-31T06:27:55.637 回答