1

我的 PHP 代码有问题。我在 mysql 数据库中存储了 8 行通道,我使用 var totalrowcount 显示 8 个框,以便 div 类显示我从 mysql 存储的行,但是当我存储超过 8 个 div 类时,我无法显示超过 8 个框mysql中有8行。

我想为具有任何数字范围的 div 类创建无限框以匹配行取决于我存储在 mysql 数据库中的行数。

这是我发现问题所在的代码:

    var totalrowcount = 8;
<div class="mainWrap">
    <div class="row" id="row1">
        <div id="image1" class="channelList div_1_1"></div>
        <div class="rowSubPgm">
            <div id="programe1" class="pgmFirstRow div_1_2"></div>
            <div id="programe2" class="pgmFirstRow div_1_3"></div>
            <div id="programe3" class="pgmFirstRow div_1_4"></div>
        </div>
    </div>
    <div class="clear"></div>
    <div class="row"  id="row2">
        <div id="image2" class="channelList div_2_1"></div>
        <div class="rowSubPgm">
            <div id="programe4" class="pgmFirstRow div_2_2"></div>
            <div id="programe5" class="pgmFirstRow div_2_3"></div>
            <div id="programe6" class="pgmFirstRow div_2_4"></div>
        </div>
    </div>
    <div class="clear"></div>
    <div class="row"  id="row3">
        <div id="image3" class="channelList div_3_1"></div>
        <div class="rowSubPgm">
            <div id="programe7" class="pgmFirstRow div_3_2"></div>
            <div id="programe8" class="pgmFirstRow div_3_3"></div>
            <div id="programe9" class="pgmFirstRow div_3_4"></div>
       </div>
    </div>
    <div class="clear"></div>
    <div class="row"  id="row4">
        <div id="image4" class="channelList div_4_1"></div>
        <div class="rowSubPgm">
            <div id="programe10" class="pgmFirstRow div_4_2"></div>
            <div id="programe11" class="pgmFirstRow div_4_3"></div>
            <div id="programe12" class="pgmFirstRow div_4_4"></div>
       </div>
    </div>
    <div class="clear"></div>
    <div class="row"  id="row5">
        <div id="image5" class="channelList div_5_1"></div>
        <div class="rowSubPgm">
            <div id="programe13" class="pgmFirstRow div_5_2"></div>
            <div id="programe14" class="pgmFirstRow div_5_3"></div>
            <div id="programe15" class="pgmFirstRow div_5_4"></div>
        </div>
    </div>
    <div class="clear"></div>
    <div class="row" id="row6">
        <div id="image6" class="channelList div_6_1"></div>
        <div class="rowSubPgm">
            <div id="programe16" class="pgmFirstRow div_6_2"></div>
            <div id="programe17" class="pgmFirstRow div_6_3"></div>
            <div id="programe18" class="pgmFirstRow div_6_4"></div>
       </div>
    </div>
    <div class="clear"></div>
        <div class="row"  id="row7">
        <div id="image7" class="channelList div_7_1"></div>
        <div class="rowSubPgm">
            <div id="programe19" class="pgmFirstRow div_7_2"></div>
            <div id="programe20" class="pgmFirstRow div_7_3"></div>
            <div id="programe21" class="pgmFirstRow div_7_4"></div>
        </div>
    </div>
    <div class="clear"></div>
        <div class="row"  id="row8">
        <div id="image8" class="channelList div_8_1"></div>
        <div class="rowSubPgm">
            <div id="programe22" class="pgmFirstRow div_8_2"></div>
            <div id="programe23" class="pgmFirstRow div_8_3"></div>
            <div id="programe24" class="pgmFirstRow div_8_4"></div>
        </div>
    </div>
    <div class="clear"></div>

我可以毫无问题地从 mysql 输出无限行,但我不能为 div 类显示超过 8 个框。如果我想创建类,我将不得不更改每行末尾的最后一个数字,例如:pgmFirstRow div_9_4、pgmFirstRow div_10_4 等等。

我将在 mysql 中存储数千行,我发现为添加 div 类添加数千行代码对我来说太过分了。

有谁知道如何创建代码以允许我使用数组添加无限数量的 div 类,以匹配我存储在 mysql 中的行以允许我为 div 类添加任何范围的框?

任何建议将不胜感激。

4

1 回答 1

2

这就是他们发明forandwhile循环的原因。div您可以使用循环继续打印s 和行,直到到达末尾。

PDO例子:

$query = $con->prepare("/* Your QUERY*/");
try {
// Try to execute the query
$query->execute();
}
catch(PDOException $e) {
// Error jumps here
echo $e->getMessage();
}
// Set fetch mode so we can retrieve the data by an object
$query->setFetchMode(PDO::FETCH_OBJ);

while($row = $query->fetch()){
/* While there are rows available we continue executing this loop */
/* You can get data using $row->id for example */
echo 
"<div class=\"clear\"></div>
<div class=\"row\"  id=\"row".$row->id."\">
    <div id=\"image".$row->id."\" class=\"channelList div_".$row->id."_1\"></div>
    <div class=\"rowSubPgm\">
        <div id=\"programe".$row->id."\" class=\"pgmFirstRow div_".$row->id."_2\"></div>
        <div id=\"programe".$row->id."\" class=\"pgmFirstRow div_".$row->id."_3\"></div>
        <div id=\"programe".$row->id."\" class=\"pgmFirstRow div_".$row->id."_4\"></div>
    </div>";
}

mysql_*例子。(我不推荐使用这个。我只是为你写的,以防你更熟悉这个而不是 PDO。切换到 PDO 总是一个好主意。

// Assuming you already executed the query
while($row = mysql_fetch_assoc($result)){
// Here it's pretty much the same as PDO. Only now the $row is an array instead of an object
echo
    "<div class=\"clear\"></div>
    <div class=\"row\"  id=\"row".$row['id']."\">
    <div id=\"image".$row['id']."\" class=\"channelList div_".$row['id']."_1\"></div>
    <div class=\"rowSubPgm\">
        <div id=\"programe".$row['id']."\" class=\"pgmFirstRow div_".$row['id']."_2\"></div>
        <div id=\"programe".$row['id']."\" class=\"pgmFirstRow div_".$row['id']."_3\"></div>
        <div id=\"programe".$row['id']."\" class=\"pgmFirstRow div_".$row['id']."_4\"></div>
    </div>";
}

编辑:

顺便说一句,我不建议同时显示所有行。想象一下,您的行数增长到数以千计,并且您想要打印一千条记录。我敢打赌 mysql 服务器和浏览器都会对这个事实感到不安 :) 而是尝试对行进行分页。

于 2013-06-04T22:16:35.050 回答