Technology, Web & Business Forum

You are here: Technology, Web & Business Forum : Web Development : Programming : help with arrays and inserting. thanks in adance!!!


Welcome to the Technology, Web & Business Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.


Programming Discuss programming languages such as .NET, PHP, PERL, ASP, ColdFusion, C++


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-16-2009
Junior Member
 
Join Date: Jan 2009
Posts: 4
Default help with arrays and inserting. thanks in adance!!!

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 3 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 a "0" when i purchase stock. here is what should be seen in the table results looking from phpmyadmin.


id 105's volume should be 10 106's should be 20 and 107's should be 30 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 amount of buys or volume. im assuming price change would be this same theory.

id symbol shares volume price increase
107 JAME 10 0 jimbo 500
106 JAME 10 0 jimbo 500
105 JAME 10 0 jimbo 500
53 JAME 0 50.00 0

i believe this is happening because of $volume, but i was assuming it would take its end result
using this -----echo 'volume = '.$volume = $volume + $row['shares'] ; although could my problem also be how i am inserting into JAME_PH?? right now im trying to insert $volume. i think this is where im going wrong, did i leave anything in or out? thanks in advance



here is my code------------

PHP Code:
<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.");
 

$link mysql_connect($server$db_user$db_pass
or die (
"Could not connect to mysql because ".mysql_error()); 


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


// my attempt at volume is below, from a friend on net.

$query "select * from JAME_PH WHERE `symbol`='".JAME."'";
   
$result mysql_query($query); 
   while ( 
$row mysql_fetch_array($result) ) {
echo 
$row['volume']; // echo them out
}



$volume 0;

$result mysql_fetch_assoc($result);  
{
    echo 
'id '.$row['id'];
    echo 
'symbol '.$row['symbol'];
    echo 
'shares '.$row['shares'];
    
// add to the volume and print
    
echo 'volume = '.$volume $volume $row['shares'] ;
    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());

//where i insert volume into table JAME_PH
$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>
Reply With Quote
  #2 (permalink)  
Old 03-16-2009
Junior Member
 
Join Date: Mar 2009
Posts: 1
Default

you didn't give single quotes around volume while inserting to table, but i guess that's ok. What i think is you should see

PHP Code:
$row['shares'
if it's at all fetching anything?! so is the error
you forget the while loop

PHP Code:
$result mysql_fetch_assoc($result);  
{
    echo 
'id '.$row['id'];
    echo 
'symbol '.$row['symbol'];
    echo 
'shares '.$row['shares'];
    
// add to the volume and print
    
echo 'volume = '.$volume $volume $row['shares'] ;
    echo 
'price = '.$row['price'];

should be changed to

PHP Code:
while($result mysql_fetch_assoc($result)) //it's better to use mysql_fetch_array
{
    echo 
'id '.$row['id'];
    echo 
'symbol '.$row['symbol'];
    echo 
'shares '.$row['shares'];
    
// add to the volume and print
    
echo 'volume = '.$volume $volume $row['shares'] ;
    echo 
'price = '.$row['price'];

Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -5. The time now is 06:58 PM.