Jump to content

[SOLVED] Append to Session Variable


imperium2335

Recommended Posts

Hi guys, I'm trying to make something that remembers what the user has viewed previously (using an image id list that builds as the user rates each new picture).

 

But all its doing is showing the current image id, here is my code:

 

<?PHP

session_start() ;

if (!file_exists("php/dbconnect.php"))
{
die("Database settings not found, administrator intervention required.") ;
}
else
{
require("php/dbconnect.php") ; //Must connect to the database.
}

$_SESSION['idview'] = NULL ;

$countq = ("SELECT image_id FROM image_bank") ; //Count how many cakes are in the base first.

$count = mysql_query($countq) ;

$numrows = mysql_num_rows($count) ;

$image_select = rand(1,$numrows) ; //Assign the max random number based on how many results there are.

$query = "SELECT * FROM image_bank WHERE image_id = $image_select" ;

$result = mysql_query($query) ;

while($row = mysql_fetch_array($result))
{

$dbtitle = $row['title'] ;
$imgsrc = $row['image_url'] ;

if(empty($_SESSION['idview']))
{
	$_SESSION['idview'] = $row['image_id'] ;
}
else
{
	$_SESSION['idview'] .= " " ;
	$_SESSION['idview'] .= $row['image_id'] ; //Add the image id to what the user has viewed.
}


}

echo $_SESSION['idview'] ;

include("php/dbdisconnect.php") ;
?>

 

Thanks in advance!

Link to comment
Share on other sites

[EDIT]

 

Instead of:

 

$countq = ("SELECT image_id FROM image_bank") ; //Count how many cakes are in the base first.

$count = mysql_query($countq) ;

$numrows = mysql_num_rows($count) ;

$image_select = rand(1,$numrows) ; //Assign the max random number based on how many results there are.

 

you can do this which is lighter and faster query:

 

$countq = ("SELECT COUNT(image_id) as imgCount FROM image_bank") ; //Count how many cakes are in the base first.

$count = mysql_query($countq) ;

$numrows = mysql_fetch_assoc($count) ;

$image_select = rand(1,$numrows['imgCount']) ; //Assign the max random number based on how many results there are.

 

[EDIT]

 

Or much better... since what you want to do is get the random image from the database... you can randomly get one using only query nothing more:

 

$query = "SELECT * FROM image_bank ORDER BY RAND() LIMIT 1;" ;

 

With this... you could eliminate a lot of codes with one query and get everything you need. Hope this helps.

 

Jay,

Link to comment
Share on other sites

Thanks Jay, I will try this out tomorrow. My question was more specifically aimed at the bit im trying to make that prevents the user from being shown an image they have already seen/voted for. For that I need to get the image_ids stuck together in the session var, then i can explode them and pass that into something else.

 

Thanks.

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.