suprsnipes Posted June 9, 2009 Share Posted June 9, 2009 Could someone be so kind as to help me get this bit of code working? My PHP experience is only a couple of weeks old. <?php // Connect to MySQL $con = mysql_connect("","",""); if (!$con) { die('Could not connect: ' . mysql_error()); } // Select database mysql_select_db("spi", $con); // query 1 $sql="SELECT * FROM trades2"; if ($result=mysql_query($sql)) { // Classification of trades switch($result) { case 'trd': if ($type == "N") { if ($type1 == "a" and $price >= $price1) { echo 'buy'; } else if ($type1 == "b" and $price <= $price1) { echo 'sell'; } else { echo ''; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/ Share on other sites More sharing options...
GingerRobot Posted June 9, 2009 Share Posted June 9, 2009 So what happens? What doesn't happen? What's supposed to happen? Are there any errors? We need a bit more info than "it's not working" and your code Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852109 Share on other sites More sharing options...
dawsba Posted June 9, 2009 Share Posted June 9, 2009 Try the following instead <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = "127.0.0.1"; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- ?> <?php $sql = "SELECT * FROM `trades2`"; $result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while ($result = mysql_fetch_array($result2)) { switch($result[trade]) { case 'trd': if(strtoupper($result[type])=='N') { if(strtolower($result[type1]) == 'a' && $result[price] >= $result[price1]){echo "Buy";} if(strtolower($result[type1]) == 'b' && $result[price] <= $result[price1]){echo "Sell";} } } } I'm presuming your db structure Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852112 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Hi dawsba, Just run the suggested code and I'm getting the following error message; Notice: Use of undefined constant trade - assumed 'trade' in www\Classification_CodeTest.php on line 18 Notice: Undefined index: trade in \www\Classification_CodeTest.php on line 18 Thanks for the reply by the way. Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852116 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Share Posted June 9, 2009 replace switch($result[trade]) with switch($result['trade']) Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852122 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Ok, now I'm only getting the one Notice: Undefined index: trade in \www\Classification_CodeTest.php on line 18 Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852127 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Non numeric array keys (strings) need to be surrounded by quotes. Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852133 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Non numeric array keys (strings) need to be surrounded by quotes. Excuse my ignorance thorpe I'm sure it's my lack of knowledge but I'm not exactly sure what to do from here... ??? Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852140 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Does anyone know what thorpe means by the following with regards to my code? Non numeric array keys (strings) need to be surrounded by quotes. Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852216 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 As an example. if(strtoupper($result[type])=='N') should be.... if(strtoupper($result['type'])=='N') Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852217 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Ok, I've updated the code but I still get this error message; Notice: Undefined index: trade in C:\......\CodeTest.php on line 18 <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = ""; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- ?> <?php $sql = "SELECT * FROM `trades2`"; $result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while ($result = mysql_fetch_array($result2)) { switch($result['trade']) { case 'trd': if(strtoupper($result['type'])=='N') { if(strtolower($result['type1']) == 'a' && $result['price'] >= $result['price1']){echo "Buy";} if(strtolower($result['type1']) == 'b' && $result['price'] <= $result['price1']){echo "Sell";} } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852229 Share on other sites More sharing options...
trq Posted June 9, 2009 Share Posted June 9, 2009 Are you sure you have a field named trade in your table? Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852247 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Are you sure you have a field named trade in your table? This was the problem I have attached the updated code The name of the field is `TYPE`. Now it appears that the code works as there are no error messages...what steps would I need to display the results? <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = ""; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- $sql = "SELECT * FROM `trades2`"; $result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while ($result = mysql_fetch_array($result2)) { switch($result['TYPE']) { case 'trd': if(strtoupper($result['TYPE'])=='N') { if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";} if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";} } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852261 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Share Posted June 9, 2009 echo $result['FieldName']; I would guess. What are you trying to echo? Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852271 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 My apologies to the more experienced members but I am learning slowly but surely. I have noted next to the code below what results I'm trying to echo...I'm getting a blank screen with this code at the moment. <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = ""; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- $sql = "SELECT * FROM `trades2`"; $result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while ($result = mysql_fetch_array($result2)) { switch($result['TYPE']) { case 'trd': if(strtoupper($result['TYPE'])=='N') { if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";} if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";} } } } ?> if(strtoupper($result['TYPE'])=='N') { if(strtolower($result['TYPE1']) == 'a' && $result['PRICE'] >= $result['PRICE1']){echo "buy";} I want to echo this result if(strtolower($result['TYPE1']) == 'b' && $result['PRICE'] <= $result['PRICE1']){echo "sell";} & this result Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852277 Share on other sites More sharing options...
Garethp Posted June 9, 2009 Share Posted June 9, 2009 Well your main question is why is TYPE1 ==b and Price lower than Price1 if you don't want it to be? Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852284 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 How do you mean? I thought both of those statements were treated equally? If not I am mistaken. The table that I have selected (trades2) has data that meets both criteria. All type columns data is equal to 'N' and I'm trying to classify the data with the code as described and I thought the echo would show a result on the screen with a "buy" or "sell" where trades meet the criteria... Am I on the wrong track here? Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852288 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 I have also tried the following; The only error I get at the moment is as follows; Parse error: parse error in C:\Program Files\EasyPHP 3.0\www\Needs work\Classification_CodeTest1.php on line 31 <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = ""; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- $sql = mysql_query("SELECT * FROM `trades2`"); while ($result = mysql_fetch_array($sql)) { switch($result['TYPE']) { case 'trd': if ('TYPE'=='N'){ if ('TYPE1' =='a' && 'PRICE' >= 'PRICE1'){ echo "buy"; break; } else if ('TYPE1' =='b' && 'PRICE' <= 'PRICE1'){ echo "sell"; break; } } } ?> I'm not quite sure how to move forward with this. I've tried to cut it down to the bare minumum and I still can't make any progress. Basically my thought process is I'm trying to run the sql query to get the data first and then I want to run an if statement on the query and display the output of the result to screen. Where am I going wrong? This is the SQL query; SELECT * FROM `trades2` I want to run the following if statements on the data from the above SQL query and have the result output to screen... if ('TYPE'=='N'){ if ('TYPE1' =='a' && 'PRICE' >= 'PRICE1'){ echo "buy"; break; } else if ('TYPE1' =='b' && 'PRICE' <= 'PRICE1'){ echo "sell"; break; } This has been driving me banana's I've been able to do almost everything else I this is one of the last bits of code I need... Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852418 Share on other sites More sharing options...
suprsnipes Posted June 9, 2009 Author Share Posted June 9, 2009 Please help I have been stuck on this for weeks... I can run other queries without too much of a problem, but an if statement I'm finding much much more difficult...I don't know how to go about it, can it even be done? I want to run my SELECT query first and from the result of the query run the if statement. But where do I put the if statement? And how do I have the results displayed on the screen? I'm so frustrated... :'( Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852465 Share on other sites More sharing options...
dawsba Posted June 9, 2009 Share Posted June 9, 2009 If you could run the following it may help me understand your db structure: and display/post the results . <?php // DATABASE CONNECTIVITY //----------------------------------------------------------------------------------------------------------- $dbserver = ""; $dbusername = ""; $dbpassword = ""; $dbname ="spi"; $link=mysql_connect ($dbserver,$dbusername,$dbpassword) or die ('DB ERROR -> ' . mysql_error()); mysql_select_db ($dbname); //--------------------------------------------------------------------------------------------------------- $sql = "SELECT * FROM `trades2`"; $result2 = mysql_db_query($dbname,$sql,$link) or die("Invalid Query<br>". mysql_error()); while ($result = mysql_fetch_array($result2)) { foreach($result as $k=>$v){echo $k." <-> ".$v ."<BR />";} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852681 Share on other sites More sharing options...
suprsnipes Posted June 10, 2009 Author Share Posted June 10, 2009 Results as requested...it's only a small sample, there are around 2600 records in the table. 0 <-> 18 ID1 <-> 18 1 <-> A SYMBOL1 <-> A 2 <-> 27/05/2009 2:30:37 PM DATE1 <-> 27/05/2009 2:30:37 PM 3 <-> 1430 TIME1 <-> 1430 4 <-> b TYPE1 <-> b 5 <-> 3824 PRICE1 <-> 3824 6 <-> 8 SIZE1 <-> 8 7 <-> 19 ID <-> 19 8 <-> A SYMBOL <-> A 9 <-> 27/05/2009 2:30:37 PM DATE <-> 27/05/2009 2:30:37 PM 10 <-> 1430 TIME <-> 1430 11 <-> N TYPE <-> N 12 <-> 3825 PRICE <-> 3825 13 <-> 1 SIZE <-> 1 0 <-> 19 ID1 <-> 19 1 <-> A SYMBOL1 <-> A 2 <-> 27/05/2009 2:30:37 PM DATE1 <-> 27/05/2009 2:30:37 PM 3 <-> 1430 TIME1 <-> 1430 4 <-> N TYPE1 <-> N 5 <-> 3825 PRICE1 <-> 3825 6 <-> 1 SIZE1 <-> 1 7 <-> 20 ID <-> 20 8 <-> A SYMBOL <-> A 9 <-> 27/05/2009 2:30:37 PM DATE <-> 27/05/2009 2:30:37 PM 10 <-> 1430 TIME <-> 1430 11 <-> N TYPE <-> N 12 <-> 3825 PRICE <-> 3825 13 <-> 1 SIZE <-> 1 0 <-> 22 ID1 <-> 22 1 <-> A SYMBOL1 <-> A 2 <-> 27/05/2009 2:30:37 PM DATE1 <-> 27/05/2009 2:30:37 PM 3 <-> 1430 TIME1 <-> 1430 4 <-> b TYPE1 <-> b 5 <-> 3824 PRICE1 <-> 3824 6 <-> 10 SIZE1 <-> 10 7 <-> 23 ID <-> 23 8 <-> A SYMBOL <-> A 9 <-> 27/05/2009 2:30:38 PM DATE <-> 27/05/2009 2:30:38 PM 10 <-> 1430 TIME <-> 1430 11 <-> N TYPE <-> N 12 <-> 3825 PRICE <-> 3825 13 <-> 1 SIZE <-> 1 0 <-> 25 ID1 <-> 25 1 <-> A SYMBOL1 <-> A 2 <-> 27/05/2009 2:30:38 PM DATE1 <-> 27/05/2009 2:30:38 PM 3 <-> 1430 TIME1 <-> 1430 4 <-> a TYPE1 <-> a 5 <-> 3825 PRICE1 <-> 3825 6 <-> 3 SIZE1 <-> 3 7 <-> 26 ID <-> 26 8 <-> A SYMBOL <-> A 9 <-> 27/05/2009 2:30:38 PM DATE <-> 27/05/2009 2:30:38 PM 10 <-> 1430 TIME <-> 1430 11 <-> N TYPE <-> N 12 <-> 3825 PRICE <-> 3825 13 <-> 1 SIZE <-> 1 Quote Link to comment https://forums.phpfreaks.com/topic/161475-basic-ifelse-php-codeneed-a-little-help/#findComment-852775 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.