简体   繁体   中英

PHP while statement only working for one row

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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