help with arrays and inserting. thanks in adance!!!
03-15-2009, 02:15 PM
|
help with arrays and inserting. thanks in adance!!!
|
Posts: 14
Name: biklad
|
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>
|
|
|
|
03-16-2009, 07:33 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
|
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
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']; }
i hope this helps u
Last edited by dark_lord; 03-16-2009 at 07:38 AM..
|
|
|
|
03-16-2009, 11:56 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 14
Name: biklad
|
I used your suggestions and STILL have 0 being inserted into volume.
I also used what you supplied me with below, did you mean i should change assoc to array? either way i did both and still have the same problem
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'];
|
|
|
|
03-16-2009, 01:44 PM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
I didn't read your entire post, *sorry* :P
But at least I spotted an error in the php code above. The $result variable is being replaced in the loop, it should be like this.
PHP Code:
while($row = mysql_fetch_assoc($result)) { $volume += $row['shares']; echo 'id '.$row['id']; echo 'symbol '.$row['symbol']; echo 'shares '.$row['shares']; // add to the volume and print echo 'volume = '.$volume; echo 'price = '.$row['price']; }
In the future, when posting code, please put it into code tags to simplify reading.
[ php] ... [ /php] (without the spaces).
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
03-18-2009, 05:38 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
|
Quote:
Originally Posted by jcrensha627
I used your suggestions and STILL have 0 being inserted into volume.
I also used what you supplied me with below, did you mean i should change assoc to array? either way i did both and still have the same problem
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'];
|
you should print $volume and check yourself after the above loop. If u still see 0 then you have a problem with the loop and hence the $row[share] field. then u should check the value of $row[share], check if it is not zero. This is how you can get the problem.
if u can give the sql table i can check for you
|
|
|
|
03-18-2009, 11:18 AM
|
help with arrays and inserting. thanks in advance!!!
|
Posts: 14
Name: biklad
|
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.
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.
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.
PHP Code:
<?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(); ?>
|
|
|
|
03-18-2009, 12:28 PM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
By your first comment, the sql query is wrong, unless you have defined a constant named JAME. It should most likely be a variable as in $JAME.
By your second comment, if the 'shares' column is of type integer I'm pretty sure that should work. But you could make it an integer yourself just to be sure, as in
PHP Code:
$volume += (int)$row['shares'];
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
03-18-2009, 02:42 PM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 14
Name: biklad
|
thanks liz ill check this out now- 
|
|
|
|
03-18-2009, 02:50 PM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 14
Name: biklad
|
i believe i did define jame in the code, i made it = 0 - do you mean i have to make another JAME variable for the query to work??
also my shares were set to int(5) and i did add the int below in the code and still get the same issue. it just wont take into consideration the newest purchase.. just now i bought 1 share, which should bring the total volume to 36, but it is still at 35 - how can i make it add in the entry that was just made.
Quote:
Originally Posted by lizciz
By your first comment, the sql query is wrong, unless you have defined a constant named JAME. It should most likely be a variable as in $JAME.
By your second comment, if the 'shares' column is of type integer I'm pretty sure that should work. But you could make it an integer yourself just to be sure, as in
PHP Code:
$volume += (int)$row['shares'];
|
|
|
|
|
03-18-2009, 04:32 PM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
|
This line
$query = "select * from JAME_PH WHERE `symbol`='".JAME."'";
---------------------------------------------------^^^
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
|
|
|
|
03-19-2009, 09:24 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 84
|
I would suggest you create a variable like
$mysymbol = "JAME";
and try
$query = "select * from JAME_PH WHERE `symbol`=".$mysymbol ;
__________________
Please login or register to view this content. Registration is FREE - Please login or register to view this content. Registration is FREE - 1-888-869-HOST(4678)
Award winning Managed Hosting - Please login or register to view this content. Registration is FREE
Managed Dedicated Servers. Reseller Discounts. 24/7 Impressive Tech Support
|
|
|
|
03-20-2009, 01:06 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 41
Name: Parijat Roy
Location: INDIA-KOLKATA
|
@jcrensha627 silly mistake
$query = "select * from JAME_PH WHERE `symbol`='JAME'";
don't use periods there. periods are for displaying variables. When u use constant put that only in single quotes.
or u can always go with HivelocityDD
|
|
|
|
03-20-2009, 02:00 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
Quote:
Originally Posted by jcrensha627
i believe i did define jame in the code, i made it = 0 - do you mean i have to make another JAME variable for the query to work??
|
What I meant was, you defined the variable $JAME (starting with $) and set it to zero. But in your query you used a constant (without the $) JAME. Unless you have defined that constant, as in
PHP Code:
define('JAME', 'some value');
it doesn't exists, and the query is wrong. Instead you should use the variable $JAME.
PHP Code:
// this $query = "select * from JAME_PH WHERE `symbol`='".JAME."'";
// should be $query = "select * from JAME_PH WHERE `symbol`='".$JAME."'";
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
03-21-2009, 01:58 AM
|
Re: help with arrays and inserting. thanks in adance!!!
|
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
|
If you want a real stock market simulation game - Just cause the server to crash! 
__________________
<mgraphic /> - I don't have a solution but I admire the problem.
|
|
|
|
|
« Reply to help with arrays and inserting. thanks in adance!!!
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|