954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

HELP PHP w/SQL Code HELP

Hi All
I need help and i am new to php so bare wih me :confused:
what i m tryin 2 do is that add poduct shipping handeling togther to get an avraged rate so this is the part of code Troubling me:

$nrow = mysql_num_rows($result);
if ($Vh4 == "VH4")
{
	for ($i=0;$i<$nrow;$i++);
	{
		$row = mysql_fetch_array($result);
		extract ($row);
		$Total = $Total + $VH4 + $UD4 + $Handling;
	}

}
else
{
	echo "LOL<BR><BR>";
}

$Total = $Total / $nrow; 
echo "$Total";

Problem here is that it is not adding all the rows together and then getitng the price but instead its just avraging 1 row.

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

I do not understand how it would not be looping $nrow times. Please add echo() statements after most every line to output debugging information. For example:
[PHP]
if (!$result) {
echo "
ERROR! result is false.";
}
$nrow = mysql_num_rows($result);
echo "
nrow: ".$nrow;
if ($Vh4 == "VH4")
{
echo "
Starting loop";
for ($i=0;$i<$nrow;$i++);
{
echo "i: ".$i;
$row = mysql_fetch_array($result);
if (!$row) {
echo "
ERROR! row is false.";
}

extract ($row);
echo "
VH4: ".$VH4;
echo "
UD4: ".$UD4;
echo "
Handling: ".$Handling;
$Total = $Total + $VH4 + $UD4 + $Handling;
echo "
Total: ".$Total;
}
}
else
{
echo "LOL

";
}
echo "nrow: ".$nrow;
echo "
Total: ".$Total;
$Total = $Total / $nrow;
echo "
$Total
"; [/PHP]
See if this does not show you where the problem is. Let us know.

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

Hi I tried it and it didn't work here is the results
______________________________________________
nrow: 2
Starting loop
--------------------------------------------------------------------------
i: 2
VH4: 100.00
UD4: 103.00
Handling: 25.00
Total: 228
--------------------------------------------------------------------------
nrow: 2
Total: 228
114
_______________________________________________

As you see the second part should have been reped 2 time with different figures.
so now i am comfused about why it is not looping.

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

AHA!!! Remove the semicolon at the end of this line:
[PHP]
for ($i=0;$i<$nrow;$i++);
[/PHP]

Troy
Posting Whiz
362 posts since Jun 2005
Reputation Points: 36
Solved Threads: 6
 

Thanks that helped a lot and because of this hint i was able to expand the program even more...

ashneet
Junior Poster
147 posts since Jun 2005
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You