I would like to implement a "save" feature, which allows my users to save a particular entry that I have presented to them from my database. For example, if they see something they like, they can press the "save" button and it will save to a "bookmarks" page.
Here is my code:
<?php
// Get all the data from the example table
$result = mysql_query("SELECT * FROM table WHERE item <> 0 ORDER BY id")
or die(mysql_error());
// keeps getting the next row until there are no more to get
$i = 10;
while ($i > 0) {
$i--;
$row = mysql_fetch_array( $result );
if ($row['id'] != "" ) {
?>
<!-- Some content in this div-->
<div class="content">
<img src="<?php echo $row['logo']; ?>"/>
<?php echo $row['id']; ?>
</div>
<!-- This div will have the save icon that users can press -->
<div class="save">
<img src="images/saveIcon.png" />
</div>
<?php }} ?>
<div class="bookmarked">
Every single div that is labled as "content" that has been saved, will be displayed here.
</div>
Question: How would I implement a feature to allow a user to add the particular ID from SQL (of the dynamically created content) to a cookie variable, so that it can be reproduced at a later time.
If I have understood your question correctly, you want a button which marks the related DIV, and stores this relation to cookies.
ReplyDeleteFirst of all, I would add the id of entry as class to the DIV. Then you'll have the possibility to get the id easily via javascript (for further implementations).
To set the favorites via php, you should add a link to a new php script. This script sets the cookie, via the setcookie function in PHP.
In your bookmarked-DIV you would make the same while-loop as above, but check the ids with the values in the cookies. You can access cookies with $_COOKIE. Just show items, which are containing in the cookies.
This is a possible way of implementation.
You would probably want to use PHP to store it in it's session rather than using Javascript.
ReplyDeleteYou could achieve this by simply pushing the ID into a session variable:
<?php
session_start();
if(isset($_GET['id']) && is_numeric($_GET['id'])){
$_SESSION['bookmarks'][] = intval($_GET['id']);
}
Using AJAX you could then hit this script with an the ID of the entity to bookmark and it'll get saved into the session.