[英]Why is my list function only outputting first value of my array?
I'm still a php novice...I apologize if this is a silly question. 我仍然是php新手...很抱歉这是一个愚蠢的问题。
I have an array of numerical values ('views'), each 'view' specific to a video. 我有一个数字值数组(“视图”),每个特定于视频的“视图”。 I want to output the top 'views' into cells of a table (html).
我想将顶部的“视图”输出到表格的单元格(html)。 Whether my list is ascending or descending, I continue to only see the first number (but the correct number) of the list ($views1 in the first row).
无论列表是升序还是降序,我都只会看到列表的第一个数字(但正确的数字)(第一行中的$ views1)。 Here is my code:
这是我的代码:
<? php //lots of code, followed by
include 'connect.php';
$views=mysql_fetch_array(mysql_query("SELECT video.views FROM video ORDER by
video.views DESC LIMIT 10"));
list($views1,$views2,$views3,$views4,$views5,$views6,$views7,$views8,$views9,$views10)
= $views;
?>
<html>
<body>
<table>
<tr><td><?php echo $views1 ?></td></tr>
<tr><td><?php echo $views2 ?></td></tr>
. //My actual code has 10 rows going from $views1 to $views10
.
.
<tr><td><?php echo $views10 ?></td></tr>
</body>
</html>
I just cannot see what the problem is...thanks for any help! 我只是看不到问题出在哪里...谢谢您的帮助!
use 采用
var_dump($views);
before your list(...) statement to debug it. 在您的list(...)语句之前对其进行调试。 It may be $views isn't being populated like you think.
$ views可能没有像您想象的那样被填充。
This code should work: 此代码应工作:
<?php //lots of code, followed by
include 'connect.php';
$q=mysql_query("SELECT video.views FROM video ORDER by video.views DESC LIMIT 10");
$views=array();
while ($views[]=mysql_fetch_array($q));
?>
<html>
<body>
<table>
<?php
foreach ($views as $view)
echo '<tr><td>'.$view['views'].'</td></tr>';
?>
</table>
</body>
</html>
Don't forget that you shouldn't write duplicate codes, loops are there for you. 不要忘记您不应该编写重复的代码,循环为您服务。
That's because mysql_fetch_array
fetches one row, so even assigning a list of variables in this manner will only set as much as there is colunms in the result - one, in your case. 这是因为
mysql_fetch_array
取一排,因此,即使分配以这种方式的变量列表,因为在结果colunms将只设置尽可能多-一个,你的情况。 Use something like that instead: 使用类似的东西:
$res = mysql_query(...);
while ($arr = mysql_fetch_array($res)) $views[] = $arr[0];
<tr><td><?=$views[0]?></td></tr>
...
By the way, mysql extension is deprecated, use mysqli instead. 顺便提一下,不建议使用mysql扩展,而应使用mysqli。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.