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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.