Reputation: 376
I'm creating little statistics, but got problem.. I have fish , rig , bait and line info from database.
And I want to check every possible variations with this information. My code looks like this:
if ((isset($_SESSION['fish_id'])) && (isset($_SESSION['rig_id'])) && (isset($_SESSION['bait_id'])) && (isset($_SESSION['line_id']))) {
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo 'FISH , RIG , BAIT , LINE';
echo '</li></ul></div>';
} elseif ((isset($_SESSION['fish_id'])) && (isset($_SESSION['rig_id'])) && (isset($_SESSION['bait_id'])) && (!isset($_SESSION['line_id']))) {
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo 'FISH , RIG , BAIT , *LINE*';
echo '</li></ul></div>';
} elseif ((isset($_SESSION['fish_id'])) && (isset($_SESSION['rig_id'])) && (!isset($_SESSION['bait_id'])) && (isset($_SESSION['line_id']))) {
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo 'FISH , RIG , *BAIT* , LINE';
echo '</li></ul></div>';
} elseif ((isset($_SESSION['fish_id'])) && (!isset($_SESSION['rig_id'])) && (isset($_SESSION['bait_id'])) && (isset($_SESSION['line_id']))) {
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo 'FISH , *RIG* , BAIT , LINE';
echo '</li></ul></div>';
} ((!isset($_SESSION['fish_id'])) && (isset($_SESSION['rig_id'])) && (isset($_SESSION['bait_id'])) && (isset($_SESSION['line_id']))) {
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo '*FISH* , RIG , BAIT , LINE';
echo '</li></ul></div>';
}
After that I need to check if fish is not set and rig is not set.. and so on..
How can I avoid this repetition?
Upvotes: 4
Views: 157
Reputation: 27565
The fish, rig, bait, line seem to be completely independent of each other, so the conditions may be applied separately to each of these.
The below code uses the ternary operator ?:
, but it is essentially the same as a single if-else construct:
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
$items = array();
$items[] = isset($_SESSION['fish_id']) ? 'FISH' : '*FISH*';
$items[] = isset($_SESSION['rig_id']) ? 'RIG' : '*RIG*';
$items[] = isset($_SESSION['bait_id']) ? 'BAIT' : '*BAIT*';
$items[] = isset($_SESSION['line_id']) ? 'LINE' : '*LINE*';
echo join(' , ', $items);
echo '</li></ul></div>';
Or you can do it even more generically with a loop (it would make sense if you had more items; arguable if it makes sense with four fixed items):
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
$items = array();
foreach(array('fish', 'rig', 'bait', 'line') as $name) {
$id = $name . '_id';
$nameUppercased = strtoupper($name);
$items[] = isset($_SESSION[$id]) ? $nameUppercased : "*$nameUppercased*";
}
echo join(' , ', $items);
echo '</li></ul></div>';
Upvotes: 4
Reputation: 1095
Please use below code. As you checking whether variable is set in session and then modifying the string. So if any one of session is set then only it will proceed.
if ( (isset($_SESSION['fish_id']) )
|| (isset($_SESSION['rig_id']) )
|| (isset($_SESSION['bait_id']) )
|| (isset($_SESSION['line_id']) ) ) {
$names = array();
$names[] = isset($_SESSION['fish_id']) ? 'FISH' : '*FISH*';
$names[] = isset($_SESSION['rig_id']) ? 'RIG' : '*RIG*';
$names[] = isset($_SESSION['bait_id']) ? 'BAIT' : '*BAIT*';
$names[] = isset($_SESSION['line_id']) ? 'LINE' : '*LINE*';
echo '<img class="img-responsive" src="../assets/img/choose_fish.png">';
echo '<div class="list-notes"> <ul> <li class="name">';
echo (string)implode(' , ', $names);
echo '</li></ul></div>';
}
Upvotes: 1