如何检查对象是否已添加书签?

时间:2019-06-09 13:42:12

标签: php cookies

我正在致力于这个项目的创建,以创建一个房地产网站,人们可以在其中添加书签,以及将其从书签中删除。下面的代码在某种程度上可以正常工作。 首先,我尝试检查属性是否已添加书签,在这种情况下,我希望显示从书签中删除的选项。 在将任何内容添加为书签之前,$ property_id_list当然为空,这就是为什么我收到以下错误消息的原因。

警告:in_array()期望参数2为数组,在第52行的/kunden/587634_20457/00/18/09_Bookmarks/bookmarks.php中给出null。

添加了某些内容后,它会继续按需添加和删除属性。

我已经尽我所能使用此代码,但是我承认我可能在这里遗漏了一些非常明显的东西,因为我两个月前才开始编写代码... 考虑到这一点,我在下面发布了所有代码...

非常感谢您抽出宝贵的时间阅读本书。 任何类型的建议都将有所帮助,即使我必须更改100%的代码。

<?php
//bookmarkrules.php



if(isset($_POST["add_to_bookmark"]))
{
	if(isset($_COOKIE["bookmark"]))
	{// Adds to bookmark
		$cookie_data = stripslashes($_COOKIE['bookmark']);
		$bookmark_data = json_decode($cookie_data, true);
    //will decode JSON string and store under this variable
	}
	else
	{
		$bookmark_data = array();
	}


	$property_id_list = array_column($bookmark_data, 'property_id');
	//checks if the property has already been bookmarked
	if(in_array($_POST["hidden_id"], $property_id_list))
	 {
	  foreach($bookmark_data as $keys => $values)
	  {
	   if($bookmark_data[$keys]["property_id"] == $_POST["hidden_id"])
	   {
      //no doubles
	   }
	  }
	 }
  else{

     //store data into $property_array variable
		$property_array = array(
			'property_id'			=>	$_POST["hidden_id"],
			'property_name'			=>	$_POST["hidden_name"],
			'property_price'		=>	$_POST["hidden_price"],
		);
		$bookmark_data[] = $property_array;
			}
//convert PHP array to JSON string
	$property_data = json_encode($bookmark_data);
	setcookie('bookmark', $property_data, time() + 3600 * 24 * 365, "/");

//data will expire after one day

}



//
if(isset($_GET["action"]))
{
	if($_GET["action"] == "delete")
	{
		$cookie_data = stripslashes($_COOKIE['bookmark']);
		$bookmark_data = json_decode($cookie_data, true);
		foreach($bookmark_data as $keys => $values)//loop
		{
			if($bookmark_data[$keys]['property_id'] == $_GET["id"])
			{
				unset($bookmark_data[$keys]);//destroy the key's array
				$property_data = json_encode($bookmark_data);
				setcookie("bookmark", $property_data, time() + (3600 * 24 * 365),"/");
			}
		}
	}
}




?>
<?php

//bookmarks.php

include('../01_Global/assets/sql/database_connection.php');
include('bookmarkrules.php');
?>
<!DOCTYPE html>
<html>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
	</head>
	<body>
		<br />
		<div class="container">
			<br />
			<h3 align="center">Bookmark using Cookies</h3><br />
			<br /><br />
			<?php
			$query = "SELECT * FROM Property";
			$statement = $connect->prepare($query);
			$statement->execute();
			$result = $statement->fetchAll();
			foreach($result as $row)
			{
			?>
			<div >
				<form method="post">
           <!---Information we want displayed--->
					 <div class="col-sm-4 col-lg-3 col-md-3">
					  <div style="border:1px solid #ccc; border-radius:5px; padding:16px; margin-bottom:16px; height:450px;">
					 	<img src="../01_Global/assets/img/Houses/'. $row['Image'] .'" alt="" class="img-responsive" >
					 	<p align="center"><strong><a href="productpage.php"><?php echo $row["Name"]; ?></a></strong></p>
					 	<h4 style="text-align:center;" class="text-danger" ><?php echo $row["Price"]; ?></h4>
					 	<p>Property Type : <?php echo $row["Property_Type"]; ?><br />
					 	<p>For : <?php echo $row["Listing_Type"]; ?><br />
					 	<p>City : <?php echo $row["City"]; ?><br />
							<!---Values stored under hidden fields--->
							<input type="hidden" name="hidden_name" value="<?php echo $row["Name"]; ?>" />
							<input type="hidden" name="hidden_price" value="<?php echo $row["Price"]; ?>" />
							<input type="hidden" name="hidden_property_type" value="<?php echo $row["Property_Type"]; ?>" />
							<input type="hidden" name="hidden_listing_type" value="<?php echo $row["Listing_Type"]; ?>" />
							<input type="hidden" name="hidden_city" value="<?php echo $row["City"]; ?>" />
							<input type="hidden" name="hidden_id" value="<?php echo $row["PropertyID"]; ?>" />

							 <!--- Add to bookmark button--->
							 <?php
    				 	 $property_id_list = array_column($bookmark_data, 'property_id');


							 $property_id_list = array_column($bookmark_data, 'property_id');
							 if (in_array($row["PropertyID"], $property_id_list)) {
	                 foreach ($bookmark_data as $keys => $values) {
	                     if ($bookmark_data[$keys]["property_id"] == $row["PropertyID"]) {
	                         ?> <a href="bookmarks.php?action=delete&id=<?php echo $values["property_id"]; ?>"><span class="text-danger">Remove</span></a><?php
	                     }
	                 }
	             }
							 else {
	                 ?>
	                 <form>
	                     <input type="submit" name="add_to_bookmark" style="margin-top:5px;"
	                         value="Add to bookmark" />
	                 </form>
	             <?php
	             }
	             ?>
	                     </div>

	                 </div>
	             </form>
	         </div>


	         <?php
	       }
	         ?>

0 个答案:

没有答案