创建收藏夹/书签列表

时间:2015-06-01 12:30:25

标签: javascript php jquery ajax json

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

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

以下代码:

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

1 个答案:

答案 0 :(得分:1)

试试这个:

CurrentCulture

因为你每次遇到没有相同链接内容的$ obj时就插入了(所以如果前几个$ obj对象有不同的对象,你会插入相同的喜欢obj直到你遇到了一个内容相同的人)