• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simplified code with PHP?

I've got an unnecessarily long code that basically changes two things. Adds id="active_menu"> into a link in my navigation, and an arrow next to the linked word. You'll understand by looking at my code:

<?php
$edit = $_GET['edit'];
if($edit == "pastEntries")
{
?>
<li><a href="admin.page.php" class="mainlevel"> Main Page </a></li>
<li><a href="admin.page.php?edit=newEntry" class="mainlevel"> Add New Entry </a></li>
<li><a href="admin.page.php?edit=pastEntries" class="mainlevel" id="active_menu">-> Edit Previous Entries </a></li>
<li><a href="admin.page.php?edit=comments" class="mainlevel"> Edit Comments </a></li>
<?php
}
elseif($edit == "newEntry")
{
?>
<li><a href="admin.page.php" class="mainlevel"> Main Page </a></li>
<li><a href="admin.page.php?edit=newEntry" class="mainlevel" id="active_menu">-> Add New Entry </a></li>
<li><a href="admin.page.php?edit=pastEntries" class="mainlevel"> Edit Previous Entries </a></li>
<li><a href="admin.page.php?edit=comments" class="mainlevel"> Edit Comments </a></li>
<?php
}
elseif($edit == "comments")
{
?>
<li><a href="admin.page.php" class="mainlevel"> Main Page </a></li>
<li><a href="admin.page.php?edit=newEntry" class="mainlevel"> Add New Entry </a></li>
<li><a href="admin.page.php?edit=pastEntries" class="mainlevel"> Edit Previous Entries </a></li>
<li><a href="admin.page.php?edit=comments" class="mainlevel" id="active_menu">-> Edit Comments </a></li>
<?php
}
else
{
?>
<li><a href="admin.page.php" class="mainlevel" id="active_menu">-> Main Page </a></li>
<li><a href="admin.page.php?edit=newEntry" class="mainlevel"> Add New Entry </a></li>
<li><a href="admin.page.php?edit=pastEntries" class="mainlevel"> Edit Previous Entries </a></li>
<li><a href="admin.page.php?edit=comments" class="mainlevel"> Edit Comments </a></li>
<?php
}
?>

Any suggestions to simplify that?

EDIT: Nevermind, I got it:
<?php
$edit = isset( $_GET['edit'] ) ? $_GET['edit'] : False;
?>
<li><a href="admin.page.php" class="mainlevel"
<?=( $edit == False ) ? ' id="active_menu">->' : '>'; ?> Main Page </a></li>

<li><a href="admin.page.php?edit=newEntry" class="mainlevel"
<?=( $edit == 'newEntry' ) ? ' id="active_menu">->' : '>'; ?> Add New Entry </a></li>

<li><a href="admin.page.php?edit=pastEntries" class="mainlevel"
<?=( $edit == 'pastEntries' ) ? ' id="active_menu">->' : '>'; ?> Edit Previous Entries </a></li>

<li><a href="admin.page.php?edit=comments" class="mainlevel"
<?=( $edit == 'comments' ) ? ' id="active_menu">->' : '>'; ?> Edit Comments </a></li>
 
Originally posted by: LoKe
Any suggestions to simplify that?

EDIT: Nevermind, I got it:

:thumbsup: I've found the ternary operator to be infinitely useful, especially for web development.
 
Originally posted by: LoKe
EDIT: Nevermind, I got it:
<?php
$edit = isset( $_GET['edit'] ) ? $_GET['edit'] : False;
?>
<li><a href="admin.page.php" class="mainlevel"
<?=( $edit == False ) ? ' id="active_menu">->' : '>'; ?> Main Page </a></li>

<li><a href="admin.page.php?edit=newEntry" class="mainlevel"
<?=( $edit == 'newEntry' ) ? ' id="active_menu">->' : '>'; ?> Add New Entry </a></li>

<li><a href="admin.page.php?edit=pastEntries" class="mainlevel"
<?=( $edit == 'pastEntries' ) ? ' id="active_menu">->' : '>'; ?> Edit Previous Entries </a></li>

<li><a href="admin.page.php?edit=comments" class="mainlevel"
<?=( $edit == 'comments' ) ? ' id="active_menu">->' : '>'; ?> Edit Comments </a></li>

While the code is definitely shorter, its still really 4 if statements and it doesn't really change the codepath.
 
I was just trying to make the code shorter so it's not such an eye sore. Also, the main point was to remove all the extra code that didn't need to be there (links not affected by the change).
 
Back
Top