Forum Moderators: coopster

Message Too Old, No Replies

adding, multiplying information in a table

adding, multiplying information in a table

         

Imy_S3

4:30 pm on Mar 20, 2004 (gmt 0)

10+ Year Member



Hi

Got some code below i want.

This is how it works:

user enters information into a form.
i then want to work out some values depending on what user has entered.

i want it so that for each individual row the price of adult, price of child, price of oap, price of student are added together and then diplayed in the total column.

Hope you can help

Thanks in advance

$row = pg_fetch_array ($result1);

printf ("<tr> <td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>

<td>%s</td>

</tr>", $myrow['id'], $myrow['surname'], $myrow['forename'], $myrow['email'], $myrow['adultprices'], $myrow['childprices'], $myrow['studentprices'], $myrow['oapprices'], $myrow['card'], $myrow['expdatemonth'], $myrow['expdateyear'], $myrow['adultprices']+$myrow['childprices']+$myrow['studentprices']);

print "<tr><td>" . $row["id"];
print "<td>" . $row["surname"];
print "<td>" . $row["forename"];
print "<td>" . $row["email"];
print "<td>" . $row["adultprices"];
print "<td>" . $row["childprices"];
print "<td>" . $row["studentprices"];
print "<td>" . $row["oapprices"];
print "<td>" . $row["card"];
print "<td>" . $row["expdatemonth"];
print "<td>" . $row["expdateyear"];
print "<td>" . $row['adultprices']+$row['childprices']+$row['studentprices'];

coopster

9:17 pm on Mar 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I'm assuming you are asking why the printf [php.net] works as you expect, but the second section of code does not, correct? It is because of this line:
print "<td>" . $row['adultprices']+$row['childprices']+$row['studentprices'];

What I believe is happening here, is that the parser thinks you are trying to concatenate
<td>
with
$row['adultprices']
first. Then, it is trying to union that value with
$row['childprices']
, and finally union that value with
$row['studentprices']
. My guess is you are getting one less cell in your table, right? That's because the union [php.net] is not going to find another <td> in the right-hand values. You want to add the values together, then concatenate the total to your <td>, therefore you are going to have to force the operator precedence [php.net]:
print "<td>" . ($row['adultprices'] + $row['childprices'] + $row['studentprices']);