简体   繁体   English

创建收藏夹/书签列表

[英]Creating favourites/bookmark list

I'm currently trying to create a favourites/bookmark list on my website, the aim of this list is that a user can double click on a link or click a icon to set the link as a favourite. 我目前正在尝试在我的网站上创建收藏夹/书签列表,此列表的目的是用户可以双击链接或单击图标将链接设置为收藏夹。 The problem I'm having is that the link can be appended twice to the list. 我遇到的问题是该链接可以追加两次到列表中。 Could someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

I'm saving the linkID and linkContent in a object to a file, then reading the file. 我将对象中的linkID和linkContent保存到文件中,然后读取文件。 Also the link can be appended twice in the file. 链接也可以在文件中附加两次。 How do I stop duplicates being saved in the file? 如何停止将重复项保存在文件中?

Code below: 代码如下:

 function SaveToFavouriteLinkFile(linkID, linkContent) { var saveFavouriteLinkObject = {}; saveFavouriteLinkObject.linkID = linkID; saveFavouriteLinkObject.linkContent = linkContent; // Writing $.ajax({ global: false, type: "POST", cache: false, dataType: "json", data: ({ action: 'write', content: saveFavouriteLinkObject }), url: 'php/saveFavouriteLinks.php', success: function(data) { console.log(data); }, error: function(data) { alert(JSON.stringify(data)); } }); } $(document).dblclick(function(e) { switch (e.target.innerText) { case " Server": $("#favouritesList").append("<li><a id='serverBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw'></i> Server <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("serverBTNFav", "Server"); break; case " Group": $("#favouritesList").append("<li><a id='groupBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> Group <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("groupBTNFav", "Group"); break; case " User": $("#favouritesList").append("<li><a id='userBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> User <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("userBTNFav", "User"); break; case " Sync": $("#favouritesList").append("<li><a id='syncBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> Sync <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("syncBTNFav", "Sync"); break; case " Patient Listing": $("#favouritesList").append("<li><a id='PatientListBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> Patient Listing <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("PatientListBTNFav", "Patient Listing"); break; case " App Settings": $("#favouritesList").append("<li><a id='AppSettingsBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> App Settings <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("AppSettingsBTNFav", "App Settings"); break; case " Logging": $("#favouritesList").append("<li><a id='LoggingBTNFav' class='selectNavigationBTN'><i class='fa fa-dashboard fa-fw selectNavigationBTN'></i> Logging <i class='glyphicon glyphicon-star pull-right'></i></a></li>"); SaveToFavouriteLinkFile("LoggingBTNFav", "Logging"); break; } }); 

PHP Code: PHP代码:

 switch ($_REQUEST['action']) { case 'write': // New favourites item for list $favouriteLink = $_REQUEST['content']['linkContent']; // New favourites item ID $favouriteLinkID = $_REQUEST['content']['linkID']; // File for item to be stored $file = "favouriteLinks.txt"; // Existing items in favourites list $json = json_decode(file_get_contents($file), true); $result = getArrayIndex($json, $favouriteLink); if (empty($result)) { echo json_encode("I did not find your string: ".$favouriteLink); } else { echo json_encode("The index of your main array, where '".$favouriteLink. "' found is: ".$result); } foreach ($json as $obj) { if ($obj['favouriteLinkContent'] == $favouriteLink) { echo json_encode("HELLO"); break; } else { $json[] = array("favouriteLinkContent" => $favouriteLink, "favouriteLinkID" => $favouriteLinkID); file_put_contents($file, json_encode($json)); } } break; case 'read': $data = file_get_contents('favouriteLinks.txt'); echo $data; break; } 

try this: 尝试这个:

$linkexists = false;    
foreach ($json as $obj) {
   if ($obj['favouriteLinkContent'] == $favouriteLink) {
    echo json_encode("HELLO");
    $linkexists = true;
   }

}

if(!$linkexists){
   $json[] = array("favouriteLinkContent" => $favouriteLink,
                   "favouriteLinkID" => $favouriteLinkID);
   file_put_contents($file, json_encode($json));
}

As it was you were inserting every time you ran into an $obj that didn't have the same linkcontents (so if the first couple of $obj objects had a different one you would insert the same favorite obj until you ran into one that had the same content) 因为这是您每次遇到不具有相同linkcontent的$ obj时都要插入的内容(因此,如果前两个$ obj对象具有不同的对象,则将插入相同的收藏夹obj,直到遇到具有相同的内容)

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

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