...

View Full Version : Resolved discarding IDs (in sessions) for query ?



sitNsmile
04-26-2010, 07:29 PM
Hey,

Whats the best way to build sessions so that I can use it to cancel out the "site_ids" the user has already came from or been too.

with something like this in sql query

AND trade_sites.site_id NOT IN (".$site_ids.")


-this doesnt seem to work the way I thought it would


$sitenumbers = $_SESSION['log_visited'];
if($sitenumbers) { $site_ids = implode(", ", $sitenumbers); }
return $site_ids;


log_visited are set like " , 103, 547, 210, 215 " just throws in all the ids. I need a way to log and track Id's so that it doesnt repeat it in the query. If someone could help me on a good modification that could organize the ids to place in the query using sessions?

Thanks

Fou-Lu
04-26-2010, 08:01 PM
You're treating a string as an array, if the log_visited has the format of " , 103, 547, 210, 215 ", it is a string. You then try to implode it together, which will not work.
Its the right idea, but the log_visited is the incorrect datatype. When you add to it, use $_SESSION['log_visited'][] = 'site_id'; instead of appending with a $_SESSION['log_visited'] .= ', site_id';. This will allow you to use an implode and not worry about the proper formatting of the string (which can become damaged should you start removing from it).

sitNsmile
04-26-2010, 08:13 PM
I tried to do something like this, such as building an array, but for some reason i'm still getting blank when I added in the "NOT IN (siteid) - in query for some reason.

This is what is putting into the session that I want to extract from and build a list of "site_ids"


$plugs_visited = $_SESSION['sites_visited'].",".$SiteId;
$_SESSION['sites_visited'] = $plugs_visited;
$print_v = $_SESSION['sites_visited'];

$split = explode(',', $print_v);

while(list($key,$value) = each($split))
{
$pluggedcontent[]=$value;
}

$pluggedcontent = array_unique($pluggedcontent);
//$clean_array = array_shift($pluggedcontent);

$_SESSION['log_visited'] = $pluggedcontent;

Fou-Lu
04-26-2010, 08:58 PM
I'm not sure why you're using a string at all, or the sites_visited. All you need is the log_visited:


// Adding $SiteId to the log_visited
if (!isset($_SESSION['log_visited']) || !is_array($_SESSION['log_visited']))
{
$_SESSION['log_visited'] = array();
}

if (!in_array($SiteId, $_SESSION['log_visited']))
{
$_SESSION['log_visited'][] = $SiteId;
}

// Then when building your SQL
$currentSQLStuff .= ' AND trade_sites.site_id NOT IN (' . implode(', ', $_SESSION['log_visited']) . ')';

Assuming that log_visited are lists of numbers and trade_sites.site_id is an numerical field (preferably a numerical primary key or unique/indexed field).

sitNsmile
04-26-2010, 09:16 PM
I'm not sure why you're using a string at all, or the sites_visited. All you need is the log_visited:


// Adding $SiteId to the log_visited
if (!isset($_SESSION['log_visited']) || !is_array($_SESSION['log_visited']))
{
$_SESSION['log_visited'] = array();
}

if (!in_array($SiteId, $_SESSION['log_visited']))
{
$_SESSION['log_visited'][] = $SiteId;
}

// Then when building your SQL
$currentSQLStuff .= ' AND trade_sites.site_id NOT IN (' . implode(', ', $_SESSION['log_visited']) . ')';

Assuming that log_visited are lists of numbers and trade_sites.site_id is an numerical field (preferably a numerical primary key or unique/indexed field).

Yeah, Awesome Thank you. That worked perfectly. Once again Fou-Lu!

sitNsmile
04-26-2010, 09:32 PM
Actually question for you Fou-Lu

When I add in


$currentSQLStuff .= ' AND trade_sites.site_id NOT IN (' . implode(', ', $_SESSION['log_visited']) . ')';

in query. and say if session log_visited is empty because that person hasnt gone anywhere. Why would the query come out to null/blank. If I put something in the session, the query works. But need it to work both ways.

any ideas why it would say nothing if the NOT IN is also blank?

- so actually to fix that all I did was

if (!$checkone){$checkone=0;}
but still, is that only quick fix to something like that?

Thanks

Fou-Lu
04-26-2010, 11:11 PM
Simple solution:


if (!empty($_SESSION['log_visited']))
{
// Then when building your SQL
$currentSQLStuff .= ' AND trade_sites.site_id NOT IN (' . implode(', ', $_SESSION['log_visited']) . ')';
}


That way it will only append the AND ... NOT IN (...) if your log_visited has at least one entry.

sitNsmile
04-26-2010, 11:35 PM
Simple solution:


if (!empty($_SESSION['log_visited']))
{
// Then when building your SQL
$currentSQLStuff .= ' AND trade_sites.site_id NOT IN (' . implode(', ', $_SESSION['log_visited']) . ')';
}


That way it will only append the AND ... NOT IN (...) if your log_visited has at least one entry.


ah yea thats true. I thought of that before hand, but I wasn't sure if it was the way I wanted to build the query. Guess cant hurt.

Fou-Lu
04-26-2010, 11:40 PM
Definitely not the only way, but its a pretty easy way ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum