Jump to content

[SOLVED] help me please with save listing / save search


work_it_work

Recommended Posts

Hey,

 

I'm trying to develop an website with web announcements and i'm stuck... It's a php based website.

Example of what i want to do:

Visitor go to website, search for an announcement, finds it, then see all the details in the announcement_details_page, on this page is a link "save this announcement" which stores the announcement to that computer if it's clicked. Assuming the visitor save a few announcements. Then, other day he returns to the website, goes to "My saved announcements" page, and there finds the list with of the saved announcements, he may want to visit again or just simply delete one, two or all of them . Also if one announcement is no longer available the link for that particular announcement is no longer active and it displays something like (announcement expired)

 

If the visitor clear his web browser data (history, cookies, other stuff), the "My saved announcements" page will be empty. As far as i can see this is based on a cookie session.

 

Can anyone help me with this? Any already made scripts over the internet, just ready for use? any idea, anyone did this before? please help! It's one of the last things i have to make to the website so i can just put it online.

 

Thanks in advance!!!

Link to comment
Share on other sites

i think it's working... i got something into my cookie :D

 

there was an error here with $a_id and $a_is:

<?php

$seven_days = (60*60*24*7);

$a_id = 1;//this would be your announcement id

setcookie("saved_announcement", $a_is, time()+$seven_days);

 

changed to

 

<?php

$seven_days = (60*60*24*7);

$a_id = 1;//this would be your announcement id

setcookie("saved_announcement", $a_id, time()+$seven_days);

 

now i must figure it out how to get the $a_id from cookie and post it into a my_saved_listing page...

 

any idea on that?

Link to comment
Share on other sites

great!! it's working :) thank you very much for your help trough here.

 

the other problem is that i only can store 1 $a_id in cookie...

example: i have saved first announcement that has $a_id = 25 and in cookie i have the 25 stored.

if i go to other announcement which has $a_id = 3 and click save, the cookies is modified, and i olny have $a_id = 3 instead of both $a_id = 3 and $a_id = 25...

 

