Wed Jul 04, 2012 5:08 pm
i'm using php,,
want to calculate data from multiple selection,,
if(isset($_POST['food_name'])){
$namesArray = $_POST['food_name'];
$carb = mysql_query("SELECT SUM(food_carbohydrate) FROM food WHERE food_name='$namesArray'");
print $carb;
$i=0;
foreach($namesArray as $key => $value){
$i++;
$result = mysql_query("SELECT * FROM food where food_name='$value'");
$cal = mysql_query("SELECT food_calories FROM food where food_name='$value'");
$fat = mysql_query("SELECT food_fat FROM food where food_name='$value'");
$prot = mysql_query("SELECT food_protein FROM food where food_name='$value'");
echo "<table border='1'>
<tr>
<td width='100'></td>
<td width='100'></td>
<td width='100'></td>
<td width='100'></td>
<td width='100'></td>
<td width='100'></td>
</tr>";
while($row = mysql_fetch_array($result))
{echo "<tr>";
echo "<td bgcolor='#66CCFF'>" . $row['food_name'] . "</td>";
echo "<td bgcolor='#66CCFF'>" . $row['food_portion'] . "</td>";
echo "<td bgcolor='#FFFF99'>" . $row['food_carbohydrate'] . "</td>";
echo "<td bgcolor='#FFFF99'>" . $row['food_calories'] . "</td>";
echo "<td bgcolor='#FFFF99'>" . $row['food_fat'] . "</td>";
echo "<td bgcolor='#FFFF99'>" . $row['food_protein'] . "</td>";
echo "</tr>";
}
}
while($row2 = mysql_fetch_array($cal)) {
echo " <tr>";
print $row2;
echo " <td colspan='2' align='center' bgcolor='#339933'>Total</td>";
echo "<td bgcolor='#FF9900'>". array_sum($row2) ."</td>";
}
while($row4 = mysql_fetch_array($fat)) {
echo "<td bgcolor='#FF9900'>". array_sum($row4) ."</td>";
}
while($row5 = mysql_fetch_array($prot)) {
echo "<td bgcolor='#FF9900'>". array_sum($row5) ."</td>";
echo "</tr>";
}
echo "</table>";
echo $carb;
}
----------------------------------------
after i run this program
Resource id #4
and the array is empty..
Fri Jul 06, 2012 4:24 pm
I think you first have to be sure that there a data returns from the database by getting the number of rows in query results for instance:
- Code:
$results= mysql_query("SELECT * FROM student");
$number=mysql_num_rows($results);
You can apply that on
- Code:
$result = mysql_query("SELECT * FROM food where food_name='$value'");
$cal = mysql_query("SELECT food_calories FROM food where food_name='$value'");
$fat = mysql_query("SELECT food_fat FROM food where food_name='$value'");
$prot = mysql_query("SELECT food_protein FROM food where food_name='$value'");
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.