Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate output 1

Status
Not open for further replies.

ttuser4

MIS
Jun 19, 2008
147
CA
hi,
i have a simple php code; it displays selected data from mysql database.

code:

<?php

$span=$_POST['span'];
$blength=$_POST['length'];

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db1", $connection);

$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
qbay WHERE bspan='$span'", $connection);

print "Model: Length: Span: Load: Price:<br>";

while ($row = mysql_fetch_array($result, MYSQL_NUM))

{
foreach ($row as $attribute)
print "{$attribute} ";
print "\n";
}

mysql_close($connection);

?>

output:

Model: Length: Span: Load: Price:
SDFS 7 35 20 9070.38600
SVFS 7 35 23 11251.26400
TDFS 7 35 0 12260.12502
TVFS 7 35 49 15182.74888


i need to do some calculations and attach them to output; so i modified my php code:

<?php

$span=$_POST['span'];
$plength=$_POST['length'];
$numbays=intval($plength/7);

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db1", $connection);

$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
qbay WHERE bspan='$span'", $connection);

print "Model: Length: Span: Load: Price:<br>";

while ($row = mysql_fetch_array($result, MYSQL_NUM))

{
$bmodel=$row['bmodel'];
$blentgh=$row['blength'];
$bspan=$row['bspan'];
$bsnowload=$row['bsnowload'];
$bprice=$row['bprice'];
$minlength=$numbays*$blength;
$minprice=$numbays*$bprice;
$maxlength=($numbays+1)*$blength;
$maxprice=($numbays+1)*$bprice;

print "$bmodel $blength $bspan $bsnowload $bprice $minlength $minprice $maxlength $maxprice";
}

mysql_close($connection);

?>

but for the same input data i got output like this:

Model: Length: Span: Load: Price:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

can you please help me fix this code? i am new to php and i try to make very simple web version of existing windows vb6 application. thanks.
 
Hi

You get the fields with [tt]MYSQL_NUM[/tt], so no field names will be passed. So later you can not retrieve the field values by field name. Ask for [tt]MYSQL_ASSOC[/tt] or [tt]MYSQL_BOTH[/tt] instead :
Code:
while ($row = mysql_fetch_array($result, [red]MYSQL_ASSOC[/red]))

Feherke.
 
I can't see anything wrong with your code other than if your $_POST['length'] is less than 7 when you divide by 7 and then intval the result it will be 0.

So everything else that uses that value will also be 0.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Ahh yes missed that part. the MYSQL_NUM constant will make mysql_fetch_array return a numbered array instead of having the field names as indexes like Feherke said.

However I'm sure you should be getting at the very least a notice the way you are ding it.

Notice undefined Index ...

Still my first observation still stands.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks for you help.
i tried both MYSQL_ASSOC and MYSQL_BOTH
...
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysql_fetch_array($result, MYSQL_BOTH))
...
but the output is still zeros
Model: Length: Span: Load: Price:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

