Click here to Skip to main content
15,881,561 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I have a question. I want to select all column in my table but I limit it rows rand limit 3 (order by rand limit 3) ..I already done that part. I want all column display, My problem is I have one column for price, and I only want to count the total of the price and add one display row as amount (at browser) . Which part I need to put the code for sum of the price? Below is my code.

What I have tried:

<?php

$con=mysqli_connect("localhost","root","","travel");

if(mysqli_connect_errno($con))

{

echo "Connection Error: ".mysqli_connect_error();

}


$qry="SELECT id_no, category, destination, visit_attraction, price
FROM activity_1
WHERE category = 'take it easy'
ORDER by RAND()
LIMIT 3
";

$result =mysqli_query($con,$qry);

print "
Travel: Category & Activity Data
";

echo "















";

while($rowval = mysqli_fetch_array($result))

{

echo "";

echo "";

echo "";

echo "";

echo "";

echo "";

echo "";

echo "";

}

echo "
DaysId_NoCategoryDestinationVisit_AttractionPrice
" . $rowval['id_no'] . "" . $rowval['category'] . "" . $rowval['destination'] . "" . $rowval['visit_attraction'] . "" . $rowval['price'] . "
";

mysqli_close($con);
}

?>
Posted
Updated 11-Dec-16 17:45pm

You will have to construct the table rows and sum the price in the loop programmatically, see the code snippet with comments:
// opening table tag and table headers
echo '<table><tr><th>Id_No</th><th>Category</th><th>Destination</th><th>Visit_Attraction</th><th>Price</th></tr>';

// declare variable to store the price addition in the while loop
$total_price = 0;

while($rowval = mysqli_fetch_array($result))
{
   // display a row of record
   echo '<tr><td>$rowval['id_no']</td><td>$rowval['category']</td><td>$rowval['destination']</td><td>$rowval['visit_attraction']</td><td>$rowval['price']</td><tr>'

   // add the price to the total
   $total_price += $rowval['price'];

}

// add a last row to display only the total price in the last column
echo '<tr><td></td><td></td><td></td><td></td><td>$total_price</td><tr>'

// closeing table tag
echo '</table>';

Take this as an example, barring any syntax errors.
 
Share this answer
 
v2
yes. Its work. Thank you Peter. :) Here is my code share :

<?php

$con=mysqli_connect("localhost","root","","travel");

if(mysqli_connect_errno($con))

{

echo "Connection Error: ".mysqli_connect_error();

}


$qry="SELECT id_no, category, destination, visit_attraction, price  
FROM activity_1
WHERE  category = 'take it easy'
ORDER by RAND()
LIMIT 3
";

$result =mysqli_query($con,$qry);

print "<h5>Travel: Category & Activity Data</h5>";

echo "<table border='1'>

<tr>

<th>Days</th>
<th>Id_No</th>
<th>Category</th>
<th>Destination</th>
<th>Visit_Attraction</th>
<th>Price</th>
</tr>";


// declare variable to store the price addition in the while loop
$total_price = 0;

while($rowval = mysqli_fetch_array($result))

  {

  echo "<tr>";
  
  echo "<td></td>";

  echo "<td>" . $rowval['id_no'] . "</td>";

  echo "<td>" . $rowval['category'] . "</td>";
  
    echo "<td>" . $rowval['destination'] . "</td>";

  echo "<td>" . $rowval['visit_attraction'] . "</td>";
  
  echo "<td>" . $rowval['price'] . "</td>";

  echo "</tr>";


// add the price to the total
   $total_price += $rowval['price'];

}

// add a last row to display only the total price in the last column
echo "<tr><td></td><td></td><td></td><td></td><td></td><td>$total_price</td><tr>";

// closeing table tag
echo "</table>";

mysqli_close($con);
}
 
Share this answer
 
v2
Comments
Peter Leow 12-Dec-16 1:31am    
You are supposed to mark solution 1 as the accepted solution. This should be added as a comment to solution 1. So remove it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900