PHP while 语句仅适用于一行
我试图让 while 语句显示所有表行。我现在有三行,例如R1商业R2通信和R3教育。显示三,但都是R1。因此,列出的不是商务沟通教育,而是商务商务商务。
我的代码是:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'\includes\format.php');
$con = mysql_connect("server","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("foundation", $con);
$result = mysql_query("select * from `database`.`collegemeter");
?>
<?php
if (!$result || !($row = mysql_fetch_assoc($result))) {
echo "DB error";
exit;
}
$college = $row["college"];
$date = $row["date"];
$goal = $row["goal"];
$url = $row["url"];
$current = $row["current"];
$percent = round($current / $goal * 100);
$totalwidth = 245;
$ourwidth = $totalwidth * $percent / 100;
?>
<?php
$halfofarrowheight = 36;
$arrowheight = $ourwidth-$halfofarrowheight;
if($arrowheight < 0)
$arrowheight = 0;
?>
<?php do { ?>
<div class="progresswrap">
<p><a href="<?php echo $url;?>"><?php echo $college;?></a></p>
<div class="progresscontainer">
<div class="progress"></div>
</div>
<div class="arrow"> $<?php echo formatmoney($current);?></div>
<h4>$<?php echo formatmoney($goal);?></h4>
</div>
<?php } while ($row_college = mysql_fetch_assoc($result)); ?>
那么,如何让它按升序列出而不重复第一行?
I am trying to have a while statement display all table rows. I have three rows at the moment such as R1 Business R2 Communication and R3 Education. It is display three but all of R1. So instead of listing Business Communication Education it is Business Business Business.
The code I have is:
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'\includes\format.php');
$con = mysql_connect("server","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("foundation", $con);
$result = mysql_query("select * from `database`.`collegemeter");
?>
<?php
if (!$result || !($row = mysql_fetch_assoc($result))) {
echo "DB error";
exit;
}
$college = $row["college"];
$date = $row["date"];
$goal = $row["goal"];
$url = $row["url"];
$current = $row["current"];
$percent = round($current / $goal * 100);
$totalwidth = 245;
$ourwidth = $totalwidth * $percent / 100;
?>
<?php
$halfofarrowheight = 36;
$arrowheight = $ourwidth-$halfofarrowheight;
if($arrowheight < 0)
$arrowheight = 0;
?>
<?php do { ?>
<div class="progresswrap">
<p><a href="<?php echo $url;?>"><?php echo $college;?></a></p>
<div class="progresscontainer">
<div class="progress"></div>
</div>
<div class="arrow"> lt;?php echo formatmoney($current);?></div>
<h4>lt;?php echo formatmoney($goal);?></h4>
</div>
<?php } while ($row_college = mysql_fetch_assoc($result)); ?>
So, how do I get it to list ascending down and not repeating the first row?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你应该尝试这个,你只定义了一次变量
you should try this one, you were defining your variables only once
该代码是一团糟。
a)
不要在路径中使用反斜杠。仅使用正斜杠
/
。如果您使用的是 Windows,PHP 会补偿您,并自动转换为反斜杠。像这样的字符串中的反斜杠可能会被误解为转义字符,而不是路径引用。b)
你错过了 Collegemeter 上的结束反引号,除非你使用数据库名称的保留字,否则反引号是完全没有必要的。
c)
检查数据库错误的正确(+最简单)方法是:
不要通过尝试获取行来检查是否有任何结果。这就是 mysql_num_rows() 的用途。
d) 从风格上来说,数据库结果的 do/while 循环有点难看。你最好有一个更标准的:
构造。这还可以让您看到您仅在获取循环的外部定义您的
$college
和其他变量一次。无需将检索数据库行提取到各个变量中,您可以直接输出它们:That code is a mess.
a)
Don't use backslashes in paths. Use forward
/
slashes only. PHP will compensate for you if you're on Windows, and auto-transate into backslashes. Backslashes in strings like that open the door to misinterpretation as an escape character, and not a path reference.b)
You're missing the closing backtick on collegemeter, and unless your're using a reserved word for your database name, the backticks are totally unnecessary.
c)
The proper (+ simplest) method to check for a DB error is:
Don't check if there's any results by trying to fetch a row. That's what
mysql_num_rows()
is for.d) Stylistically, do/while loops for database results are a bit ugly. You'd be better off with a more standard:
construct. This would also let you see that you're only defining your
$college
and other vars only ONCE, OUTSIDE of your fetch loop. There's no need to fetch the retrieve DB row into individual variables, you can ouput them directly: