problem with arrays,loops and inserting. THANKS in advance!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jcrensha627
Forum Newbie
Posts: 15
Joined: Fri Feb 27, 2009 11:34 pm

problem with arrays,loops and inserting. THANKS in advance!!

Post by jcrensha627 »

hello everyone - i am creating a phpsql stock market simulation game.

I have no obvious error but now something is obviously wrong with what is inserted- here are my last 4 purchases, the table from phpmyadmin is below. what i am trying to do is volume, which is the simply the sum of all values in the shares column. as of now i am just gettin the sum of shares but it won take into consideration the newest amount of shares bought. when i purchase stock. here is what should be seen in the table results looking from phpmyadmin.

:idea:
id 122's volume should be 5 123's should be 15 .124's should be 25 and finally 125's volume should be the total value of all shares thus far, 35 - and so on and so forth to have a running total volume for this stock - in the end i would also like the price of the stock to change, based on amount of buys or volume. im assuming price change would be this same theory.

what my table shows as of now.... with the code below. :banghead:

id symbol shares volume price increase time
125 JAME 10 25 jimbo 500 0000-00-00 00:00:00
124 JAME 10 15 jimbo 500 0000-00-00 00:00:00
123 JAME 10 5 jimbo 500 0000-00-00 00:00:00
122 JAME 5 0 jimbo 250 0000-00-00 00:00:00
53 JAME 0 50.00 0 0000-00-00 00:00:00

i believe this is happening near my array, i may be missing something or making an obvious mistake. i have comments below. :?: :idea:

Code: Select all

<html>
<body>
Go Back to<a href="index.php">Your Portfolio</a>
<br>
<br>
Are you sure you want to buy shares in JAME? (Total Below)
<br>
<br>
 
<?php
ob_start();
include("config.php");
 
$username = $_COOKIE['loggedin'];
if (!isset($_COOKIE['loggedin'])) die("You are not logged in, <a href=../login.html>click here</a> to login.");
 
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
 
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
 
$get_my_basket = mysql_query("SELECT `symbol`, `price` FROM `JAME_PH` WHERE `symbol`='".JAME."'");
 
  $total_cost = 0;
  $JAME = 0;
  $quantity = $_POST['quantity'];
 
 
while($my_basket = mysql_fetch_array($get_my_basket))
{
  if($my_basket['symbol']=='JAME') $JAME++;
 
  $total_cost += $my_basket['price'] * $quantity;
}
 
echo 'You have selected: ';
if($JAME > 0)
{
   echo $quantity.' JAME shares, ';
}
else
{
echo 'Your Basket it Empty';
}
$total_cost = number_format($total_cost,2);
 
echo ' and it will cost you $'.$total_cost;
 
 
 
 // i received this part of the code for volume from a friend on the net.  it may be wrong somewhere, but i //haven't been able to find out where!!!!! 
 
$query = "select * from JAME_PH WHERE `symbol`='".JAME."'";
   $result = mysql_query($query);
  
 $volume = 0;
 
while($row = mysql_fetch_array($result))
{
    $volume += $row['shares'];   //this could also be where the problem is!! 
    echo 'id '.$row['id'];
    echo 'symbol '.$row['symbol'];
    echo 'shares '.$row['shares'];
    
    echo 'volume = '.$volume;
    echo 'price = '.$row['price'];
}
 
 
 
 
$insert = mysql_query("insert into trades values ('NULL', 'JAME', '".$_POST['quantity']."', '$total_cost', '$username' )")
or die("Could not insert data because ".mysql_error());
 
$insert2 = mysql_query("insert into JAME_PH values ('NULL', 'JAME', '".$_POST['quantity']."', $volume, '$username','$total_cost', '$username' )")
or die("Could not insert data because ".mysql_error());
 
ob_end_flush();
?>
 
 
<br>
<br>
Visit<a href="index.php">Your Portfolio</a> to see your purchases.
 
</body>
</html
Post Reply