chaseman Posted February 22, 2011 Share Posted February 22, 2011 I'm showcasing user avatars and nicknames on the page, the mistake I did was I used session variables for that which are being set after login, now obviously those will be deleted as soon as the session is over. So I thought of inserting the values into scalar variables instead of session variables. The problem I'm encountering is, how to make those variables available at the necessary spots on other pages? Let's say I fetch data off the database after login inside login.php and I put the data into scalar variables, what would be common practice to make those scalar variables accessible in index.php? Is this solution a good solution at all to make the avatar and nickname of the user permanent visible? What would be common practice? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/ Share on other sites More sharing options...
cunoodle2 Posted February 22, 2011 Share Posted February 22, 2011 How often would you say this list changes? Once per hour/day/week? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178263 Share on other sites More sharing options...
ChemicalBliss Posted February 22, 2011 Share Posted February 22, 2011 From your post here is what I think I understand: You have a page on which you only display Avatars with nicknames of the members that have logged in. This is not possible with sessions as each session is unique to each user so you will only ever see your own avatar. If you meant: Geenrally showing avatars of members on any page at any time, it depends on where the avatar data is stored, the url or the image path, and the nickname, if it's in a database it should be very simple, and even if not - storing the data in a file is easy too but tbh i don't know exactly what you're asking. Is this for some sort of message board or something? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178264 Share on other sites More sharing options...
chaseman Posted February 22, 2011 Author Share Posted February 22, 2011 Sorry for being so unclear, I will break it down in a process, I was confusing your guys with sessions and logins, here is it simplified. - user logs in - user does contribution per input - the contribution gets listed on a page together with user name, user avatar, and created date - the contribution with username, avatar is visible for every body and permanently. The information is all being stored in the database. Of course the user has to login FIRST so the script knows which user it is. Similar to a message board. The question, what is common practice to achieve this? I thought of putting the information into scalar variables similar to this: $avatar = $assoc['avatar']; $username = $assoc['user_name']; But then again, is this really common practice, and how would I make those scalar variables availible across pages. For example on the list with the contributions and the profile pages and on all other pages where I would need those information.? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178269 Share on other sites More sharing options...
ChemicalBliss Posted February 22, 2011 Share Posted February 22, 2011 Well now that's much more clear . Ok so for such a basic script you would move the mysql portion to each page, or make it a function that each page has accses to (via an include perhaps). This is much easier imo with objects but to keep it simple, I would make a file named mysql.func.php, this will hold the functions to connect/query and any other specific code you dont want repeated, like a select statement for your contributions table. Then each page, at the start will "include('mysql.func.php');", Then you can use any mysql on any page as long as it includes all the mysql functions from this file. You could also make the mysql.func.php auto-connect when it's included (basically a file that connects to mysql and provides some functions that do some erpeated tasks). hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178271 Share on other sites More sharing options...
chaseman Posted February 22, 2011 Author Share Posted February 22, 2011 Thanks a lot, that makes a lot of sense, I will try that out. So my idea of scalar variables was the right bet, the only thing missing was the use of functions. Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178274 Share on other sites More sharing options...
chaseman Posted February 22, 2011 Author Share Posted February 22, 2011 I tired your suggested approach and unfortunately I didn't get it to work. Here's the script which I use inside a function: $query_user = "SELECT * FROM user WHERE nickname = '$user_name'"; $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']; The problem I'm having: in $query_user the variable $user_name takes the user_name from a session variable which is being set after login (somehow I have to know which user it is), and that is exactly the problem. Since the script is being re-run every time it is called it will not work as soon as the user DOES NOT login, because there's no session set. And when there's no session, there's no user_name to insert into the variable and everything else will not work from there on. The very first step that gets this chain started is the session variable (to know which user it is), if that first step is missing, the chain won't work. What is it that I'm missing here? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178289 Share on other sites More sharing options...
chaseman Posted February 22, 2011 Author Share Posted February 22, 2011 Right in the login.php script when I'm fetching the information off the database to check if the entered password and username match, I could take those information and assign it to scalar variables (instead of sessions). BUT then I'm at my initial problem again, how would I make those scalar variables accessible on other pages so I can make use of them in a function? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178339 Share on other sites More sharing options...
ChemicalBliss Posted February 23, 2011 Share Posted February 23, 2011 Here's an example mysql.func.php : one moment... Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178759 Share on other sites More sharing options...
ChemicalBliss Posted February 23, 2011 Share Posted February 23, 2011 Ok so i did write an ellaborate description of this but im really p***** off because of the rediculously short edit time period and the fact that returning to the previous page loses your form data so i cant get it back im not writing it again here is the code i hope you get it from this alone. <?php $mysql_conn = mysql_connect("host","user","pass"); mysql_select_db("database"); // returns an array of avatars and usernames. $c_user is "current user", so that it wont giev your own avatar. // $mysql_c is the connection handler held in $mysql_conn function get_avatar_array($mysql_c, $c_user=null){ $query = "SELECT `avatar_url`,`username` FROM `user_table`"; if($c_user != null){ $query .= " WHERE `username`='".mysql_real_escape_string($c_user,$mysql_c)."'"; } $result = mysql_query($query, $mysql_c); // make array to return $r_array = array(); while($row = mysql_fetch_assoc($result)){ $r_array[$row['username']] = $row['avatar_url']; } // return result return $r_array; } ?> hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178767 Share on other sites More sharing options...
chaseman Posted February 23, 2011 Author Share Posted February 23, 2011 Thanks for the post, I have only one question to your script you posted. How do you define $c_user and $mysql_c? How do you tell $c_user, which user is the current user? Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178774 Share on other sites More sharing options...
ChemicalBliss Posted February 23, 2011 Share Posted February 23, 2011 That was explained.. unfortunately.. in the lost post. here is an example of using the function: <?php include("myqsl.func.php"); // make username for testing purposes $username = "someuser"; // with a user logged in $result = get_avatar_array($mysql_conn, $username); // $mysql_conn is created when we include the mysql file // without $result = get_avatar_array($mysql_conn); // using session $result = get_avatar_array($mysql_conn, $_SESSION['username']); // debug the result to a human-readable format print_r($result); ?> hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178799 Share on other sites More sharing options...
chaseman Posted February 23, 2011 Author Share Posted February 23, 2011 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, special thanks to you ChemicalBliss. Quote Link to comment https://forums.phpfreaks.com/topic/228514-common-practice-to-make-avatar-and-nickname-permanently-visible/#findComment-1178853 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.