PDA

View Full Version : How to replace an image when logged into the account?



crf121359
05-16-2010, 03:23 PM
1) Script Title: How to replace an image when logged into the account?

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex1/staticmenu3.htm

3) Describe problem:

Hi Everyone,

I am making a website which is in PHP.

Could anyone help me and advise me on how to replace an image (Sing-up button) with another image when the user's are logged in?

For example: The sign-up button is showing in the header.php and when the users logged in I want that image to change to another image!!

Please view the image here: http://img508.imageshack.us/i/samplel.jpg/

is there any piece of code that I could place in my header.php in order to do that?

Any help will be much appreciated.

Thank you.




Moderator note: this was originally posted as 3 separate threads. They have all been merged, so that's why reading the posts below is confusing. And that's a good reason NOT to post the same question more than once!

crf121359
05-16-2010, 04:24 PM
[Duplicate post, content removed. Same question as above.]

crf121359
05-16-2010, 04:25 PM
[Duplicate post, content removed. Same question as above.]

naoshad
05-16-2010, 05:31 PM
Hi,
There is a very simple way to do that. While you are authenticating a user I am assuming that you are using session to save the authenticated user's record. I am assuming that you are storing a value in session like $_SESSION[user_id]

then in your sidebar you can use a simple logic:


if(isset($_SESSION[user_id]))
echo '<img src="abc.jpg">';
else
echo '<img src="signup.jpg">';

Have a try... best of luck! :)

Beverleyh
05-16-2010, 06:16 PM
Without seeing your members script, something like this, with the logged-in variable passed to the session;


<?php if (isset($_SESSION['logged_in_user'])) { ?>

<!-- any HTML -->
<img src="images/hello_member.jpg" alt="">

<?php } else { ?>

<!-- any HTML -->
<img src="images/login_prompt.jpg" alt="">

<?php } ?>

For something more specific though, we'd need to see your php code.

I do something similar for one of my own sites;
Guests get the boring banner, login panel and 3 mini-quick icons (left side) while members get the character banner and 6 mini-quick icons (left side). As webmaster, I also get an extra menu button on the right for administration.

Logged out -
http://jemthingsandstuff.co.uk/testing/images/loggedout.jpg

Logged in -
http://jemthingsandstuff.co.uk/testing/images/loggedin.jpg

Hope that helps

djr33
05-16-2010, 06:28 PM
1. It's best to replace the entire thing (including the link, for example) so that the new image doesn't go to the same place (I'm assuming you have the sign up image as a link).

2. Naoshad, using an index name directly [user_id] will give a warning if you don't have "notice" errors turned off. It parses as a constant of the same name, so it WILL work, but it's not correct. Use ['user_id'] instead to avoid errors and have more efficient code.



Here's a slightly adjusted version. Adjust as needed:

if(isset($_SESSION['user_id']))
echo '<img src="welcomeuser.jpg">';
else
echo '<a href="signup.php"><img src="signup.jpg"></a>';


Using the html directly (outside of PHP) is actually a great way to do it, so just borrow from Beverley's post above, but also remember it's probably best to include the <a> tag there.



Additionally, since you've posted this in several places (and now it's all merged), there's another complicating factor: the script this is all based on. The answers here are general and not for the specific script.
It should still generally apply, but you may have to adjust some or use PHP to dynamically generate the Javascript because it's a little different using that.

crf121359
05-16-2010, 08:47 PM
Hi guys,

Thanks for the replies.
Unfortunately I am still struggling to get this done!!

Here is a copy of the header.php:


<div id="wrapper_header">



<div class="logo_s"><a href="<?=_BASEURL_?>/" title="<?=_TITLE_?>"><img src="<?=_BASEURLMEDIA_?>/logo.png" alt="My website" /></a><div class="beta"></div></div>

<div id="log_pane">

