Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
problem with a fetch array and math calcualtion.
Old 04-08-2009, 08:14 PM problem with a fetch array and math calcualtion.
Novice Talker

Posts: 14
Name: biklad
Trades: 0
Hey everyone im tryin to code something which may be simple but ive attempted and failed. Im creating a stock market simulation website using php and phpmyadmin. In the table JAME_PH i have this-

id symbol shares volume price
53 JAME 0 50.00

I now want to do price change, ive already gotten volume to change and work (the sum of all shares) but now i would like the price to change due to volume .01% at a time, so it would really be a .01% change for every 10 shares traded. I'm lost on how to do this calculatio in php. below i started some code think i was goin in the right direction... all help is appreciated!!!!


here is the code on my buy page as of now
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.");
 
// 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 `shares` from JAME_PH";
$result mysql_query($query);
  
 
$volume $_POST['quantity'];
 
while(
$row mysql_fetch_array($result))
{
    
$volume += (int)$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'];
}

//  I want to make a new query and new result here

$query2 "select `price` from JAME_PH WHERE `id`=53";
$result2 mysql_query($query);
  
 
$price50.00;
 
while(
$row mysql_fetch_array($result2))
{
    
//  SO LOST ON WHERE TO BEGIN. i want it so every time a user trades(buys) 10 shares the price (50.00) will increase by .01% - so when someone buys 10 shares 50.00 will change to 50.05 

  //  50.00 * .01 = 0.5 now add the .5 to 50.00 to get 50.05 and insert into the table at id 53 under price!!!. I wish i new where to begin. THANKS IN ADVANCE ANYONE!!!!!
}

 
$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',$increase, '$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

Last edited by jcrensha627; 04-08-2009 at 08:28 PM.. Reason: addition
jcrensha627 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-08-2009, 08:26 PM Re: problem with a fetch array and math calcualtion.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
So you are trying to "change" the price by .01% (.0001) for every 10 shares traded?
For an increase:
PHP Code:
$newPrice 0.00000;
$newPrice = (float)($price 1.0001); 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-08-2009, 08:32 PM Re: problem with a fetch array and math calcualtion.
Novice Talker

Posts: 14
Name: biklad
Trades: 0
thanks pointer!!

but how does this take in consideration only changing when a user buys 10 shares??? or does it?? whats fllat btw? THANKS!!

$newPrice = 0.00000;
$newPrice = (float)($price * 1.0001);


i guess im looking for an if or something?? maybe not?

if post quantity is >10 ($price * 1.0001);
jcrensha627 is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 08:38 PM Re: problem with a fetch array and math calcualtion.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You'd have to track how many shares are traded.
PHP Code:
if($sharesTraded 10 == && $sharesTraded 0)
{
     
$newPrice 0.00000//specifying precision
     
$newPrice = (float)($price 1.0001);

Float is a datatype, specifically any real number, including decimal values (to a certain point). If you were writing this code in java you would be painfully aware of datatypes. In PHP however datatypes are more or less transparent, however when dealing with floating point numbers you sometimes need to cast the right side of an equation to a float so PHP won't truncate all of the values to the right of the decimal.

% by the way is called modulus. It returns the remainder of a division. So 14 % 10 is 4. 20 % 10 is 0.

If you plan on storing that value in the database, make sure you don't do it as an INT otherwise you'll lose all of the values to the right of the decimal. Store it either as a float, or multiple the value by 100 before entering it and divide it by 100 as soon as you retrieve it (if you don't mind losing cent fractions).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 04-08-2009 at 08:47 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 04-08-2009, 08:43 PM Re: problem with a fetch array and math calcualtion.
Novice Talker

Posts: 14
Name: biklad
Trades: 0
Quote:
Originally Posted by NullPointer View Post
You'd have to track how many shares are traded.
PHP Code:
if($sharesTraded 10 == && $sharesTraded 0)
{
     
$newPrice 0.00000//specifying precision
     
$newPrice = (float)($price 1.0001);

Float is a datatype, specifically any real number, including decimal values (to a certain point). If you were writing this code in java you would be painfully aware of datatypes. In PHP however datatypes are more or less transparent, however when dealing with floating point numbers you sometimes need to cast the right side of an equation to a float so PHP won't truncate all of the values to the right of the decimal.

% by the way is called modulus. It returns the remainder of a division. So 14 % 10 is 4. 20 % 10 is 0.
wow thanks for the info pointer, def undestand this better. so is this code good to go? i think i need to insert new price into the table now correct???? and how would i keep track of shares traded? is that line if($sharesTraded % 10 == 0 && $sharesTraded > 0) doin it for me? THANKS ALOT MAN. amazing
jcrensha627 is offline
Reply With Quote
View Public Profile
 
Old 04-08-2009, 08:50 PM Re: problem with a fetch array and math calcualtion.
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
The code is mostly an example of how you would do it, not an actual implementation.

To track the number of shares traded just initialize $sharesTraded to zero and increment it each time a trade occurs.

PHP Code:
$sharesTraded 0;

//trade occurs

$sharesTraded++; 
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to problem with a fetch array and math calcualtion.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.62268 seconds with 12 queries