6 Replies - 666 Views - Last Post: 20 October 2009 - 01:00 PM Rate Topic: -----

#1 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

$_COOKIE to bookmark posts

Post icon  Posted 16 October 2009 - 02:54 PM

hi,

i am trying to figure out how i can allow the user to bookmark a post they like. if they do bookmark a post, then it would need to appear in their bookmark list. i don't want to store this info in my db, so i was thinking of trying something like youtube's quicklist feature, which i'm pretty sure uses cookies.

my basic understanding to achieve this, would be to create a new cookie each time the user clicks the add button.

the problems that i cannot figure out:
- the posts are retrieved and presented in a do...while loop, and each post has a unique postID, then how can i make sure a post that hasn't been added to the user's bookmark list has the add button? (i.e. if a cookie for that post does not exist on the user's computer, then display the add button ELSE do not display the add button)
- how do i show the number of bookmarks in the user's bookmark list? for example, if i have zero in the list, then bookmark list should not be there. but if i have 1 bookmark in the list it should appear as: bookmarks (1). then let's say i add a bookmark, it would be: bookmarks (2), etc.

so far i have something like this, but it doesn't work:
 
<?php
$addbutton = image source code here;
$post = $row_post['post_info'];
$postNotOnList = $addbutton + $post;
 
do {
if ($_COOKIE['postID'] == '$row_post['postID']) {
echo $post;
} else {
echo $postNotOnList;
} while ($row_post = mysql_fetch_assoc)
?>
 


any ideas? thank you in advance for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: $_COOKIE to bookmark posts

#2 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: $_COOKIE to bookmark posts

Posted 18 October 2009 - 03:23 PM

I would definitely store this information in a database, but if you really want to use cookies you should come up with a good naming convention for the cookies, say the post's id is 23 then if the post has been bookmarked a cookie named "Post23" should be found otherwise the post has not been bookmarked, which would be pretty simple and straight forward, right? :)
if(empty($_COOKIE["Post23"]))
{
	// show bookmark button
}

Hope it helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: $_COOKIE to bookmark posts

Posted 19 October 2009 - 09:51 AM

Okay, that definitely makes sense and solves the problem of showing the add sign or not.

How would I retrieve these when the user goes to their bookmark page to view all of their bookmarked items? I'm stuck on this because it seems like with your method of storing the cookies, I might have to go through every postID to check and see if it's there. You probably know much better than me on this one. Maybe a foreach loop? Or do... while? I'll keep playing around with it, thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#4 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: $_COOKIE to bookmark posts

Posted 19 October 2009 - 09:00 PM

View Postninethousandfeet@msn.com, on 19 Oct, 2009 - 08:51 AM, said:

Okay, that definitely makes sense and solves the problem of showing the add sign or not.

How would I retrieve these when the user goes to their bookmark page to view all of their bookmarked items? I'm stuck on this because it seems like with your method of storing the cookies, I might have to go through every postID to check and see if it's there. You probably know much better than me on this one. Maybe a foreach loop? Or do... while? I'll keep playing around with it, thanks for the help!


This is where a database would come in handy, again. You could have a database setup with something of the following:

TABLE: bookmarked_posts

id (auto increment; primary key)
postid
userid


As well as any additional fields you wish to include such as a date timestamp to keep track of when the user bookmarked the post.

When the user bookmarks the post, you would simply run a mysql_query(""); and INSERT a new field into the bookmarks table with the id of the post being bookmarked as postid and the user's id who bookmarked the post as userid.

To retrieve this information, you could simply run a while loop to retrieve the rows for that particular user.

Example:

$userid = $_SESSION["userid"]; // or whatever, get the currently logged in user's id so we can retrieve his/her bookmarked posts from the database

while ($row = mysql_query("SELECT * FROM `bookmarked_posts` WHERE `userid` = '$userid'"))
{
	echo "<a href=\"viewpost.php?id=" . $row["postid"] . "\">View Post #" . $row["postid"] . "</a><br />";
}




Of course, you could store even more information in the bookmarked_posts such as the post title, etc, etc.
Was This Post Helpful? 1
  • +
  • -

#5 ninethousandfeet@msn.com  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 310
  • Joined: 09-February 09

Re: $_COOKIE to bookmark posts

Posted 20 October 2009 - 07:05 AM

it seems like the overall consensus is that using the db is easier and a better choice all around. i'll go ahead with this option, thank you both for the help!
Was This Post Helpful? 0
  • +
  • -

#6 Master Jake  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 106
  • Joined: 27-February 09

Re: $_COOKIE to bookmark posts

Posted 20 October 2009 - 08:54 AM

View Postninethousandfeet@msn.com, on 20 Oct, 2009 - 06:05 AM, said:

it seems like the overall consensus is that using the db is easier and a better choice all around. i'll go ahead with this option, thank you both for the help!


Your welcome. Glad you chose that option. It will really pay off.
Was This Post Helpful? 0
  • +
  • -

#7 Wimpy  Icon User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: $_COOKIE to bookmark posts

Posted 20 October 2009 - 01:00 PM

That's why you should have gone along the way of using a database in the first place as I think I mentioned in my last post! Happy Master_Jake succeeded in getting you to change your mind! :) Almost everything that requires multiple records( for multiple users) should (in my opinion) be stored in a database, it's rarely defensible (again, in my opinion) to store the information in any other way! :)

View Postninethousandfeet@msn.com, on 19 Oct, 2009 - 06:51 PM, said:

Okay, that definitely makes sense and solves the problem of showing the add sign or not.

How would I retrieve these when the user goes to their bookmark page to view all of their bookmarked items? I'm stuck on this because it seems like with your method of storing the cookies, I might have to go through every postID to check and see if it's there. You probably know much better than me on this one. Maybe a foreach loop? Or do... while? I'll keep playing around with it, thanks for the help!

This post has been edited by Wimpy: 20 October 2009 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1