Jump to content

[SOLVED] Displaying a div with an if statement


andrew_biggart

Recommended Posts

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 

Link to comment
Share on other sites

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>";
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

}
?>

Link to comment
Share on other sites

<?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  }  ?>

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

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>

";
}
?>

Link to comment
Share on other sites

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>
";
}
?>

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.