Why can't I do algebra in my PHP loop?

  • Thread starter Thread starter dhvrm
  • Start date Start date
D

dhvrm

Guest
It comes up empty because $votes isn't declared.

It's nearly impossible to tell what you are trying to accomplish, but your code is a total mess.

<?php
$link = mysql_connect('host', 'user', 'pass') or die('Cannot connect to db server');
mysql_select_db('dbname') or die('Cannot select database');
$rs = mysql_query("SELECT * FROM chewytable ORDER BY rating DESC") or die('Cannot parse query');
if(mysql_num_rows($rs) == 0) {
die('No records found');
}
else {
$row = mysql_fetch_array($rs);
}
echo "<table>\n";
echo "<tr>\n";
echo "<td>$row
HTML:
</td>\n";
echo "<td>";
echo "<strong>$row[name]</strong>";
echo "<strong>Rating:</strong> ";
$rate = $row['rating'] / ($row['votes'] + 1)
echo round( $rate, 2);
echo "<strong>Number of ratings: </strong>";
echo $row['votes'];
echo "<strong>Votes plus one: </strong>";
echo $row['votes'] + 1;
echo "<strong>Description:</strong> ";
echo $row['description'];
echo "</td>\n";
echo "</tr>\n";
echo "</table>\n";
?>
 
Bellow is the code in question (after it connects to the MySQL database)

$result = mysql_query("SELECT * FROM chewytable ORDER BY rating DESC");
?><table><?
while($row = mysql_fetch_array($result))

{
?>
<tr><td><?echo $row['html']?></td><td>
<table><td>
<?
$name=$row['name'];
$Grate=$row['rating'];
$Zvotes=$row['votes'];
$description=$row['description'];
$votesplusone=$votes+1;
$ARate=$GRate/$votesplusone;
$RoundRate=round($ARate,2);
?>
<b><?echo "$name"; ?></b><br/>
<b>Rating:</b> <?echo "$RoundRate"; ?><br/>
<b>Number of Ratings:</b><?echo "$Zvotes"; ?><br/>
<b>Vplusone:</b><?echo "$votesplueone"; ?><br/>

<B>Description:</b><? echo "$description"; ?>
</table>
</td></tr>
<?PHP
}
echo "</table>";

The problem is that $RoundRate comes up empty. I've been able to figure out that $GRate works fine and that $votesplusone doesn't work. It appears that PHP won't compute $ARate and $votesplueone, perhaps because doesn't think that the other variables are integers? However in the MySQL databse they are defined as integers.

Please let me know if you want more info. Help is greatly appreciated! (this is my first PHP project, so I'm still learning the ropes)
 
Back
Top