Skipjackrick Posted February 19, 2009 Share Posted February 19, 2009 Ok, So I am trying to set some if/else statements with several conditions and I am getting a fatal error. Example, IF num_rows = 3 {execute code 1} else { IF num_rows = 4 {execute code 2} } else { {execute code 3} } } Anyways, maybe its possible....here is what I've got so far.. <?php $querymembers = "SELECT * FROM anglers WHERE team_id=$teamvar GROUP BY angler LIMIT 5"; $teamanglers = mysql_query($querymembers) or die(mysql_error()); if (mysql_num_rows($teamanglers) = 3) { while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } $results[3]['angler'] = 0; $results[3]['handle'] = vacant; $results[3]['name'] = vacant; $results[3]['kayak'] = vacant; $results[4]['angler'] = 0; $results[4]['handle'] = vacant; $results[4]['name'] = vacant; $results[4]['kayak'] = vacant; } else { if (mysql_num_rows($teamanglers) = 4) { while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } $results[4]['angler'] = 0; $results[4]['handle'] = vacant; $results[4]['name'] = vacant; $results[4]['kayak'] = vacant; } else { while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/ Share on other sites More sharing options...
RichardRotterdam Posted February 19, 2009 Share Posted February 19, 2009 maybe your looking for something like this <?php if($condition1){ //condition1 }elseif($condition2){ //condition2 }else{ //other } ?> or look into switch statement Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765818 Share on other sites More sharing options...
Skipjackrick Posted February 19, 2009 Author Share Posted February 19, 2009 maybe your looking for something like this <?php if($condition1){ //condition1 }elseif($condition2){ //condition2 }else{ //other } ?> or look into switch statement I still get a fatal error and This is the only code I typed into the page. Is this a syntax issue or something else? Not sure? Fatal error: Can't use function return value in write context in <?php $querymembers = "SELECT * FROM anglers WHERE team_id=$teamvar GROUP BY angler LIMIT 5"; $teamanglers = mysql_query($querymembers) or die(mysql_error()); if (mysql_num_rows($teamanglers)=3) { while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } $results[3]['angler'] = 0; $results[3]['handle'] = 'vacant'; $results[3]['name'] = 'vacant'; $results[3]['kayak'] = 'vacant'; $results[4]['angler'] = 0; $results[4]['handle'] = 'vacant'; $results[4]['name'] = 'vacant'; $results[4]['kayak'] = 'vacant'; } elseif(mysql_num_rows($teamanglers)=4){ while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } $results[4]['angler'] = 0; $results[4]['handle'] = 'vacant'; $results[4]['name'] = 'vacant'; $results[4]['kayak'] = 'vacant'; } else { while($row = mysql_fetch_assoc($teamanglers)) { $results[] = $row; } } ?> [code] Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765833 Share on other sites More sharing options...
Philip Posted February 19, 2009 Share Posted February 19, 2009 Umm, what exactly are you trying to do with your script? Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765835 Share on other sites More sharing options...
MasterACE14 Posted February 19, 2009 Share Posted February 19, 2009 this line... if (mysql_num_rows($teamanglers)=3) should be... if (mysql_num_rows($teamanglers) == 3) Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765837 Share on other sites More sharing options...
RichardRotterdam Posted February 19, 2009 Share Posted February 19, 2009 Umm, what exactly are you trying to do with your script? I was just going to ask that. What result do you want in the end can you give a discription. Looking at your code it looks a bit messy Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765839 Share on other sites More sharing options...
Philip Posted February 19, 2009 Share Posted February 19, 2009 As should the next if statement I just don't understand why you're pulling stuff from a database, and then setting the result array to another value: $results[3]['angler'] = 0; Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765840 Share on other sites More sharing options...
Skipjackrick Posted February 19, 2009 Author Share Posted February 19, 2009 this line... if (mysql_num_rows($teamanglers)=3) should be... if (mysql_num_rows($teamanglers) == 3) Ah......Thanks... WORKS GREAT! Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765841 Share on other sites More sharing options...
MasterACE14 Posted February 19, 2009 Share Posted February 19, 2009 no problem. As should the next if statement I just don't understand why you're pulling stuff from a database, and then setting the result array to another value: $results[3]['angler'] = 0; lost me there too. lol Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765842 Share on other sites More sharing options...
Skipjackrick Posted February 19, 2009 Author Share Posted February 19, 2009 As should the next if statement I just don't understand why you're pulling stuff from a database, and then setting the result array to another value: $results[3]['angler'] = 0; Well, its a fix for the rest of my code in the page. If there are only three rows in the query then I get. $results[0]['angler'] = 1 $results[1]['angler'] = 2 $results[2]['angler'] = 3 But I need to set the following variables to something otherwise it leaves my other queries empty and that won't work.....Therefore...I just set them as some other value that will make do. $results[3]['angler'] = 0 $results[4]['angler'] = 0 Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765845 Share on other sites More sharing options...
CerealBH Posted February 19, 2009 Share Posted February 19, 2009 you might want to look into a a switch statement, would really make things easier on the eyes Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765846 Share on other sites More sharing options...
MasterACE14 Posted February 19, 2009 Share Posted February 19, 2009 or just format the script abit. Current layout is hard to read. <?php if($var == $var2) { while($row = mysql_fetch_array($q)) { // do something } } Just format it something like this.... <?php if($var == $var2) { while($row = mysql_fetch_array($q)) { // do something } } Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765850 Share on other sites More sharing options...
Philip Posted February 19, 2009 Share Posted February 19, 2009 Something like: <?php // query $querymembers = "SELECT * FROM `anglers` WHERE `team_id`='$teamvar' GROUP BY `angler` LIMIT 5"; $teamanglers = mysql_query($querymembers) or die(mysql_error()); // get count of how many rows $rowCount = mysql_num_rows($teamanglers); // let's get the results into an array while($row = mysql_fetch_assoc($teamanglers)) $results[] = $row; // what do we have? switch($rowCount) { case 3: $results[3]['angler'] = 0; $results[3]['handle'] = 'vacant'; $results[3]['name'] = 'vacant'; $results[3]['kayak'] = 'vacant'; // since you wanted if 3 rows to set [4] as well, then we won't place a break here. case 4: $results[4]['angler'] = 0; $results[4]['handle'] = 'vacant'; $results[4]['name'] = 'vacant'; $results[4]['kayak'] = 'vacant'; break; default: // do nothing I suppose break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765852 Share on other sites More sharing options...
Skipjackrick Posted February 19, 2009 Author Share Posted February 19, 2009 WOW! That is alot easier on the eyes... Thanks! I just don't have enough knowledge about all of the different functions to use. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/145862-if-else-multiple-conditions-is-this-possible/#findComment-765859 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.