mat3000000 Posted January 27, 2011 Share Posted January 27, 2011 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>";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/225894-easier-way-to-do-this/ Share on other sites More sharing options...
Psycho Posted January 27, 2011 Share Posted January 27, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/225894-easier-way-to-do-this/#findComment-1166257 Share on other sites More sharing options...
mat3000000 Posted January 28, 2011 Author Share Posted January 28, 2011 You are great man! Thanks so much! That is perfect! Quote Link to comment https://forums.phpfreaks.com/topic/225894-easier-way-to-do-this/#findComment-1166577 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.