PHP Logic problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
funsutton
Forum Newbie
Posts: 9
Joined: Sun Jan 18, 2004 12:02 pm

PHP Logic problem

Post by funsutton »

I am suffering from Brain-Block, and can't figure out how to code this. I'll try and keep my explaination short.

What I am trying to do:
I have a weather website and I am trying to display the last 3 searches the visitor has made, using cookies to read in and store the data. I want to make sure that each of the last 3 searches displayed is unique, and not a duplicate. But I can't figure out the logic on how to go from cookie to cookie, writing the last visit in the appropriate cookie. I know this sounds simple, but it doesn't seem that way to me. Can anyone help? BTW, I would show you what I have, but I don't have any code yet to make this work, as I can't figure out where to start.

Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

unless for some reason you're storing a lot of data, you could keep the search data in a single cookie, thus making the bookkeeping easier :)
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

As feyd pointed out, it'd be a simple:

Code: Select all

<?php
$results = array();
$i = 0;
while (count($results) < 3 && isSet($_COOKIE['searches'][$i+1])){
  if(!in_array($_COOKIE['searches'][$i+1]),$results)){
    $results[] = $_COOKIE['searches'][$i+1];
  }
}
echo "<pre>";
print_r($results);
echo "</pre>";
?>
Post Reply