Jump to content

Make Scalar Variable Accessible Across Pages?


chaseman

Recommended Posts

When login in (in login.php), the script is assigning user information to scalar variables like so:

 

$user_name = $assoc['user_name'];
$user_id = $assoc['user_id'];

 

 

Now I'd like to make use of those variables on other pages like this for example:

 

echo "Posted by " . $user_name;

 

I want that to be visible for everybody, even if the user logs out.

 

Any ideas?

Link to comment
Share on other sites

You can use database to store information like that.

 

Using sessions wouldn't work, since the OP wants the information visible by everyone, not just the person logging in.

 

Ken

 

Ahh, I see.  That's strange and highly unlikely to work unless they want to display a list of everyone who is loggedin.

Link to comment
Share on other sites

You can use database to store information like that.

 

Using sessions wouldn't work, since the OP wants the information visible by everyone, not just the person logging in.

 

Ken

Exactly to the second part.

 

And a question to the first part:

 

I've thought of that solution, how would such a solution look like?

 

 

First things first:

- the information is already in the database

- all I need is to select it from the right row in the database

- that can be achieved after login

 

And now?

- would I insert nickname and user_id into a NEW table in the database after fetching it in login.php?

 

Why would I do that when I already have that information in the user table in the database?

 

 

All I need is the query too look like:

 

SELECT * FROM user WHERE user_id = $user_id

 

And I'm fine, the $user_id variable should not be a session, since it gets deleted, and it should not be over GET because it can be changed.

 

 

I'd appreciate if you could elaborate on that solution, because I'm not quite sure how it would look like.

 

 

 

 

Ahh, I see.  That's strange and highly unlikely to work unless they want to display a list of everyone who is loggedin.

 

I just want it to be a message board type thing "Posted by $user_name", if I log out you'll still see my username next to this post.

 

Link to comment
Share on other sites

I solved this problem with a very simple solution, crazy that I didn't think of it before.

 

I was fetching the user contributed data with a while loop, so what I did was, I simply took the user_id from the foreign key in the contribution table and inserted the user_id into the SELECT query and got that way the user information like user_name and avatar path from the user table.

 

Here's a small excerpt to showcase:

 


while ($row = mysqli_fetch_array ($data) or (mysqli_error ($dbc))) {

                $user_id = $row['user_id'];

	$query_user = "SELECT * FROM user WHERE user_id = $user_id";
	$query_run = mysqli_query ($dbc, $query_user) or die (mysqli_error ($dbc));
	$assoc = mysqli_fetch_assoc ($query_run) or die (mysqli_error ($dbc));

	$dbuser_name = $assoc['nickname'];
	$dbuser_avatar = $assoc['avatar'];
/*
---- here comes the table that is being printed with the while loop ---- 
---- printed: user contribution, hidden contribution id, user name, avatar and everything else from two different tables ----

*/

 

Now everything is shown permanently for everybody who visits the page.

 

 

Thanks for all the help.

Link to comment
Share on other sites

The original query is simply a sorting and ordering functionality:

 



// SORTING FUNCTIONALITY - START
// when ALL is set or NONE of the possibilities are set
if (($select_category == 'All') || (!isset($select_category)) && (!isset($sort_date_var))) {

$query = "SELECT * FROM con";

knuffix_list ($query, $user_name, $avatar_path, $dbc);

// when the category is set BUT NOT date
} elseif (isset($select_category) && !isset($sort_date_var)) {

$query = "SELECT * FROM con WHERE category = '$select_category'";

knuffix_list ($query, $user_name, $avatar_path, $dbc);

// when the date is set BUT NOT the category
} elseif (!isset($select_category) && isset($sort_date_var)) {

$query = "SELECT * FROM con ORDER BY contributed_date $sort_date_var";

knuffix_list ($query, $user_name, $avatar_path, $dbc);

// when the category is set AND the date is set
} elseif (isset($select_category) && isset($sort_date_var)) {

$query = "SELECT * FROM con WHERE category = '$select_category' ORDER BY contributed_date $sort_date_var";

knuffix_list ($query, $user_name, $avatar_path, $dbc);
}
// END
[/code]

 

You can remove $user_name and $avatar_path from that list, I don't need those anymore, since I have them inside the function now, they where initially from session variables.

Link to comment
Share on other sites

Looking at your longest query (just modify the others the same way):

 

$query = "
SELECT * FROM con
LEFT JOIN user
ON con.user_id = user.user.id
WHERE category = '$select_category'
ORDER BY contributed_date $sort_date_var";

 

Or, this will probably work in your scenario and is shorter:

 

$query = "
SELECT * FROM con, user
WHERE con.user_id = user.user.id AND category = '$select_category'
ORDER BY contributed_date $sort_date_var";

 

Then all fields from both tables are in $row:

 

while ($row = mysqli_fetch_array ($data) or (mysqli_error ($dbc))) {
   $user_id = $row['user_id'];
   $dbuser_name = $row['nickname'];
   $dbuser_avatar = $row['avatar'];
   //etc..
}

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.