monkeypaw201 Posted July 21, 2008 Share Posted July 21, 2008 I have the code below but it seems kind-of messy.. is there any way to clean it up? Also, there seems to be an unexpected T_IF error on the indicated line In the testing scenario I have no PHP SESSION ATM <?php $rank1 = $row_aircraft['rank'] . "First Officer"; $rank2 = $row_aircraft['rank'] . "Captain"; if(isset($_SESSION['pilot_id'])) { $pilot = mysql_query("SELECT * FROM `users` WHERE `pilot_id` = '$_SESSION[pilot_id]'"); $row_pilot = mysql_fetch_array($pilot) if($row_pilot['rank'] == $rank1) //Error occurs on this line { $file = "file"; } elseif($row_pilot['rank'] == $rank1) { $file = "file"; }else{ $file = "details"; } if($row_route['category'] == "International") { if($row_pilot['international'] == "Yes") { $file = "file"; }else{ $file = "details"; } }else{ $file = "details"; } } else { $file = "details"; } ?> Link to comment https://forums.phpfreaks.com/topic/115856-solved-a-bit-in-the-deep-end-replace-6-ifs-with-cleaner-code-ampampampampampamp-unexpe/ Share on other sites More sharing options...
kenrbnsn Posted July 21, 2008 Share Posted July 21, 2008 You're missing a semi-colon on this line: <?php $row_pilot = mysql_fetch_array($pilot) ?> Ken Link to comment https://forums.phpfreaks.com/topic/115856-solved-a-bit-in-the-deep-end-replace-6-ifs-with-cleaner-code-ampampampampampamp-unexpe/#findComment-595619 Share on other sites More sharing options...
kenrbnsn Posted July 21, 2008 Share Posted July 21, 2008 As for cleaning up the if statement, try this: <?php $rank1 = $row_aircraft['rank'] . "First Officer"; $rank2 = $row_aircraft['rank'] . "Captain"; $file = "details"; if(isset($_SESSION['pilot_id'])) { $pilot = mysql_query("SELECT * FROM `users` WHERE `pilot_id` = '$_SESSION[pilot_id]'"); $row_pilot = mysql_fetch_array($pilot); $file = "file"; if($row_pilot['rank'] != $rank1 || ($row_route['category'] == "International" && $row_pilot['international'] != "Yes") || $row_route['category'] != "International") $file = "details"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/115856-solved-a-bit-in-the-deep-end-replace-6-ifs-with-cleaner-code-ampampampampampamp-unexpe/#findComment-595628 Share on other sites More sharing options...
monkeypaw201 Posted July 21, 2008 Author Share Posted July 21, 2008 Ok, so after adding a bit of code I have a new challenge. Users are ranked based on the number of hours they fly, which is calculated every time its requested based on several tables. My problem is I need to allow users to access the file page if they have the minimum rank AND any higher rank. Its a bit confusing but maybe the code will help <?php $rank1 = $row_aircraft['rank'] . "First Officer"; $rank2 = $row_aircraft['rank'] . "Captain"; if(isset($_SESSION['pilot_id'])) { $pilot = mysql_query("SELECT * FROM `users` WHERE `pilot_id` = '$_SESSION[pilot_id]'"); $row_pilot = mysql_fetch_array($pilot); if($row_pilot['rank'] == $rank1) { $file = "file"; $rankreq = "1"; } elseif($row_pilot['rank'] == $rank1) { $file = "file"; $rankreq = "1"; }else{ $file = "details"; } if($row_route['category'] == "International") { if($row_pilot['international'] == "Yes") { $file = "file"; }else{ $file = "details"; $rankintl = "0"; } }else{ $file = "details"; } } else { $file = "details"; } ?> <td> <?php if($file == "file") { echo "<a href='file.php?id=" . $row_timetable['id'] . "' "; ?> onclick="javascript:return confirm('Are you sure you want to place a bid on this flight?')" <?php echo " ><img src='http://www.cormorantair.com/images/pirep.jpg' hieght='15' width='15'>"; } else { echo "<a href='details.php?id=" . $_GET['id'] . "' "; ?> onclick="alert('Sorry, It looks like you are not meeting all the requirements to fly this route. The missing requirements are listed below. \n\n <?php if(!isset($_SESSION['pilot_id'])){echo "- You Must Be Logged In";}else{ ?><?php if(!isset($rankreq)){echo "- You Do Not Have A High Enough Rank \n";} ?><?php if(!isset($rankintl)){echo "- You Are Not Certified For International Flights \n";}} ?>')" <?php echo " ><img src='http://www.cormorantair.com/images/pirep.jpg' hieght='15' width='15'>"; } ?> kenrbnsn thanks for the rapid reply... I tried modifying your code to meet the new issue, but it seemed harder than to just add a '=1' here and there...hehe Link to comment https://forums.phpfreaks.com/topic/115856-solved-a-bit-in-the-deep-end-replace-6-ifs-with-cleaner-code-ampampampampampamp-unexpe/#findComment-595652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.