创建收藏夹/书签列表 [英] Creating favourites/bookmark list

查看:77
本文介绍了创建收藏夹/书签列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在我的网站上创建收藏夹/书签列表,此列表的目的是用户可以双击链接或单击图标将链接设置为收藏夹.我遇到的问题是该链接可以两次附加到列表中.有人可以告诉我我在做什么错吗?

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?

我将对象中的linkID和linkContent保存到文件中,然后读取文件.链接也可以在文件中附加两次.如何停止将重复项保存在文件中?

I'm saving the linkID and linkContent in a object to a file, then reading the file. Also the link can be appended twice in the file. How do I stop duplicates being saved in the file?

下面的代码:

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代码:

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;
    }

推荐答案

尝试一下:

$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));
}

因为它是您每次遇到不具有相同linkcontent的$ obj时都要插入的(因此,如果前两个$ obj对象具有不同的对象,则您将插入相同的收藏夹obj,直到遇到内容相同的一个)

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)

这篇关于创建收藏夹/书签列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