Jump to content

Easier way to do this?


mat3000000

Recommended Posts

Here is my long drawn out code. Is there a better way of doing this and how can I echo the JS without it messing up the php. I really need the JS link to work!

<?php
if(isset($_SESSION['rest'])){ echo'<table width="300" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="middle" class="topnav"><div align="center" ><a href="index.php">Home</a></div></td>
          <td valign="middle" class="topnav"><div align="center"><a href="restpanel.php">User Panel</a></div></td>
          <td valign="middle" class="topnav"><div align="center"><a href="logout.php">Logout</a></div></td>
        </tr>
    </table>';}
if(isset($_SESSION['chef'])){ echo' <table width="300" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="middle" class="topnav"><div align="center" ><a href="index.php">Home</a></div></td>
          <td valign="middle" class="topnav"><div align="center"><a href="chefpanel.php">User Panel</a></div></td>
          <td valign="middle" class="topnav"><div align="center"><a href="logout.php">Logout</a></div></td>
        </tr>
    </table>';}

if(!isset($_SESSION['chef']) || !isset($_SESSION['rest'])){ echo "<table width='350' border='0' cellpadding='0' cellspacing='0'>
      <tr>
        <td valign='middle' class='topnav'><div align='center'><a href='index.php'>Home</a></div></td>
        <td valign='middle' class='topnav'><div align='center' >"<a href="javascript:void(0)" onclick = "document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'">Log In</a></div></td>
        
        <td valign="middle"class="topnav"><div align="center"><a href="register.php">Sign Up</a></div></td>
        <td valign="middle"class="topnav"><div align="center" ><a href="contact.php">Contact Us</a></div></td>
      </tr>
    </table>";}



?>

 

Link to comment
Share on other sites

The logic in that doesn't seem right. Those three blocks of code are independant of one another. So, if the user is logged in for 'rest' and not 'chef' then both the first and third sections are executed. But the third section seems to be for users that aren't logged in.

 

The first two blocks are identical except for the URL for the user panel. As for the javascript, you really should create a javascript function instead of trying to stuff all that code into an onclick event parameter.

 

Also, since you are using the exact same format for all the links, I would create a single function to generate the links to prevent errors in display from typos. Plus, you can easily modify the format of the links by changing the function and not many lines of code.

 

Here is what I would do:

<?php

//Functin to create links in consistent manner
function createLinkTD($title, $href, $clickEvent=false)
{
    $onclick = ($clickEvent) ? " onclick=\"{$clickEvent};\"" : '';
    $linkHTML  = "<td valign=\"middle\" class=\"topnav\">";
    $linkHTML .= "<div align=\"center\">";
    $linkHTML .= "<a href=\"{$href}\"{$onclick}>{$title}</a>";
    $linkHTML .= "</div>";
    $linkHTML .= "</td>\n";
    return $linkHTML;
}

//Determine the links to be created
$links = createLinkTD('Home', 'index.php');
if(isset($_SESSION['rest']) || isset($_SESSION['chef']))
{
    //Determine User Panel HREF based on which session value is set
    $userPanelHREF = (isset($_SESSION['rest'])) ? 'restpanel.php' : 'chefpanel.php';
    $links .= createLinkTD('User Panel', $userPanelHREF);
    $links .= createLinkTD('Logout', 'logout.php');
}
else
{
    $links .= createLinkTD('Log In', 'javascript:void(0)', 'logIn()');
    $links .= createLinkTD('Sign Up', 'register.php');
    $links .= createLinkTD('Contact Us', 'contact.php');
}

//Create the HTML output
?>
<script type="text/javascript">

function logIn()
{
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
}

</script>

<table width=\"300\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
    <tr>
    <?php echo $links; ?>
    </tr>
</table>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.