Page 1 of 1

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

Posted: Sat Mar 14, 2009 12:33 pm
by jcrensha627
hello everyone - :D
here is my situation i am making a sort of stock market simulation phpsql site. this current page and code below is my purchasing page. I am having trouble coming up with volume, below i tried using :D some code from a friend using loops and rows the // comment is below, after getting volume (the sum of amount of shares) i would like to insert $volume in a table (JAME_PH) i have already created. as of now i have my table (JAME_PH) looks like this:

id symbol shares volume price increase time
53 JAME 0 50.00

i am using the `price` and `symbol` in the table above to get $total_cost. THANKS in advance to any replies, my code and error is below.

- Warning: Invalid argument supplied for foreach() in /mounted-storage/home59c/sub007/sc37816-TACW/www/members/conflebronjames.php on line <b>52

here is my code for the page below

Code: Select all

 
<?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; 
 
 
//code i received from someone on the net to come up with volume.
 
$query = "select * from JAME_PH where symbol = 'JAME' ";
$result = mysql_query($query); 
 
 
$volume = 0;
 
foreach($result as $key => $row)
{
    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());
 
$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();
?>

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

Posted: Sun Mar 15, 2009 3:40 am
by JasonDFR

Code: Select all

$result = mysql_query($query);
A mysql_query result is not like an array you would commonly think of.

That is why foreach($result as $key => $row) is throwing the invalid argument error. Foreach expects an array, but you are supplying a mysql_query result resource.

Do this:

Replace

Code: Select all

foreach($result as $key => $row)...
with:

Code: Select all

while ( $row = mysql_fetch_array($result) ) {
    echo 'id '.$row['id'];
    // ...
}
EDIT: Sorry, I might not have been clear enough the first time.

Good Luck,

J

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

Posted: Sun Mar 15, 2009 8:50 am
by jcrensha627
Hey Jason thanks for the REPLY!!! I used your suggestions and came up with more errors! of course :lol: here is my code below, with your suggestions and the corresponding errors. Again im attempting to making the values in volume be the sum of all shares. as of now its just giving me 0 for volume under each id. here is what my table looks like:

id symbol shares volume price increase
53 JAME 0 50.00 0

im hoping this code array coding will make the table look like this. after purchasing shares i want volume to change according to the sum of shares.

id symbol shares volume price increase
53 JAME 0 50.00
54 JAME 12 12 50.00
55 JAME 20 22 50.00

HERE IS THE ERROR----
You have selected: 20 JAME shares, and it will cost you $1,000.00
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /mounted-storage/home59c/sub007/sc37816-TACW/www/members/conflebronjames.php on line 56

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /mounted-storage/home59c/sub007/sc37816-TACW/www/members/conflebronjames.php on line 63

Warning: Invalid argument supplied for foreach() in /mounted-storage/home59c/sub007/sc37816-TACW/www/members/conflebronjames.php on line 64


AND THE CODE-----

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; 
 
 
 
 
$query = "select * from JAME_PH where symbol = 'JAME' ";
   while ( $row = mysql_fetch_array($result) ) {
   echo 'id '.$row['id'];
  
    }
 
$volume = 0;
 
$result = mysql_fetch_assoc($result);  
foreach($result as $key => $row)
{
    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());
 
$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>

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

Posted: Sun Mar 15, 2009 9:03 am
by JasonDFR

Code: Select all

$query = "select * from JAME_PH where symbol = 'JAME' ";
   while ( $row = mysql_fetch_array($result) ) {
   echo 'id '.$row['id'];
 
    }
You've got to get the $result from the function mysql_query() !!! :)

Code: Select all

$query = "select * from JAME_PH where symbol = 'JAME' ";
   $result = mysql_query($query); // This line executes your query
   while ( $row = mysql_fetch_array($result) ) {
   echo 'id '.$row['id'];
 
    }
Related, but not necessary to accomplish what you are doing, you should use mysqli http://www.php.net/manual/en/book.mysqli.php

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

Posted: Sun Mar 15, 2009 9:15 am
by jcrensha627
jason - thanks for the php link im gonna check that manual out

I added that mess up and STILL got another error here it is:

You have selected: 10 JAME shares, and it will cost you $500.00id 90id 53
Warning: Invalid argument supplied for foreach() in /mounted-storage/home59c/sub007/sc37816-TACW/www/members/conflebronjames.php on line 65

and my code-- thanks for looking at this jason, i appreciate it :!:

ive looked this over and nothing stood out to me, am i missing a } or ; somewhere??? :?:

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; 
 
 
 
 
$query = "select * from JAME_PH where symbol = 'JAME' ";
   $result = mysql_query($query); 
   while ( $row = mysql_fetch_array($result) ) {
   echo 'id '.$row['id'];
  
    }
 
$volume = 0;
 
$result = mysql_fetch_assoc($result);  
foreach($result as $key => $row)
{
    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());
 
$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>

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

Posted: Sun Mar 15, 2009 9:22 am
by JasonDFR

Code: Select all

// Here is your problem
$result = mysql_fetch_assoc($result);  
foreach($result as $key => $row)
{
Come on jcrensha627 !!

This is exactly the same as the first problem we solved. Check the first post I made.

If you still have errors, post em up.

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

Posted: Sun Mar 15, 2009 9:46 am
by jcrensha627
sorry jason im trying, i guess im just puttin things in the wrong area, ill make sure to debug more before i come back with questions. thanks either way! you've been a real help :P


im assuming I take this out?? am i wrong???

$result = mysql_query($query);

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

Posted: Sun Mar 15, 2009 10:07 am
by JasonDFR
jcrensha627 wrote:sorry jason im trying, i guess im just puttin things in the wrong area, ill make sure to debug more before i come back with questions. thanks either way! you've been a real help :P

$result = mysql_query($query);
Don't apoligize man. It just takes some practice.

Here is the basic process to follow when doing a MySQL query.

Code: Select all

<?php
 
// First build the query
 
$query = "SELECT * FROM table";
 
// Second, query the database
 
$result = mysql_query($query);
 
// Third, use the results
 
while ( $row = mysql_fetch_array($result) ) { // while results rows exists
    
    echo $row['column_name']; // echo them out
}
The first two steps are usually the same every time. The third is where you use the information stored in your database.

Forget about using foreach to loop through the results from your db query. Foreach is used to loop through arrays.

Keep posting your errors and we'll get it figured out.

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

Posted: Sun Mar 15, 2009 11:19 am
by jcrensha627
hey hey im backkkkk - no errors but now something is obviously wrong, i took out the foreach and think i understand the CORRECT query setup, thanks :banghead: - here are my last 3 purchases, the table is below.

id symbol shares volume price increase time
107 JAME 10 0 jimbo 500 0000-00-00 00:00:00
106 JAME 10 0 jimbo 500 0000-00-00 00:00:00
105 JAME 10 0 jimbo 500 0000-00-00 00:00:00
53 JAME 0 50.00 0 0000-00-00 00:00:00

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? you will receive major props when i complete this website. thanks in advance :D

here is what should be seen
id 105's volume should be 10 106's should be 20 and 107's should be 30.


- my code--------

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; 
 
 
 
 
$query = "select * from JAME_PH WHERE `symbol`='".JAME."'";
   $result = mysql_query($query); 
   while ( $row = mysql_fetch_array($result) ) {
echo $row['volume']; // echo them out
}
 
 
// should i be using this below to get volume?? i deleted the foreach and assumed this way fine
//i thought this below made sense when trying to get the sum of all shares.
$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());
 
$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>