简体   繁体   English

如何使用Cookie实现动态内容的“保存”功能

[英]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. 问题:我将如何实现一项功能,以允许用户将来自(动态创建内容的)SQL中的特定ID添加到cookie变量,以便以后可以重现它。

If I have understood your question correctly, you want a button which marks the related DIV, and stores this relation to cookies. 如果我正确理解了您的问题,则需要一个按钮来标记相关的DIV,并将此关系存储到cookie。

First of all, I would add the id of entry as class to the DIV. 首先,我将条目的ID作为类添加到DIV中。 Then you'll have the possibility to get the id easily via javascript (for further implementations). 然后,您将可以通过javascript轻松获取ID(用于进一步的实现)。

To set the favorites via php, you should add a link to a new php script. 要通过php设置收藏夹,您应该添加指向新php脚本的链接。 This script sets the cookie, via the setcookie function in PHP . 该脚本通过PHP中setcookie函数设置cookie。

In your bookmarked-DIV you would make the same while-loop as above, but check the ids with the values in the cookies. 在带有书签的DIV中,您将进行与上述相同的while循环,但请检查cookie中包含ID的值。 You can access cookies with $_COOKIE . 您可以使用$_COOKIE访问cookie。 Just show items, which are containing in the cookies. 仅显示Cookie中包含的项目。

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. 您可能希望使用PHP将其存储在会话中,而不是使用Javascript。

You could achieve this by simply pushing the ID into a session variable: 您可以通过将ID放入会话变量中来实现此目的:

<?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. 然后,您可以使用AJAX将该脚本与实体的ID关联起来以添加书签,然后将其保存到会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM