PHP 和mysql while 循环
我是 PHP 新手,我认为这是一个简单的问题。
我可以这样显示来自我的数据库的内容:
...
...
<?php while($rows = mysql_fetch_array($queryNews)){ ?>
<div class='title_news'><?php echo $row['Title'] ?> </div>
.... and so on...
到目前为止还可以
然后我想模拟一种分页:我的意思是显示与新闻一样多的数字 我首先尝试了 for 循环
<?php for($i=1; $i<= sizeof(mysql_fetch_array($queryNews)); $i++){ ?>
<div class="num_txt"><?php echo $i ?></div>
<?php } ?>
没有成功。所以我尝试了与第一种类似的方法,也没有成功。
<?php
$i=1;
while($row = mysql_fetch_array($queryNews)){ ?>
echo '<script language="javascript">confirm('.$i.')</script>;';
<div class="num_txt"><?php echo $i ?></div>
<?php $i++; } ?>
有谁知道我做错了什么? 谢谢
I'm new in PHP and I think this is an easy question.
I can show the content that comes from my db this way:
...
...
<?php while($rows = mysql_fetch_array($queryNews)){ ?>
<div class='title_news'><?php echo $row['Title'] ?> </div>
.... and so on...
Thats ok so far
Then I want to simulate a kind of pagination: I mean showing as many numbers as news
I first tried a for-loop
<?php for($i=1; $i<= sizeof(mysql_fetch_array($queryNews)); $i++){ ?>
<div class="num_txt"><?php echo $i ?></div>
<?php } ?>
No success. so I tried an similar way as the first one, also without success.
<?php
$i=1;
while($row = mysql_fetch_array($queryNews)){ ?>
echo '<script language="javascript">confirm('.$i.')</script>;';
<div class="num_txt"><?php echo $i ?></div>
<?php $i++; } ?>
Does anyone knows what I'm doing wrong??
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的 for 循环几乎是正确的,但是
sizeof(mysql_fetch_array($queryNews))
从结果集中获取一行并返回该行中的列数。您想要结果集中的行数:Your for loop was almost correct, but
sizeof(mysql_fetch_array($queryNews))
fetches a row from the result set and returns the number of columns in that row. You want the number of rows in the result set:要生成数字数组,请尝试以下操作:
这不是大多数人定义的分页,但如果您想了解如何使用 Mysql 中的 LIMIT 子句一次获取 10 个结果组,您可以轻松搜索 PHP Pagination。
To generate an array of numbers try this:
Thats not pagination as most define it, but you can easily search for PHP Pagination if you want to learn how to get groups of, say, 10 results at a time using the LIMIT clause in Mysql.
使用 mysql_num_rows 计算总行数,或创建一个变量,该变量在循环记录以获取总数时递增。
Use
mysql_num_rows
to calculate the total number of rows, or create a variable that increments when you loop through your records to get the total.