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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...