2011-07-29 25 views
0

我有一張圖表,基於幾個值自動填充。這些值是目標(總計)當前(現在的位置)和圖形的總高度。我試圖將目標和目前的數據庫格式轉化爲金錢,並且應該將圖表填充到需要的位置。PHP MySQL從表格中讀取以用於圖表

我讓它使用了一個URL,它是?current=584559096&goal=1000000000,我只用$ _GET。爲了更容易維護周圍的人,我想從一個可以更新的帖子中提取數據庫。

我到目前爲止的代碼是:

<? php function formatMoney($number, $fractional=false) { 
     if ($fractional) { 
      $number = sprintf('%.0f', $number); 
     } 
     while (true) { 
      $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number); 
      if ($replaced != $number) { 
       $number = $replaced; 
      } else { 
       break; 
      } 
     } 
     return $number; 
    } 

     $goal = $row['goal']; 
     $current = $row['current']; 
     $percent = round($current/$goal * 100); 
     $totalheight = 216; 
     $ourheight = $totalheight * $percent/100; 

     $halfofarrowheight = 12; 
      $arrowheight = $totalheight-$ourheight-$halfofarrowheight; 
      if($arrowheight < 0) 
       $arrowheight = 0; 
    mysql_close($con); 
?> 

這裏是我的查詢

mysql_select_db("databasename", $con); 

$result = mysql_query("select * from `dbname`.`tablename"); 

這是我的錯誤警告:被零除在E:\的Inetpub \ wwwroot的\ test.website .NET \網絡\包括\ globalfoot.php在線路36上36

行是$percent = round($current/$goal * 100);

所以,我一直在玩弄這個東西4天,現在試圖從目標列和當前列中將數字格式化爲金錢並填入圖表。對於測試的緣故可以說,我們的目標是萬億,電流爲5000000000.

+0

凡'$ row'越來越設定?你可以顯示該代碼嗎? –

回答

0

您的代碼應該是這樣的:

mysql_select_db("databasename", $con); 
$result = mysql_query("select * from `dbname`.`tablename"); 
if (!$result || !($row = mysql_fetch_assoc($result))) { 
    echo "DB error: ".mysql_error(); 
    exit; 
} 

$goal = $row['goal']; 
$current = $row['current']; 
// ... 
+0

其實這工作!非常感謝你! –

+0

那麼,也許有什麼與您的查詢?看到更新的代碼 – lazyhammer

+0

@ Dmitriy是的,它的工作,我錯過了一個;這對PHP來說總是很痛苦,錯過了一件小事,它毀了你的一天。 –

3
$row['goal'] = $goal; 
$row['current'] = $current; 

請原諒我,如果我沒有理解你的代碼,但不應以上可以:

$goal=$row['goal']; 
$current=$row['current']; 
+0

是的,我轉過身去看它是否會做任何事情,忘記將它切換回來。沒有改變的錯誤。 –

+0

Plese,再次顯示你的$行初始化代碼 – lazyhammer

+1

,不知道我是否教我的奶奶吸雞蛋,但你有沒有嘗試在計算之前回應這兩個變量的值?一個零的警告表明$目標是零或空的,你應該驗證它被設置 – jammypeach

0

始終,在進行分工時總是仔細檢查你的變量。

$percent = ($goal > 0 || $goal < 0) ? round($current/$goal * 100) : 0; 
+0

謝謝馬特擺脫了錯誤。出於某種原因,儘管我的圖形還沒有運行。甚至不會回顯箭頭上的行列。這就像它甚至沒有讀取數據庫,即使測試回聲顯示它會將數字拉出。我是否需要關閉頁面底部的連接? –

+0

希望我可以檢查兩個答案,因爲你們都幫助過。 –