<? if($_SESSION['uid'] ==''){ ?>

<div class="corner_l"><div class="corner_c">



<form method="post" action="<?=_BASEURL_?>/login.php" name="frm11">

<div><input type="text" name="email" value="<?=_EML_ID11_?>" class="inputtext" onclick="_delete_login_fields(this, '<?=_EML_ID11_?>', this.value);"/></div>

<div><input type="password" name="password" value="<?=_PWS_11111_?>" class="inputtext" onclick="_delete_login_fields(this, '<?=_PWS_11111_?>', this.value);" /></div>

<div><input type="submit" name="submit" value=" " class="submit" /></div>

</form>

<div class="reg">

<a href="<?=_BASEURL_?>/register/index.html">Register</a> | <a href="<?=_BASEURL_?>/forgot-your-password/index.html">Forgot Your Password</a></div></div></div>

<? }else{?>

<div class="corner_l"><div class="corner_c">

<div class="sfl tm15px lm30px">Welcome</div>

<div class="normaltextblackbold11 lm5px sfl"> <?=get_uname($_SESSION['uid'])?></div>





<?

$uname=mysql_fetch_array(mysql_query("select id,uname,image,no_bids from user where id=".$_SESSION['uid']));

?>



<div class="tm10px "> Available My website Credits:</div>

<div id="user_current_bids" class="normaltextblackbold11 lm5px sfl"> <?=$uname['no_bids'];?> </div>



<script language="javascript">

function find_no_bids(strURL)

{



var xmlHttpReq_num = false;

var self_num = this;

// Mozilla/Safari

if (window.XMLHttpRequest) {

self_num.xmlHttpReq_num = new XMLHttpRequest();

}

// IE

else if (window.ActiveXObject) {

self_num.xmlHttpReq_num = new ActiveXObject("Microsoft.XMLHTTP");

}



self_num.xmlHttpReq_num.open('POST', strURL, true);

self_num.xmlHttpReq_num.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

self_num.xmlHttpReq_num.onreadystatechange = function() {

if (self_num.xmlHttpReq_num.readyState == 4)

{

document.getElementById("user_current_bids").innerHTML=self_num.xmlHttpReq_num.responseText;

}

}

self_num.xmlHttpReq_num.send('');



setTimeout("find_no_bids('ajax_num_bids.php')",2000);

}



setTimeout("find_no_bids('ajax_num_bids.php')",2000);

</script>









<a class="header2right smalla1bww " href="<?=_BASEURL_?>/logout/index.html"><?=_LOGOUT11_?></a></div></div>

<? }?>

<div class="corner_r"></div>

<div class="shadow"></div>

<div id="log_secrh"><div class="log_secrh"><form name="headerSearchForm" id="headerSearchForm" action="<?=_BASEURL_?>/search.php" onSubmit="return searchproduct();" method="get"><input type="text" id="keyword" name="keyword" value="Search..." class="inputtext" onclick="_delete_login_fields(this, 'Search...', this.value);" /></form></div></div>

</div>









<div class="tab_show_effect_sh">

<div class="tab_show_effect">

<div id="toptab_ef">

<ul class="show_tab">

<li class="home"><a href="<?=_BASEURL_?>/">&nbsp;</a></li>

<? if($_SESSION['uid']!=''){?>

<li class="acc"><a href="<?=_BASEURL_?>/myauctions/index.html">&nbsp;</a></li>

<? }

else{?>

<li class="acc"><a href="<?=_BASEURL_?>/login/index.html">&nbsp;</a></li>

<? }?>



<li class="cat"><a href="<?=_BASEURL_?>/allcategories/index.html">&nbsp;</a></li>

<li class="auc"><a href="<?=_BASEURL_?>/seeallauctions/index.html">&nbsp;</a></li>

<li class="how"><a href="<?=_BASEURL_?>/howitworks/index.html">&nbsp;</a></li>

<li class="news"><a href="<?=_BASEURL_?>/newsdetail/index.html">&nbsp;</a></li>

<li class="green"><a href="<?=_BASEURL_?>/faq_buyers.php">&nbsp;</a></li>

<li class="contact"><a href="<?=_BASEURL_?>/contact/index.html">&nbsp;</a></li>

</ul>



</div>

</div>

</div>
<DIV STYLE="position:absolute; marging-top:1px; margin-left:720px; width:239px; height:199px; font-family:Arial; font-size:10pt;"><img src="img/sign.png" width="250" height="199" border="0" usemap="#Map" style="float:right"/>
<map name="Map" id="Map"><area shape="rect" coords="74,116,176,170" href="http://www.my website.com/register/index.html" target="_self" />
</map></DIV>




<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript" src="fadeslideshow.js">

