andrew_biggart Posted March 30, 2009 Share Posted March 30, 2009 Ok what i want to do is have this div <div id="nav_menu"> <img alt="" src="../icons/myacc.gif" width="12" height="11" /> <a class="button6" href="my_profile.php">My Account</a> <img alt="" src="../icons/mypro.gif" width="12" height="11" /> <a class="button6" href="profile.php?username=$myusername">My Profile</a> <img alt="" src="../icons/mymes.gif" width="12" height="11" /> <a class="button6" href="my_profile_messages.php">My Messages</a> <img alt="" src="../icons/myale.gif" width="11" height="11" /> <a class="button6" href="my_profile_alerts.php">My Alerts</a> <img alt="" src="../icons/myreq.gif" width="12" height="11" /> <a class="button6" href="my_profile_requests.php">My Requests</a> <img alt="" src="../icons/myfri.gif" width="12" height="11" /> <a class="button6" href="my_profile_friends.php">My Friends</a> <img alt="" src="../icons/mylog.gif" width="12" height="12" /> <a class="button6" href="logout.php">Logout</a> </div> But i only want this information if the session $myusername is valid otherwise i dont want it to show up. Can someone please point me in the right direction to do this as i am at the end of teather with it Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/ Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 I didn't bother escaping all the quotes in the div, but you get the basic idea session_start() if(isset($_SESSION['myusername'])) { echo " <div id="nav_menu"> <img alt="" src="../icons/myacc.gif" width="12" height="11" /> <a class="button6" href="my_profile.php">My Account</a> <img alt="" src="../icons/mypro.gif" width="12" height="11" /> <a class="button6" href="profile.php?username=$myusername">My Profile</a> <img alt="" src="../icons/mymes.gif" width="12" height="11" /> <a class="button6" href="my_profile_messages.php">My Messages</a> <img alt="" src="../icons/myale.gif" width="11" height="11" /> <a class="button6" href="my_profile_alerts.php">My Alerts</a> <img alt="" src="../icons/myreq.gif" width="12" height="11" /> <a class="button6" href="my_profile_requests.php">My Requests</a> <img alt="" src="../icons/myfri.gif" width="12" height="11" /> <a class="button6" href="my_profile_friends.php">My Friends</a> <img alt="" src="../icons/mylog.gif" width="12" height="12" /> <a class="button6" href="logout.php">Logout</a> </div>"; } Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797146 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 Thankyou, so do i need to chage all the " in the div to ' ?? Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797155 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 you escape " with \" Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797158 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 Ok so i have changed my mind! instead of displaying the div i want to display different info in the div depending on if the user is logged in or not! if the user is not logged in i want to display the following login form. <form method="post"> <table style="float: right"> <tr> <td><input class="username" name="username" type="text" value="Username"/></td> <td class="logmein">:Username</td> <td><input class="password" name="Password1" type="password" value="Password"/></td> <td class="logmein">:Password</td> <td><input class="submit" name="Submit2" type="submit" value="C'umon in"/><a href="user_register.htm"><input class="register" name="Submit2" type="button" value="Register"/></a></td> </tr> </table> </form> and if the user is logged in display a sub menu like this <img alt="" src="../icons/myacc.gif" width="12" height="11" /> <a class="button6" href="my_profile.php">My Account</a> <img alt="" src="../icons/mypro.gif" width="12" height="11" /> <a class="button6" href="profile.php?username=$myusername">My Profile</a> <img alt="" src="../icons/mymes.gif" width="12" height="11" /> <a class="button6" href="my_profile_messages.php">My Messages</a> <img alt="" src="../icons/myale.gif" width="11" height="11" /> <a class="button6" href="my_profile_alerts.php">My Alerts</a> <img alt="" src="../icons/myreq.gif" width="12" height="11" /> <a class="button6" href="my_profile_requests.php">My Requests</a> <img alt="" src="../icons/myfri.gif" width="12" height="11" /> <a class="button6" href="my_profile_friends.php">My Friends</a> <img alt="" src="../icons/mylog.gif" width="12" height="12" /> <a class="button6" href="logout.php">Logout</a> Basically i want the code to check whether or not the session myusername exists and echo the correct html if it is or isnt! Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797262 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 its the exact same idea, just edit the IF statement for whatever condition you want. I think with a little effort you can figure this one out on your own Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797264 Share on other sites More sharing options...
MatthewJ Posted March 30, 2009 Share Posted March 30, 2009 if all the quotes are doubles in the div... and you're not outputting vars... just change the quotes surrounding the content to single quotes... no need to escape 70 double quotes Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797281 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 ok so this is what i have got! <?php if(isset($_SESSION['myusername'])) { echo " <img alt=/"/" src=/"../icons/myacc.gif/" width=/"12/" height=/"11/" /> <a class=/"button6/" href=/"my_profile.php/">My Account</a> <img alt=/"/" src=/"../icons/mypro.gif/" width=/"12/" height=/"11/" /> <a class=/"button6/" href=/"profile.php?username=$myusername/">My Profile</a> <img alt=/"/" src=/"../icons/mymes.gif/" width=/"12/" height=/"11/" /> <a class=/"button6/" href=/"my_profile_messages.php/">My Messages</a> <img alt=/"/" src=/"../icons/myale.gif/" width=/"11/" height=/"11/" /> <a class=/"button6/" href=/"my_profile_alerts.php/">My Alerts</a> <img alt=/"/" src=/"../icons/myreq.gif/" width=/"12/" height=/"11/" /> <a class=/"button6/" href=/"my_profile_requests.php/">My Requests</a> <img alt=/"/" src=/"../icons/myfri.gif/" width=/"12/" height=/"11/" /> <a class=/"button6/" href=/"my_profile_friends.php/">My Friends</a> <img alt=/"/" src=/"../icons/mylog.gif/" width=/"12/" height=/"12/" /> <a class=/"button6/" href=/"logout.php/">Logout</a> "; } else{ <form method=/"post/"> <table style=/"float: right/"> <tr> <td><input class=/"username/" name=/"username/" type=/"text/" value=/"Username/"/></td> <td class=/"logmein/">:Username</td> <td><input class=/"password/" name=/"Password1/" type=/"password/" value=/"Password/"/></td> <td class=/"logmein/">:Password</td> <td><input class=/"submit/" name=/"Submit2/" type=/"submit/" value=/"C'umon in/"/><a href=/"user_register.htm/"><input class=/"register/" name=/"Submit2/" type=/"button/" value=/"Register/"/></a></td> </tr> </table> </form> } ?> am i going along the right lines? thankyou by the way Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797282 Share on other sites More sharing options...
corbin Posted March 30, 2009 Share Posted March 30, 2009 http://www.tizag.com/phpT/strings.php That very lightly touches on it. Think about PHP syntax... $string = "something"; Now, what if something contains a "? Wouldn't that make the string end? So, you have to escape " unless you want the string to end: $string = "Corbin said, \"Hello\""; For example. Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797293 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 Ok im getting the blank screen which means theres an error in my code grr. I have tried the above solution by changing all " to /", and i have also tried changing them to '. But i cant get my head around this at all. This is the code im trying to use can someone please have a scan through it and let me know what i am doing wrong! Thanks for your patience! <?php if(isset($_SESSION['myusername'])) { echo " <img alt='' src='../icons/myacc.gif/' width='12' height='11' /> <a class='button6' href='my_profile.php'>My Account</a> <img alt='' src='../icons/mypro.gif' width='12' height='11' /> <a class='button6' href='profile.php'>My Profile</a> <img alt='' src='../icons/mymes.gif' width='12' height='11' /> <a class='button6' href='my_profile_messages.php'>My Messages</a> <img alt='' src='../icons/myale.gif' width='11' height='11' /> <a class='button6' href='my_profile_alerts.php'>My Alerts</a> <img alt='' src='../icons/myreq.gif' width='12' height='11' /> <a class='button6' href='my_profile_requests.php'>My Requests</a> <img alt='' src='../icons/myfri.gif' width='12' height='11' /> <a class='button6' href='my_profile_friends.php'>My Friends</a> <img alt='' src='../icons/mylog.gif' width='12' height='12' /> <a class='button6' href='logout.php'>Logout</a> "; } else{ <form method='post'> <table style='float: right'> <tr> <td><input class='username' name='username' type='text' value='Username'/></td> <td class='logmein'>:Username</td> <td><input class='password' name='Password1' type='password' value='Password'/></td> <td class='logmein'>:Password</td> <td><input class='submit' name='Submit2' type='submit' value='C'umon in'/><a href='user_register.htm'"><input class='register' name='Submit2' type='button' value='Register'/></a></td> </tr> </table> </form> } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797313 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 you have to echo the HTML in your else statement Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797325 Share on other sites More sharing options...
Andy-H Posted March 30, 2009 Share Posted March 30, 2009 <?php //session_start(); if(isset($_SESSION['myusername'])) { ?> <img alt="" src="../icons/myacc.gif" width="12" height="11" /> <a class="button6'" href="my_profile.php">My Account</a> <img alt="" src="../icons/mypro.gif" width="12" height="11" /> <a class="button6" href="profile.php">My Profile</a> <img alt="" src="../icons/mymes.gif" width="12" height="11" /> <a class="button6" href="my_profile_messages.php">My Messages</a> <img alt="" src="../icons/myale.gif" width="11" height="11" /> <a class="button6" href="my_profile_alerts.php">My Alerts</a> <img alt="" src="../icons/myreq.gif" width="12" height="11" /> <a class="button6" href="my_profile_requests.php">My Requests</a> <img alt="" src="../icons/myfri.gif" width="12" height="11" /> <a class="button6" href="my_profile_friends.php">My Friends</a> <img alt="" src="../icons/mylog.gif" width="12" height="12" /> <a class="button6" href="logout.php">Logout</a> <?php }else{ ?> <form method="post"> <table style="float: right"> <tr> <td><input class="username" name="username" type="text" value="Username" onfocus="if (this.value == 'Username'){ this.value = ''; }" onblur="if (this.value == ''){ this.value = 'Username'; }" /></td> <td class="logmein">:Username</td> <td><input class="password" name="Password1" type="password" value="Password" onfocus="if (this.value == 'Password'){ this.value = ''; }" onblur="if (this.value == ''){ this.value = 'Password'; }" /></td> <td class="logmein">:Password</td> <td><input class="submit" name="Submit2" type="submit" value="C'mon in" /><a href="user_register.htm"><input class="register" name="Submit2" type="button" value="Register" /></a></td> </tr> </table> </form> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797327 Share on other sites More sharing options...
lonewolf217 Posted March 30, 2009 Share Posted March 30, 2009 edited Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797328 Share on other sites More sharing options...
bloodgoat Posted March 30, 2009 Share Posted March 30, 2009 I totally suck at sessions, maybe if you do, too, you can try using cookies. This is what a snippet my panel looks like for editing my page so far, mainly in regards to the navigation: <?php /* NAVIGATION */ /* ADD ANYTHING YOU LIKE IN THE FORM OF: $navigation[] = array("link title", "link url", "link description"); */ $navigation = array(); $navigation[] = array("Home", $_SERVER['PHP_SELF'], "Main panel of ".$site_name." Management"); $navigation[] = array("Log Out", $_SERVER['PHP_SELF']."?action=logout, "Log out of ".$site_name." Management"); /* GET USER LEVEL BASED ON THE INFORMATION SUPPLIED FROM THE COOKIE REGISTERED ON LOGIN */ if($_COOKIE['levl'] == "3"){ $user_type = "Administrator"; $submit = "<input type=\"submit\" value=\"Submit Changes\" class=\"input\">"; $navigation[] = array("Recent Activity", $_SERVER['PHP_SELF']."?action=recent_activity", "Modify the recent activity features"); } elseif($_COOKIE['levl'] == "1"){ $user_type = "Demonstration"; $submit = "As a demo user you do not have the permissions to make changes."; } sort($navigation); /* Just to make things alphabetical, nawmeen? */ ?> Basically when you log in, your user level is set (1, 2, or 3) in a cookie ("levl"). As the highest level user (3) the "Recent Activity" link will be displayed for you, and only you. Any user with a level beneath that will not see it. Of course, this can easily be modified so that the only required parameter is that you are logged in. Something like: <?php if(isset($_COOKIE['username'])) /*or whatever you called your username cookie*/ { $navigation = array(); $navigation[] = array("Home", $_SERVER['PHP_SELF'], "Main panel of ".$site_name." Management"); } ?>; So for your case, maybe something like this would work <?php if(isset($_COOKIE['username'])){ $links = <<< html <img alt="" src="../icons/myacc.gif" width="12" height="11" /> <a class="button6'" href="my_profile.php">My Account</a> <img alt="" src="../icons/mypro.gif" width="12" height="11" /> <a class="button6" href="profile.php">My Profile</a> <img alt="" src="../icons/mymes.gif" width="12" height="11" /> <a class="button6" href="my_profile_messages.php">My Messages</a> <img alt="" src="../icons/myale.gif" width="11" height="11" /> <a class="button6" href="my_profile_alerts.php">My Alerts</a> <img alt="" src="../icons/myreq.gif" width="12" height="11" /> <a class="button6" href="my_profile_requests.php">My Requests</a> <img alt="" src="../icons/myfri.gif" width="12" height="11" /> <a class="button6" href="my_profile_friends.php">My Friends</a> <img alt="" src="../icons/mylog.gif" width="12" height="12" /> <a class="button6" href="logout.php">Logout</a> html; echo $links; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797333 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 Ok what a dick lol ive added the echo but it still isnt working. I have my session start at the very beginning of the page so thats not the problem! any ideas? again thanks for your patience! <?php if(isset($_SESSION['myusername'])) { echo " <img alt='' src='../icons/myacc.gif/' width='12' height='11' /> <a class='button6' href='my_profile.php'>My Account</a> <img alt='' src='../icons/mypro.gif' width='12' height='11' /> <a class='button6' href='profile.php'>My Profile</a> <img alt='' src='../icons/mymes.gif' width='12' height='11' /> <a class='button6' href='my_profile_messages.php'>My Messages</a> <img alt='' src='../icons/myale.gif' width='11' height='11' /> <a class='button6' href='my_profile_alerts.php'>My Alerts</a> <img alt='' src='../icons/myreq.gif' width='12' height='11' /> <a class='button6' href='my_profile_requests.php'>My Requests</a> <img alt='' src='../icons/myfri.gif' width='12' height='11' /> <a class='button6' href='my_profile_friends.php'>My Friends</a> <img alt='' src='../icons/mylog.gif' width='12' height='12' /> <a class='button6' href='logout.php'>Logout</a> "; } else{ echo " <form method='post'> <table style='float: right'> <tr> <td><input class='username' name='username' type='text' value='Username'/></td> <td class='logmein'>:Username</td> <td><input class='password' name='Password1' type='password' value='Password'/></td> <td class='logmein'>:Password</td> <td><input class='submit' name='Submit2' type='submit' value='C'umon in'/><a href='user_register.htm'"><input class='register' name='Submit2' type='button' value='Register'/></a></td> </tr> </table> </form> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797334 Share on other sites More sharing options...
andrew_biggart Posted March 30, 2009 Author Share Posted March 30, 2009 Ok ive worked it out for myself and got it working. Thankyou for everyone that helped i appreciate it. For anyone that cares hear is the working code. <?php $username=$_SESSION['myusername']; if(isset($_SESSION['myusername'])) { echo " <img alt='' src='../icons/myacc.gif' width='12' height='11' /> <a class='button6' href='my_profile.php'>My Account</a> <img alt='' src='../icons/mypro.gif' width='12' height='11' /> <a class='button6' href='profile.php?username=$username'>My Profile</a> <img alt='' src='../icons/mymes.gif' width='12' height='11' /> <a class='button6' href='my_profile_messages.php'>My Messages</a> <img alt='' src='../icons/myale.gif' width='11' height='11' /> <a class='button6' href='my_profile_alerts.php'>My Alerts</a> <img alt='' src='../icons/myreq.gif' width='12' height='11' /> <a class='button6' href='my_profile_requests.php'>My Requests</a> <img alt='' src='../icons/myfri.gif' width='12' height='11' /> <a class='button6' href='my_profile_friends.php'>My Friends</a> <img alt='' src='../icons/mylog.gif' width='12' height='12' /> <a class='button6' href='logout.php'>Logout</a> "; } else{ echo " <form method='post' action='check_login.php'> <table style='float: right'> <tr> <td><input class='username' name='myusername' type='text' value='Username'/></td> <td class='logmein'>:Username</td> <td><input class='password' name='mypassword' type='password' value='Password'/></td> <td class='logmein'>:Password</td> <td><input class='submit' name='Submit2' type='submit' value='Cumon in'/><a href='user_register.htm'><input class='register' name='Submit2' type='button' value='Register'/></a></td> </tr> </table> </form> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/151816-solved-displaying-a-div-with-an-if-statement/#findComment-797380 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.