PHP while 语句仅适用于一行

发布于 2024-12-10 03:47:00 字数 1466 浏览 0 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失去的东西太少 2024-12-17 03:47:01

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

<?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"> 
lt;?php echo formatmoney($current);?></div>
    <h4>
lt;?php echo formatmoney($goal);?></h4>
    </div>
    <?php } ?>

you should try this one, you were defining your variables only once

<?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"> 
lt;?php echo formatmoney($current);?></div>
    <h4>
lt;?php echo formatmoney($goal);?></h4>
    </div>
    <?php } ?>
岁月静好 2024-12-17 03:47:01

该代码是一团糟。

a)

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

不要在路径中使用反斜杠。仅使用正斜杠/。如果您使用的是 Windows,PHP 会补偿您,并自动转换为反斜杠。像这样的字符串中的反斜杠可能会被误解为转义字符,而不是路径引用。

b)

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

你错过了 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;
    }

That code is a mess.

a)

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

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)

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

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)

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

The proper (+ simplest) method to check for a DB error is:

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

Don't check if there's any results by trying to fetch a row. That's what mysql_num_rows() is for.

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

d) Stylistically, do/while loops for database results are a bit ugly. You'd be better off with a more standard:

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

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:

    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文