suprsnipes Posted June 8, 2009 Share Posted June 8, 2009 I'm really struggling and need some help with PHP code. I have created a connection to the database and can run various queries. As you can see from the basic code below I can connect and display the result of a query but I would like to run an if/else statement on the data prior to echoing (the layout would be the same) and would like to know how to go about this, perhaps an example of some kind would help. <?php $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("day", $con); $result = mysql_query("SELECT * FROM test INNER JOIN trades ON test.id1=trades.id-1"); echo "<table border='1'> <tr> <th>ID1</th> <th>SYMBOL1</th> <th>DATE1</th> <th>TIME1</th> <th>TYPE1</th> <th>PRICE1</th> <th>SIZE1</th> <th>ID</th> <th>SYMBOL</th> <th>DATE</th> <th>TIME</th> <th>TYPE</th> <th>PRICE</th> <th>SIZE</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['ID1'] . "</td>"; echo "<td>" . $row['SYMBOL1'] . "</td>"; echo "<td>" . $row['DATE1'] . "</td>"; echo "<td>" . $row['TIME1'] . "</td>"; echo "<td>" . $row['TYPE1'] . "</td>"; echo "<td>" . $row['PRICE1'] . "</td>"; echo "<td>" . $row['SIZE1'] . "</td>"; echo "<td>" . $row['ID'] . "</td>"; echo "<td>" . $row['SYMBOL'] . "</td>"; echo "<td>" . $row['DATE'] . "</td>"; echo "<td>" . $row['TIME'] . "</td>"; echo "<td>" . $row['TYPE'] . "</td>"; echo "<td>" . $row['PRICE'] . "</td>"; echo "<td>" . $row['SIZE'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Regards, suprsnipes Quote Link to comment https://forums.phpfreaks.com/topic/161355-help-with-php-code/ Share on other sites More sharing options...
trq Posted June 8, 2009 Share Posted June 8, 2009 A description of what you want to do exactly would help. Ifs (like everything else) are covered in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/161355-help-with-php-code/#findComment-851474 Share on other sites More sharing options...
suprsnipes Posted June 8, 2009 Author Share Posted June 8, 2009 This is what I want to do. This code should be ok, a friend has helped me with it, I've just updated it now best I could for the moment. Should make things a little clearer. action == "buy" or "sell", not sure what to do with this. At this stage I don't know if I should use the below if/else statement to UPDATE the type1 column in the database or if there is another option as I have read that it's not ideal to put calculated values into a database... <?php // Classification of trades if (type == "N") { if (type1 != null) { if (type1 == "buy") { if (price >= price1) { action == "buy"; } else { action == "sell"; } } else if (type1 == "sell") { if (price <= price1) { action == "sell"; } else { action == "buy"; } } } else { if (type1 == "a") { if (price >= price1) { action == "buy"; } else { action == "sell"; } } if (type1 == "b") { if (price > price1) { action == "buy"; } else { action == "sell"; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161355-help-with-php-code/#findComment-851480 Share on other sites More sharing options...
gaza165 Posted June 8, 2009 Share Posted June 8, 2009 still not 100% clear on what you are trying to do...explain more please Quote Link to comment https://forums.phpfreaks.com/topic/161355-help-with-php-code/#findComment-851501 Share on other sites More sharing options...
suprsnipes Posted June 8, 2009 Author Share Posted June 8, 2009 The query joins the tables with the intention of referring to the previous ID's data using an IF statement. Using the IF statement I want to classify the trades (N) as a buy or a sell, but I'm not sure where I place this in my PHP code...I'm also uncertain as to whether or not I update the data in the database or I do this in some other manner as I have read that you should not put calculated values into a database... Quote Link to comment https://forums.phpfreaks.com/topic/161355-help-with-php-code/#findComment-851920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.