Jump to content

sql + POST form + calculation


jp111222

Recommended Posts

Please help me. I tried to make a form from my database, and calculate the product of values which was selected.

 

<?php
$dbconn = pg_connect('host=localhost port=5432  dbname=test user=test password=test');

if(!$dbconn) {
exit('DB Connect Failed!');
}
$result = pg_query(
$dbconn,
" SELECT * FROM record " );

if(!$result){
exit('SELECT Failed!!!');
}

$rows = pg_fetch_all($result);
?>

<html><body>
<form name="cal" method="post" action="cal2.php">

<?php if(!$rows): ?>
	<div>No data was found.</div>

<?php else: ?>


<table>	 <tr>
  <th  class="ass">object name</th>
  <th  class="ass" colspan="3">price</th>
  </tr>

	 <?php foreach($rows as $r): ?>
<tr>
	  <td><?php echo $r['object_name']; ?></td>
	  </td>
	  <td>
	  <input type="radio" name="<?php echo $r['object_name'] ?>" value="<?php echo $r['price_1'] ?>">
	  <?php echo $r['object_1'] ?></td>
	  <td>
	  <input type="radio" name="<?php echo $r['object_name'] ?>" value="<?php echo $r['price_2'] ?>">
	  <?php echo $r['object_2'] ?></td>
	  <td>
	  <input type="radio" name="<?php echo $r['object_name'] ?>" value="<?php echo $r['price_3'] ?>">
	  <?php echo $r['object_3'] ?></td>
	   	 </tr>

<?php endforeach; ?>
	 </table>
<?php endif; ?>
    
	<input type="submit" name="submit" value="SUBMIT">
	         
	<input type="reset" value="CLEAR">
	         <br><br>

</form>

 

I can get values from selected object, but I don't know how to calculate their total product.

 

<?php
foreach ( $_POST as $key => $val ) {
$selval = "$val, ";
echo $selval;
}
//echo values from selected objects. ex. 12, 15, 13....

//then, product each values. ex. 12*15*13...
$p = 1;
foreach ( $_POST as $key => $val ) {
$p *= $val;
echo $p;
}
//it didn't work

foreach ( $_POST as $key => $val ) {
$a = array($val);
echo array_product($a);
}
//it didn't work

?>

Link to comment
Share on other sites

I prefer to calculate inside SQL query as much as possible. You use Postgre - it's very powerful, use it!

 

The best solution is to do all inside Postgre and then just read the final result.

 

PS. Your loop with POST values is quite incorrect!!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.