Jump to content

how do i use the $_SESSION code?


jeger003

Recommended Posts

i have a classified site and want to be able to call each users ads......so that when they login they will be able to see what ads they up and what ads have expired........it currently uses sessions and deletes it within 24 hrs i think it is..........how would i use $_SESSION['username'] to display only my users ads specific to session or to user ID.........or is there another way to do it?

Link to comment
https://forums.phpfreaks.com/topic/139166-how-do-i-use-the-_session-code/
Share on other sites

I would use mysql and give each ad a number (starting from 0) and as they see each add, the value goes up one number for that user. So first create a table with a row for each user, a username column and as default for a new user, the value 0 in the second column. Then in another database, have a column which contains all of the ads (one ad per row) and a second column with the row number. If you can set that up that database system on a small scale (or large scale), then use the following code to assign the relevant ad to what the user is viewing.

<? session_start();
$result = mysql_query("SELECT `username`, `ad_id` FROM `userprofiles` WHERE `username`='".$_SESSION['username']."'");
$gets = mysql_fetch_array($result);
$resolt = mysql_query("SELECT `code` FROM `ads_table` WHERE `id`='".$gets['ad_id']."'");
$ad = mysql_fetch_array($resolt);
$newval=$gets['ad_id']
$newval+=1;
mysql_query("UPDATE `username` SET `ad_id`='".$newval."' WHERE `username`='".$_SESSION['username']."'");
unset($result);
unset ($gets);

//echo ad
echo $ad['code'];
?>

The above code shows how to make each user see each ad once but you will need to adjust those mysql querys carefully if you have different column names. The column names I used are:

For userprofiles table:

  • username
  • ad_id

For ads_table table:

  • code
  • id

Hope that helps.

thank you cwarn23 for your help. I used the code and replaced the table infos with my table and i actually recieve a blank page......no errors or anything but a blank page.

 

here is how my tables are setup...

 

i have a table that has stored all the users ads called ads_classified which stores the seller_id, ad_id, and whether an ad is live or not (1 or 0)------this table does not have any usernames but it uses "seller_id" as usernames (which are numbers)

 

and then i have a table called ads_userdata which stores everything that the user registerd with username, email, id (id in this table is the same number as seller_id in the ads_classified table), firstname, lastname

 

finally i have an "ads_sessions" table where it stores the current session once a user logs on....it stores user_id (which is the same number as id, and seller_id), date, classified_session which have things like this bed56693e73af10e8c430ed7cd714c7a, and last_time with numbers like 1230915594.

 

 

i have no clue what all of that means........i wanted to explain to you how its stored and I'm not really sure howto put it all together........i would really appreciate your help

 

i even tried a few times to use the user_id in the ads_session table but no luck

 

thank you soo much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.