/***********************************************
* Ultimate Fade In Slideshow v2.0- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/

</script>

<script type="text/javascript">

var mygallery=new fadeSlideShow({
wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
dimensions: [707, 199], //width/height of gallery in pixels. Should reflect dimensions of largest image
imagearray: [
["http://www.My website.com/img/1.png", "", "", "My website.com offers a wide range of products. Save up to 98% of RRP prices."],
["http://www.My website.com/img/2.png", "Combined shopping & Gaming.", "_new", "Gear Deals, Small Prices... That is what we do..."],
["http://www.My website.com/img/3.png"],
["http://www.My website.com/img/4.png", "", "", "My website.com offers a wide range of products."] //<--no trailing comma after very last image element!
],
displaymode: {type:'auto', pause:16000, cycles:0, wraparound:false},
persist: false, //remember last viewed slide and recall within same session?
fadeduration: 500, //transition duration (milliseconds)
descreveal: "ondemand",
togglerid: ""
})



</script>

<div id="fadeshow1"></div>

<br />

</div>


Could you please let me know what I have to do to achieve what I am trying to do?


Cheers.

crf121359
05-16-2010, 09:03 PM
Hi again,

I actually forgot to mention that the "sing.png" is what users will see before they logged in and I just want to replace that with "sign2.png" when the users are logged in to their accounts!

Daniel, I do apologize for duplicated posts, this is my first time that I am sending any posts on forums like this and I am not quite clear on how to use this kind of forums. I will need to get the hang of it. :)

Thank you in advance.

Beverleyh
05-17-2010, 07:55 AM
You already seem to be doing the content switch already with your <? if($_SESSION['uid'] ==''){ ?> code so are you saying that what you already have isn't working?

If it is, you can just follow the same principles.

If user id is equal to nothing, show this HTML
<? if($_SESSION['uid'] ==''){ ?>
<!-- any HTML -->
<?php } ?>


If user id is not equal to nothing, show this HTML
<? if($_SESSION['uid'] !=''){ ?>
<!-- any HTML -->
<?php } ?>

crf121359
05-17-2010, 09:03 AM
You already seem to be doing the content switch already with your <? if($_SESSION['uid'] ==''){ ?> code so are you saying that what you already have isn't working?

If it is, you can just follow the same principles.

If user id is equal to nothing, show this HTML
<? if($_SESSION['uid'] ==''){ ?>
<!-- any HTML -->
<?php } ?>


If user id is not equal to nothing, show this HTML
<? if($_SESSION['uid'] !=''){ ?>
<!-- any HTML -->
<?php } ?>


Hi,

Yes, it is working fine but what I am trying to do is that to change the image "Sign UP" button or sign.png once they are logged in as well.... at the moment when the users are logged in, the sign up button is still there...

I just want to know how I can do that in that header.php...

Thank you.
Kind Regards

Beverleyh
05-17-2010, 10:17 AM
Great - then you already have the answer. So as I said before, using the existing code principles already laid out in your current header, just wrap the div that holds your sign.png image in the first part of the if statement and your alternative member image in the else statement;

ie - If user id is equal to nothing, show this HTML, else show alternative HTML

<? if($_SESSION['uid'] ==''){ ?>
<!-- any HTML - non members stuff -->
<?php } else { ?>
<!-- any HTML - members stuff -->
<?php } ?>

It's exactly what I said with the example given in my first post and exactly what is already used in your header file already.


Looking for patterns in working examples, and using trial and (sometimes a lot of) error is the best learning curve available to you.


EDIT -

If you still have difficulty understanding, it might be helpful for you read up on if/else statements, so you better understand the code already on your page.

If statement: http://www.tizag.com/phpT/if.php
If... else: http://www.tizag.com/phpT/ifelse.php
Elseif: http://www.tizag.com/phpT/elseif.php

crf121359
05-17-2010, 11:03 AM
Great - then you already have the answer. So as I said before, using the existing code principles already laid out in your current header, just wrap the div that holds your sign.png image in the first part of the if statement and your alternative member image in the else statement;

ie - If user id is equal to nothing, show this HTML, else show alternative HTML

<? if($_SESSION['uid'] ==''){ ?>
<!-- any HTML - non members stuff -->
<?php } else { ?>
<!-- any HTML - members stuff -->
<?php } ?>

It's exactly what I said with the example given in my first post and exactly what is already used in your header file already.


Looking for patterns in working examples, and using trial and (sometimes a lot of) error is the best learning curve available to you.


EDIT -

If you still have difficulty understanding, it might be helpful for you read up on if/else statements, so you better understand the code already on your page.

If statement: http://www.tizag.com/phpT/if.php
If... else: http://www.tizag.com/phpT/ifelse.php
Elseif: http://www.tizag.com/phpT/elseif.php

Cheers Beverleyh,

I do understand how if, if else etc work! I also tried what you told me which was wrapping the div for sign.png and place it in the first bit for if statement but when I did that it messed up the front page!!!

I removed the div and looked at the front page and it was okay but the image for the else statement still doesn't show up!!!

The problem I have is that the site is a live site with members and it is an auction site and I can't keep messing with it...

otherwise I'm not worried about testing and trying and errors etc...

Thank you
Regards

Beverleyh
05-17-2010, 12:27 PM
So what exactly happened when you say it messed up the front page? Just positioning of other elements that were affected or a total site meltdown?

Placement problems might also be influenced by your javascript files, which are in the body of your page - have you tried putting the ones that need to be in the head section, in the head section? Or in containing divs?

Why not set up a secondary dummy page to run tests on?

crf121359
05-17-2010, 03:59 PM
No, the content of the header went all over the place when I did that!

I now created this code:
<div><img src="img/sign2.png" width="250" height="199" style="position:absolute; marging-top:100px; margin-left:720px; width:239px; height:199px; font-family:Arial; font-size:10pt;"><img src="img/sign2.png" width="250" height="199" border="0" usemap="#Map" style="float:right"/></div>

I placed the above code in the header.php right after <? }else{?>

The image goes right at the top of the page. I change the value of marging-top but it won't move for some reason!!

any idea on what is going on?

Thank you.
Regards

crf121359
05-17-2010, 07:36 PM
Guys,

it works now. God I am pleased...

Thank you for your help guys.