Jump to content

[SOLVED] A bit in the deep end: Replace 6 IF's With Cleaner Code? && Unexpe


monkeypaw201

Recommended Posts

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";
}
?>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.