I've been at this for quite some time and just can't figure it out.I have a html table and have to find the total sum of the values in the \$total/Sale Cost (last) row. Can anyone help me out or point me in the right direction.

Code below:
<?php

``````print ("<table border=1><tr><th>Sale Number</th><th>Item</th><th>Item Cost</th><th>Units Sold</th><th>Sale Cost</th></tr>");

for(\$x=0; \$x<=20; \$x=\$x+1) {

/*
\$sofa_total = 0;
\$table_total = 0;
\$chair_total = 0;
\$box_total = 0;
\$tablecloth_total = 0;
*/

\$amount = rand(1,10);
\$res = rand(1, 5);
\$object = 0;
\$total = \$amount * \$cost;

if (\$res == 1) {
\$cost = 235.00;
\$item = "sofa";
}
elseif (\$res == 2) {
\$cost = 125.00;
\$item = "table";
}
elseif (\$res == 3) {
\$cost = 25.99;
\$item = "chair";
}
elseif (\$res == 4) {
\$cost = 15.25;
\$item = "box";
}
elseif (\$res == 5) {
\$cost = 23.50;
\$item = "tablecloth";
}

print ("<tr>");
print ("<td>" . \$table[\$x] = \$x+1 . "</td>");
print ("<td>" . \$table[\$x] = \$item . "</td>");
print ("<td>" . \$table[\$x] = \$cost . "</td>");
print ("<td>" . \$table[\$x] = \$amount . "</td>");
print ("<td>" . \$table[\$x] = \$total  . "</td>");
}
print ("</tr>");

if (\$object==0) {

\$sofa_total = \$sofa_total + \$amount;

} elseif (\$object==1) {

\$table_total = \$table_total + \$amount;

} elseif (\$object==2) {

\$chair_total = \$chair_total + \$amount;

} elseif (\$object==3) {

\$box_total = \$box_total + \$amount;

} elseif (\$object==4) {

\$tablecloth_total = \$tablecloth_total + \$amount;

}

print ("</table>");

?>
``````

Can you please normalize your question am not actually make up my mind I mean what actually you want to do with this code??!

## All 2 Replies

Can you please normalize your question am not actually make up my mind I mean what actually you want to do with this code??!

@kevinp2012 seriously, are you asking the OP to improve his English?
@dodgers125 I'm not sure what you want but maybe I can give some pointers about your code:

``````<!-- header row, the 1 must be in quotes: border="1"-->
<table border="1">
<tr><th>Sale Number</th>
<th>Item</th>
<th>Item Cost</th>
<th>Units Sold</th>
<th>Sale Cost</th>
</tr>
<?php
/* Initialise the \$totalsArray where we will store the total sales amount for each item */
\$totalsArray = array('sofa'=>0, 'table'=>0, 'chair'=>0, 'box'=>0, 'tablecloth'=>0);
for (\$x=0; \$x<=20; \$x++){
\$amount = rand(1,10);
\$res = rand(1, 5);
/* The following line makes the later condition "if (\$object==0)" always true.
Seems a bit pointless to me, but maybe it's only for testing? */
\$object = 0;
/* \$cost doesn't exist in the script yet, so this next line must come under the switch */
//\$total = \$amount * \$cost;
/* I prefer a switch than elseif's for this job */
switch(\$res){
case 1:
\$cost = 235.00;
\$item = "sofa";
\$totalsArray['sofa'] += (\$cost*\$amount);
break;
/////
case 2:
\$cost = 125.00;
\$item = "table";
\$totalsArray['table'] += (\$cost*\$amount);
break;
/////
case 3:
\$cost = 25.99;
\$item = "chair";
\$totalsArray['chair'] += (\$cost*\$amount);
break;
/////
case 4:
\$cost = 15.25;
\$item = "box";
\$totalsArray['box'] += (\$cost*\$amount);
break;
/////
case 5:
\$cost = 23.50;
\$item = "tablecloth";
\$totalsArray['tablecloth'] += (\$cost*\$amount);
break;
}//switch
//set the total now that \$cost is defined...
\$total = \$amount * \$cost;
//write the table and store vars in \$table array
echo "<tr>";
echo "<td>" . \$table[\$x] = \$x+1 . "</td>\n";
echo "<td>" . \$table[\$x] = \$item . "</td>\n";
echo "<td>" . \$table[\$x] = \$cost . "</td>\n";
echo "<td>" . \$table[\$x] = \$amount . "</td>\n";
echo "<td>" . \$table[\$x] = \$total . "</td>\n";
echo "</tr>\n";
}//for loop
?>
<!-- Don't forget to close the table, in HTML just for tidyness-->
</table>

<?php
/* for demonstration purposes, show the totalsArray */
/* use a foreach loop to display totals however you want */
echo "Totals array: <br />\n";
foreach(\$totalsArray as \$k => \$v){
echo "Amount of total \$k's sold is \$v\n<br />";
}
?>
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.