any idea how to store multiple $a_id`s in cookie ?

 

 

Link to comment
Share on other sites

Personally I'd build the string like a CSV (comma seperated values)

 

so if you have a saved cookie and want to add another run the following code;

 

<?php
//adding another announcement
$new_a_id = 10;
if(isset($_COOKIE['saved_announcements'])){
$a_ids = explode(",", $_COOKIE['saved_announcements']);
forech($a_isd as $a_id_check) {
	if($new_a_id == trim($a_id_check)) {
		header("Location: a-page-because-that-announcement-is-already-saved.php");
		exit;
	}
}
$new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";
$year = (60*60*24*365);
setcookie("saved_announcement", $new_a_array, time()+$year);
}

Link to comment
Share on other sites

<?php
//adding another announcement
$new_a_id = 10;
$year = (60*60*24*365);
if(isset($_COOKIE['saved_announcements'])){
   $a_ids = explode(",", $_COOKIE['saved_announcements']);
   forech($a_isd as $a_id_check) {
      if($new_a_id == trim($a_id_check)) {
         header("Location: a-page-because-that-announcement-is-already-saved.php");
         exit;
      }
   }
   $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";
   
   setcookie("saved_announcement", $new_a_array, time()+$year);
} else {
setcookie("saved_announcement", $new_a_id, time()+$year);

 

That would be your code for saving so you can replace the old one with that (enabling you to store multiple announcments)

Link to comment
Share on other sites

here is the save_listing.php file content:

 

<?php

//adding another announcement

$new_a_id = $_GET['id'];

$year = (60*60*24*365);

if(isset($_COOKIE['saved_announcements'])){

  $a_ids = explode(",", $_COOKIE['saved_announcements']);

  forech($a_isd as $a_id_check) {

      if($new_a_id == trim($a_id_check)) {

        header("Location: a-page-because-that-announcement-is-already-saved.php");

        exit;

      }

  }

  $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";

 

  setcookie("saved_announcement", $new_a_array, time()+$year);

} else {

setcookie("saved_announcement", $new_a_id, time()+$year);

?>

 

and the url that commands the save_listing page and psot the id number is :

localhost/save_listing.php?id=30 (i've put a value manual just to see it works)

 

now the surprise.....

 

Parse error: syntax error, unexpected T_AS in C:\wamp\www\tataee\tatae\save_listing.php on line 7

 

first time worked fine the first little script... now it doesn't ....

 

 

Link to comment
Share on other sites

ok, change it to this anjd see what prints on the screen;

 

<?php
//adding another announcement
$new_a_id = 10;
$year = (60*60*24*365);
if(isset($_COOKIE['saved_announcements'])){
   $a_ids = explode(",", $_COOKIE['saved_announcements']);
   foreach($a_isd as $a_id_check) {
      if($new_a_id == trim($a_id_check)) {
         header("Location: a-page-because-that-announcement-is-already-saved.php");
         exit;
      }
   }
   $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";
var_dump($new_a_array);
   exit;
   setcookie("saved_announcement", $new_a_array, time()+$year);
} else {
setcookie("saved_announcement", $new_a_id, time()+$year);

Link to comment
Share on other sites

Remember, cookies cannot be accessed till a page reload to show the new version.

 

Also remember to use the domain and path parameters in setcookie also to get accurate results each time. You may try unsetting the original cookie then resetting the new like so:

 

<?php
//adding another announcement
$new_a_id = $_GET['id'];
$year = (60*60*24*365);
if(isset($_COOKIE['saved_announcements'])){
   $a_ids = explode(",", $_COOKIE['saved_announcements']);
   forech($a_isd as $a_id_check) {
      if($new_a_id == trim($a_id_check)) {
         header("Location: a-page-because-that-announcement-is-already-saved.php");
         exit;
      }
   }
   $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";
   setcookie("saved_announcement", $new_a_array, time()-$year);
   setcookie("saved_announcement", $new_a_array, time()+$year);
} else {
setcookie("saved_announcement", $new_a_id, time()+$year);
?>

 

And see if that helps out.

Link to comment
Share on other sites

i get the same results with the last script... it replaces the old id with a new one...

to see the cookie content i use "Temporary Internet Files" folder and open the cookie each time after i execute the url, and i see the progress.

i have no idea about this : "Also remember to use the domain and path parameters in setcookie() also to get accurate results each time."

 

my cookie looks content look like this:

 

AM_language

en

localhost/tataee/tatae/

1536

1947570560

29988765

2083161920

29982730

*

saved_announcement

3333335    ==> this is the row that changes every time when i execute the url with new id...

localhost/tataee/tatae/

1536

4002539648

30056187

1980308448

29982762

*

 

Link to comment
Share on other sites

Have you changed the id that you're sending, because the script checks to see if the id exists, and if it does it wont 're-add' it (you dont want the same announcement saved twice).

 

Remember, cookies cannot be accessed till a page reload to show the new version.

 

Also remember to use the domain and path parameters in setcookie also to get accurate results each time. You may try unsetting the original cookie then resetting the new like so:

 

<?php
//adding another announcement
$new_a_id = $_GET['id'];
$year = (60*60*24*365);
if(isset($_COOKIE['saved_announcements'])){
   $a_ids = explode(",", $_COOKIE['saved_announcements']);
   forech($a_isd as $a_id_check) {
      if($new_a_id == trim($a_id_check)) {
         header("Location: a-page-because-that-announcement-is-already-saved.php");
         exit;
      }
   }
   $new_a_array = $_COOKIE['saved_announcements'].", $new_a_id";
   setcookie("saved_announcement", $new_a_array, time()-$year);
   setcookie("saved_announcement", $new_a_array, time()+$year);
} else {
setcookie("saved_announcement", $new_a_id, time()+$year);
?>

 

And see if that helps out.

 

Those parameters are option, though deleteing and re-assigning the cookie as premsio has done in the code above is a good idea.

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.