Jump to content

Searching function - how to display the selected result on its own from the returned list?


Charwil

Recommended Posts

Hello, I have been developing a website that needs to display products online, I was originally going to hand-write the PHP code but I thought it would be more beneficial to make it database orientated as new products can easily be added and a search function becomes possible.

I have it working so far so good, but the last part I can't seem to get working. I have it so all the products that have the desired category are displayed, but I want a more detailed analysis of the product to appear when the image is clicked, which at the moment, displays a blank page.

http://www.webwib.co.uk/JustJigsaws/products.php?p=floorpuzzles - this is the hand coded version that demonstrates how I want it to work.

http://www.webwib.co.uk/JustJigsaws/db.php?floorpuzzles - this is the db version so far, it works how it is intended to apart from the last stage, which returns as a blank page rather than a dedicated page for the selected product.

Here is my code, the third segment beginning with "if(isset($_GET['$ID'])){", is my attempt at the final stage.

<?php
	$db=mysql_connect  ("localhost", "webwibco_charlie",  "Hello123") or die ('I cannot connect to the database  because: ' . mysql_error()); 
	$mydb=mysql_select_db("webwibco_products"); 

	include("header.php");

	if(isset($_GET['floorpuzzles'])){ 
		     echo "<h1>Our Floor Puzzles</h1>";
		$sql="SELECT  ID, Name, Tags, Description, Category FROM products WHERE Category LIKE '%" . FloorPuzzles .  "%'"; 
		$result=mysql_query($sql); 
		while($row=mysql_fetch_array($result)){ 
			$Name  =$row['Name'];
			$ID  =$row['ID']; 
			$Description  =$row['Description'];  
			echo "<div class=\"box\">";
			echo "<h1>$Name</h1>";
			echo "<div class=\"floorbox\"><a href=\"?$ID#productspage\"><img src=\"images/products/catalogue/$ID.jpg\" class=\"small\"></a></div>";
			echo "<h2>$Description</h2>";
			echo "</div>";
		}
	}

	if(isset($_GET['anothercategory'])){ 
		$sql="SELECT  ID, Name, Tags, Description, Category FROM products WHERE Category LIKE '%" . AnotherCategory .  "%'"; 
		$result=mysql_query($sql); 
		while($row=mysql_fetch_array($result)){ 
			$Name  =$row['Name'];
			$ID  =$row['ID']; 
			$Description  =$row['Description'];  
			echo "<h1>Our Floor Puzzles</h1>";
			echo "<div class=\"box\">";
			echo "<h1>$Name</h1>";
			echo "<div class=\"floorbox\"><a href=\"?$ID0#productspage\"><img src=\"images/products/catalogue//$ID.jpg\" class=\"small\"></a></div>";
			echo "<h2>$Description</h2>";
			echo "</div>";
		}
	}

	if(isset($_GET['$ID'])){   
		$sql="SELECT  ID, Name, Tags, Description, Peices, Size, Barcode, Category FROM products WHERE ID LIKE '%" . $ID .  "%'";   
		$result=mysql_query($sql);   
		while($row=mysql_fetch_array($result)){  
			$Name  =$row['Name'];
			$ID  =$row['ID']; 
			$Description  =$row['Description']; 
			$Pieces  =$row['Pieces']; 
			$Size =$row['Size']; 
			$Barcode  =$row['Barcode'];   
			echo "<div class=\"1\">";
			echo "<h1>$Name</h1>";
			echo "<div class=\"bigbox\">";
			echo "<div class=\"floorbox\"><img src=\"images/products/catalogue/$ID.jpg\" class=\"big\"></div>";
			echo "</div>";
			echo "</div>";
			echo "<div class=\"2\">";
			echo "<p>Puzzle Pieces: $Pieces</p>
				  <p>Puzzle Size: $Size</p>
				  <p>Barcode $Barcode</p>";
			echo "</div>"; 
		}  
	}  

include("footer.php");
?>

Thank you in advance for any assistance given.

 

Link to comment
Share on other sites

Thank you for your quick response. I have attempted to implement your suggested solution, and it appears to be a step in the right direction. However, when navigating to the individual product page, I get this error:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in/home/webwibco/public_html/JustJigsaws/search.php on line 31

 

This is line 31:

while($row=mysql_fetch_array($result)){  

I also noticed that it only displays the error when the URL extension is just "?JJ080" and still displays a blank page when it is "?p=JJ080".

 

Here is my modified code after your solution:

<?php
	$db=mysql_connect  ("localhost", "webwibco_charlie",  "Hello123") or die ('I cannot connect to the database  because: ' . mysql_error()); 
	$mydb=mysql_select_db("webwibco_products"); 

	include("header.php");

	$status = htmlspecialchars( @$_GET ['p'] );

	if ($status == "floorpuzzles") {

		     echo "<h1>Our Floor Puzzles</h1>";
		$sql="SELECT  ID, Name, Tags, Description, Category FROM products WHERE Category LIKE '%" . FloorPuzzles .  "%'"; 
		$result=mysql_query($sql); 
		while($row=mysql_fetch_array($result)){ 
			$Name  =$row['Name'];
			$ID  =$row['ID']; 
			$Description  =$row['Description'];  
			echo "<div class=\"box\">";
			echo "<h1>$Name</h1>";
			echo "<div class=\"floorbox\"><a href=\"?$ID#productspage\"><img src=\"images/products/catalogue/big/floorpuzzles/$ID.jpg\" class=\"small\"></a></div>";
			echo "<h2>$Description</h2>";
			echo "</div>";
		}
}else{



	if ($status == "$ID") {
		$sql="SELECT  ID, Name, Tags, Description, Peices, Size, Barcode, Category FROM products WHERE ID LIKE '%" . @$_GET['p'] .  "%'";   
		$result=mysql_query($sql);   
		while($row=mysql_fetch_array($result)){  
			$Name  =$row['Name'];
			$ID  =$row['ID']; 
			$Description  =$row['Description']; 
			$Pieces  =$row['Pieces']; 
			$Size =$row['Size']; 
			$Barcode  =$row['Barcode'];   
			echo "<div class=\"1\">";
			echo "<h1>$Name</h1>";
			echo "<div class=\"bigbox\">";
			echo "<div class=\"floorbox\"><img src=\"images/products/catalogue/big/floorpuzzles/$ID.jpg\" class=\"big\"></div>";
			echo "</div>";
			echo "</div>";
			echo "<div class=\"2\">";
			echo "<p>Puzzle Pieces: $Pieces</p>
				  <p>Puzzle Size: $Size</p>
				  <p>Barcode $Barcode</p>";
			echo "</div>"; 
		}  
	}  
}

include("footer.php");
?>

I apologize if this is a quick fix I have missed, but I am a noob at this server side stuff, I fully understand what you're getting at, but not so much on how to execute it correctly.

 

Thank you again for the quick response, and thanks in advance for any further assistance.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.