yes, $_POST['length'] should be more than 7 (it's total length of enclosure, 7 is length of one bay).

 
i renamed one name from "length" to "tlength' just in case in a reserved word

so my html code is:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>ZIP Code</title>
</head>

<body>

<form action="calc.php" method="post">
Length [ft]: <input type="text" name="tlength"><br>
Span [ft]: <input type="text" name="span"> Enter number between 8 and 52
<p><input type="Submit">
<br>
<br>
</p>
</form>

</body>

</html>


php code:
<?php

$span=$_POST['span'];
$plength=$_POST['tlength'];
$numbays=intval($plength/7);

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db1", $connection);

$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
qbay WHERE bspan='$span'", $connection);

print "Model: Length: Span: Load: Price:<br>";

//while ($row = mysql_fetch_array($result, MYSQL_NUM))
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
while ($row = mysql_fetch_array($result, MYSQL_BOTH))

{
$bmodel=$row['bmodel'];
$blentgh=$row['blength'];
$bspan=$row['bspan'];
$bsnowload=$row['bsnowload'];
$bprice=$row['bprice'];
$minlength=$numbays*$blength;
$minprice=$numbays*$bprice;
$maxlength=($numbays+1)*$blength;
$maxprice=($numbays+1)*$bprice;

print "$bmodel $blength $bspan $bsnowload $bprice $minlength $minprice $maxlength $maxprice";
}

mysql_close($connection);

?>

output still

Model: Length: Span: Load: Price:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
ok, the problem is line $plength=$_POST['tlength'];

code:
$span=$_POST['span'];
$plength=$_POST['tlength'];
$numbays=intval($plength/7);

echo "$span <br>";
echo "$plength <br>";
echo "$numbays <br>";

input:
tlength=50
span=35

output:
35

0

but what exactly is wrong with that line?
 
ok, i got working that part but output still not correct

php code:

<?php

$span=$_POST['span'];
$plength=$_POST['tlength'];
$numbays=intval($plength/7);

echo "Span: $span<br>";
echo "Total Lenght: $plength<br>";
echo "Number of Bays: $numbays<br>";
echo "<br>";

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db1", $connection);

$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
qbay WHERE bspan='$span'", $connection);

print "Model: Length: Span: Load: Price: Min.length: Min.price: Max.length: Max.price:<br>";

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
$bmodel=$row['bmodel'];
$blentgh=$row['blength'];
$bspan=$row['bspan'];
$bsnowload=$row['bsnowload'];
$bprice=$row['bprice'];
$minlength=$numbays*$blength;
$minprice=$numbays*$bprice;
$maxlength=($numbays+1)*$blength;
$maxprice=($numbays+1)*$bprice;

print "$bmodel, $blength, $bspan, $bsnowload, $bprice, $minlength, $minprice, $maxlength, $maxprice <br>";
}

mysql_close($connection);

?>


output:

Span: 35
Total Lenght: 66
Number of Bays: 9

Model: Length: Span: Load: Price: Min.length: Min.price: Max.length: Max.price:
, , , , , 0, 0, 0, 0
, , , , , 0, 0, 0, 0
, , , , , 0, 0, 0, 0
, , , , , 0, 0, 0, 0
 
First thing to do would be to make sure you are getting rows returned from your query, so output your query to screen, and run it directly on your DB just to make sure its correct.

You can also check if your getting any rows back by using mysql_num_rows.

Code:
$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
                          qbay WHERE bspan='$span'", $connection) [red]or die(mysql_error())[/red];
[red]
if(mysql_num_rows($result)>0){
echo "Rows have been returned";
}
else{
echo "No rows have been returned";
}
[/red]



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thank you.
i got it working by using the other method.

code:
<?php

$span=$_POST['span'];
$plength=$_POST['tlength'];
$numbays=intval($plength/7);

echo "Span: $span<br>";
echo "Total Lenght: $plength<br>";
echo "Number of Bays: $numbays<br>";
echo "<br>";

$connection = mysql_connect("localhost","root","jp");

mysql_select_db("db1", $connection);

$result = mysql_query ("SELECT bmodel, blength, bspan, bsnowload, bprice FROM
qbay WHERE bspan='$span'", $connection) or die(mysql_error());

$num=mysql_numrows($result);

if(mysql_num_rows($result)>0){
echo "$num rows have been returned<br>";
}
else{
echo "No rows have been returned<br>";
}

echo "<br>";
echo "Model: Length: Span: Load: Price: Min.length: Min.price: Max.length: Max.price:<br>";

$i=0;
while ($i < $num) {

$bmodel=mysql_result($result,$i,"bmodel");
$blength=mysql_result($result,$i,"blength");
$bspan=mysql_result($result,$i,"bspan");
$bsnowload=mysql_result($result,$i,"bsnowload");
$bprice=number_format(mysql_result($result,$i,"bprice"),2);
$minlength=$numbays*$blength;
$minprice=number_format($numbays*$bprice,2);
$maxlength=($numbays+1)*$blength;
$maxprice=number_format(($numbays+1)*$bprice,2);

echo "$bmodel $blength $bspan $bsnowload $bprice $minlength $minprice $maxlength $maxprice <br>";

$i++;
}

mysql_close($connection);

?>

output:
Span: 35
Total Lenght: 66
Number of Bays: 9

4 rows have been returned

Model: Length: Span: Load: Price: Min.length: Min.price: Max.length: Max.price:
SDFS 7 35 20 9,070.39 63 81.00 70 90.00
SVFS 7 35 23 11,251.26 63 99.00 70 110.00
TDFS 7 35 0 12,260.13 63 108.00 70 120.00
TVFS 7 35 49 15,182.75 63 135.00 70 150.00
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top