Total members 11895 |It is currently Mon Dec 30, 2024 6:57 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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




Author:

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'");
 


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : calculate data from multiple selection
 C++ Selection Sort     -  
 Multiple Inheritance     -  
 selection sort     -  
 Help with selection sort for strings?     -  
 Using Multiple Values for a Cookie in php     -  
 Multiple Exception Catching     -  
 Jtable with multiple headings     -  
 Code to open multiple links.     -  
 Good looking selection box with image per option     -  
 selection of checkbox in buttongroup in netbeans     -  









Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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