PHP while语句只适用于一行

时间:2011-10-14 21:34:55

标签: php css while-loop

我正在尝试使用while语句显示所有表行。我目前有三排,如R1 Business R2 Communication和R3 Education。 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)); ?>

那么,如何让它列出上升而不是重复第一行?

2 个答案:

答案 0 :(得分:2)

你应该尝试这个,你只需要定义一次变量

<?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");

while($row = mysql_fetch_assoc($result)){
    $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;

    $halfofarrowheight = 36;
    $arrowheight = $ourwidth-$halfofarrowheight; 
    if($arrowheight < 0)
        $arrowheight = 0;
    ?>
    <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 } ?>

答案 1 :(得分:1)

那段代码很乱。

A)

require_once($_SERVER['DOCUMENT_ROOT'].'\includes\format.php');

不要在路径中使用反斜杠。仅使用转发/斜杠。如果您在Windows上,PHP将补偿您,并自动转换为反斜杠。像这样的字符串中的反斜杠打开了误解为转义字符的大门,而不是路径引用。

b)

$result = mysql_query("select * from `database`.`collegemeter");

你错过了大学毕业计的结束反击,除非你使用保留字作为你的数据库名称,否则完全没必要使用反引号。

c)中

if (!$result || !($row = mysql_fetch_assoc($result))) {

检查数据库错误的正确(+最简单)方法是:

if ($result === false) {
    die(mysql_error());
}

不要通过尝试获取行来检查是否有任何结果。这就是mysql_num_rows()的用途。

if (mysql_num_rows($result) == 0) then
     die("No results!");
}

d)在风格上,数据库结果的do / while循环有点难看。你会有更好的标准:

while($row = mysql_fetch_assoc($result)) { ... }

构造。这也可以让您看到您只是定义了$college和其他变量,只有你的获取循环的ONCE, OUTSIDE 。无需将检索数据库行提取到单个变量中,您可以直接输出它们:

    while ($row = ...) {
        $money = moneyformat($row['goal']);
        echo <<<EOL
    <div class="progresswrap">
    <p><a href="{$row['url']}">{$row['college']}</a></p>
    <div class="progresscontainer">
    etc...
    EOL;
    }