Skip to main content

How to implement a “save” feature with dynamic content using cookies



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.


Comments

  1. If I have understood your question correctly, you want a button which marks the related DIV, and stores this relation to cookies.

    First 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.

    ReplyDelete
  2. You would probably want to use PHP to store it in it's session rather than using Javascript.

    You 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex