繁体   English   中英

PHP while语句仅适用于一行

[英]PHP while statement only working for one row

我试图让while语句显示所有表行。 目前,我有3行,例如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)); ?>

因此,如何获取它以升序列出而不重复第一行?

您应该尝试一下,您只定义了一次变量

<?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 } ?>

该代码是一团糟。

一种)

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和其他增值经销商只有一次,你取环之外 无需将检索到的数据库行提取为单个变量,您可以直接将它们输出:

    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;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM