我的PHP中的代码有问题。我在mysql数据库中存储了8行通道,并使用var totalrowcount显示8个div类的框,以显示我从mysql存储的行,但是当我存储多个行时,我无法显示超过8个div类的框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类添加数千行代码对我来说太过分了。
有没有人知道如何创建代码以允许我使用数组为totalrowcount添加无限数量的div类来匹配我存储在mysql中的行,以允许我为div类添加任何范围的框? ?
非常感谢任何建议。
答案 0 :(得分:2)
这就是为什么他们发明了for
和while
循环。您可以继续打印div
和行,直到您使用循环结束。
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服务器和浏览器都会对这个事实感到不安:)而是尝试对你的行进行分页。