jp111222 Posted December 22, 2011 Share Posted December 22, 2011 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/253646-sql-post-form-calculation/ Share on other sites More sharing options...
scootstah Posted December 22, 2011 Share Posted December 22, 2011 $p = 1; foreach ( $_POST as $key => $val ) { $p = $p * $val; echo $p; } Quote Link to comment https://forums.phpfreaks.com/topic/253646-sql-post-form-calculation/#findComment-1300394 Share on other sites More sharing options...
jp111222 Posted December 22, 2011 Author Share Posted December 22, 2011 Thank you for your help! Now I get result like this; 12, 180, 2340,,, 0 ( there is always "0" at last, because of "SUBMIT" value) But I don't know how to show final result only. Please help me... Quote Link to comment https://forums.phpfreaks.com/topic/253646-sql-post-form-calculation/#findComment-1300398 Share on other sites More sharing options...
SergeiSS Posted December 22, 2011 Share Posted December 22, 2011 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/253646-sql-post-form-calculation/#findComment-1300399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.