2011-10-14 61 views
0

我試圖讓while語句顯示所有的表格行。目前我有三行,如R1 Business R2 Communication和R3 Education。它顯示三個,但全部是R1。所以,而不是列出商業通信教育,它是商業商業業務。PHP while while statement only only for one row

我的代碼是:

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

你應該試試這個,你定義的變量只有一次

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

那一個會列出他們每個人除了第一行就被替換>>>和所有的數字都是一樣的,儀表應該根據數據庫中的數字進行調整,以便他們能夠看到多遠,直到他們達到目標。 –

+0

@EMAWandCougs:我沒有得到您的評論 – genesis

+0

它顯示錶中的每一行,除了第一個。列行中的數字有一個公式,它告訴div在哪裏,所以它是一個進度表,現在他們都是一樣的。 –

1

該代碼是一個爛攤子。

一)

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

不要在路徑中使用反斜槓。僅使用正向/斜槓。如果你在Windows上,PHP會補償你,並自動轉換成反斜槓。像這樣的字符串反斜槓打開了將錯誤解讀爲轉義字符的大門,而不是路徑引用。

B)

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

你缺少collegemeter收盤反引號,除非your're使用保留字作爲數據庫名稱,反引號是完全不必要的。

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和其他變量只有一個,OUTSIDE。有沒有必要來獲取檢索DB一行到各個變量,可以直接輸出繼電器他們:

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

好吧,我做了修改。它現在將大學名稱拉出來的數字依然沒有變化。所以在進度條上它們的數量都是相同的,應該根據所提出的目標百分比來變化。 –