Welcome Guest, Not a member yet? Register   Sign In
How to display corpname first, if no corpname, then username?
#1

In the view pages we have session userdata as follows;

PHP Code:
<?php echo $this->session->userdata('corp_name'); ?><?php echo $this->session->userdata('user_name'); ?>

They both work but we want to first display the corp_name, and if there is no corp logged in then display the user_name.

We have tried the following but nothing appears;

PHP Code:
<?php if(!$this->session->userdata('corp_name')) echo $this->session->userdata('user_name'); ?></div> 

Anybody with any suggestions?
Reply
#2

(This post was last modified: 01-19-2021, 09:22 AM by paulbalandan.)

PHP Code:
<?php echo $this->session->userdata('corp_name') ?: $this->session->userdata('user_name'); ?>

This works on the assumption that both session data returns strings, and if 'corp_name' is not given it will return an empty string.
Reply
#3

(01-19-2021, 09:21 AM)paulbalandan Wrote:
PHP Code:
<?php echo $this->session->userdata('corp_name') ?: $this->session->userdata('user_name'); ?>

This works on the assumption that both session data returns strings, and if 'corp_name' is not given it will return an empty string.

Thanks @paulbalandan, but that doesnt work. The way Im testing is to log in with username then go to corporations and log in with a corpname, then go to an advert page which displays the corpname. That displays the corpname as it did before. But then I log in again with username then go to the advert page and the corpname still displays. We dont want that. We want the username to be displayed. In other words the 2nd part of your coding has no effect.
Reply
#4

Try this

PHP Code:
<?php if ($this->session->userdata('corp_name'))
{
    echo $this->session->userdata('corp_name');
}
else
{
    echo $this->session->userdata('user_name');
}
?>

If that does not work then your not getting or updating the session correctly.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(01-19-2021, 12:19 PM)InsiteFX Wrote: Try this

PHP Code:
<?php if ($this->session->userdata('corp_name'))
{
    echo $this->session->userdata('corp_name');
}
else
{
    echo $this->session->userdata('user_name');
}
?>

If that does not work then your not getting or updating the session correctly.

Thanks @InsiteFX. Your script works but it doesnt work. Firstly I dont yet have a log out feature in our project. If I allow the log in to expire, then log in username, then go direct to advert page it displays the username as expected. And then if I log into a corp & go to the advert it displays the corpname as expected. But if I go back & log into the username again, then to the advert it displays the corpname, probably because the corp has not logged out.

If your script started with a corp log out I think it should work, but I dont know how to do that. We dont want the session destroyed. A user may want to log into more than one corp so each corp login should also start with a log out, but again I dont know how to do that.
Reply
#6

(This post was last modified: 01-20-2021, 04:06 AM by InsiteFX.)

Sounds like it's an update problem with how your updating the names.

I would put in the logout and then set the session names in there to none.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(01-20-2021, 04:05 AM)InsiteFX Wrote: Sounds like it's an update problem with how your updating the names.

I would put in the logout and then set the session names in there to none.

The names are retrieved from the database that already exist. I dont think its an update problem. I think it is a logout problem.

Can you show how to put in the logout & set the session names to none?
Reply
#8

Can you please share the codes where you start the session?
Reply
#9

In the Controller I have had the first line below, which should answer @demyr question.

And I have now inserted the 2nd line below it, and have the coding of @InsiteFX in the view page, and now everything works fine.

PHP Code:
$this->load->library('session');
$this->session->unset_userdata('corp_name'); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB