Page 1 of 1

calling a specific image based on a search

Posted: Thu Jan 12, 2006 8:18 am
by okanem
I have a simple sql database with items in it. Each item has its unique identifier 'itemno' column

If I search for a specific item via search.html the result gets processed via results.php.

On the results page, I would like to be able to show an image of the item / items that match the search criteria.

The images can be stored in a folder within htdocs and named 1.jpg, 2.jpg etc to concur with itemno 1. itemno 2 etc.

Apologies for the question but I am only on day 3 of php learning but after extensive searching I am at a dead end.

I hope you can help and thanks in advance
Okanem

Posted: Thu Jan 12, 2006 8:48 am
by feyd
what are you asking?

Posted: Thu Jan 12, 2006 8:54 am
by okanem
I am asking for the code needed to select an image from a folder based on search criteria.

i.e. if a user enters 1 in the search.html form and clicks submit, I want results.php to choose the appropriate image for item 1 and display it along with the rest of Item 1's text description.

The text results are working fine, I just don't know how to return 1.jpg

Sorry if its not worded very well

Okanem

Posted: Thu Jan 12, 2006 8:55 am
by JayBird
do you have any code at all, or are you asking us to write it for you?

Posted: Thu Jan 12, 2006 9:04 am
by okanem
I have the code for the search.html page

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>CWS Database</title>
<style type="text/css">
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
.style1 {
	font-size: 24px;
	font-weight: bold;
	color: #FFFFFF;
}
-->
</style></head>

<body>
<table width="100%" height="100%"  border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="top"><p>&nbsp;</p>
      <table width="450" border="0" cellpadding="0" cellspacing="0" bgcolor="#000066">
        <tr>
          <td><div align="center" class="style1">CWS SEARCH PAGE </div></td>
        </tr>
      </table>      
      <br>
      <br>
      <table width="450" border="0" cellspacing="0" cellpadding="0">
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search for an Item</div></td>
          <td width="225" height="60"><form name="form1" method="post" action="itemsearchresults.php">
            <div align="left">
              <input name="searchterm" type="text" id="searchterm">
              <input type="submit" value="Search">
            </div>
          </form></td>
        </tr>
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search for an NSN </div></td>
          <td width="225" height="60"><form name="form1" method="post" action="nsnsearchresults.php">
            <div align="left">
              <input name="searchterm" type="text" id="searchterm">
              <input type="submit" value="Search">
            </div>
          </form></td>
        </tr>
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search for a G' Spec Code </div></td>
          <td width="225" height="60"><form name="form1" method="post" action="gssearchresults.php">
            <div align="left">
              <input name="searchterm" type="text" id="searchterm">
              <input type="submit" value="Search">
            </div>
          </form></td>
        </tr>
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search for a Pattern Code </div></td>
          <td width="225" height="60"><div align="left"></div></td>
        </tr>
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search Item Descriptions </div></td>
          <td width="225" height="60"><form name="form1" method="post" action="itemdescsearchresults.php">
            <div align="left">
              <input name="searchterm" type="text" id="searchterm">
              <input type="submit" value="Search">
            </div>
          </form></td>
        </tr>
        <tr align="center" valign="top">
          <td width="225" height="60"><div align="left">Search BOM's </div></td>
          <td width="225" height="60"><div align="left"></div></td>
        </tr>
      </table>      
      <table width="200" border="1" cellspacing="0" cellpadding="0">
        <tr>
          <td align="center" valign="middle">
		  Page Views - <?php 
     
    $filename = "count_file.txt"; 
    echo $data = (file_exists($filename)) ? file_get_contents($filename)+1 : 1; 
    $file = fopen($filename,w); fwrite($file, $data); fclose($file); 

		  ?> 
		  &nbsp;</td>
        </tr>
      </table>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
</html>

ALSO I have the code for the results.php page

*************************************

Code: Select all

<html>
<head>
  <title>CWS Item Search Results</title>
</head>
<body>
<h1>CWS Item Search Results</h1>
<?php
  // create short variable names
  $searchterm=$_POST['searchterm'];

  $searchterm= trim($searchterm);

  if (!$searchterm)
  {
     echo 'You have not entered search details.  Please go back and try again.';
     exit;
  }
  
  if (!get_magic_quotes_gpc())
  {
    $searchterm = addslashes($searchterm);
  }

  @ $db = new mysqli('localhost', '***', '***', '***');

  if (mysqli_connect_errno()) 
  {
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }

  $query = "select * from items where ".itemno." like '%".$searchterm."%'";
  $result = $db->query($query);

  $num_results = $result->num_rows;

  echo '<p>Number of items found: '.$num_results.'</p>';

  for ($i=0; $i <$num_results; $i++)
  {
     $row = $result->fetch_assoc();
     echo '<p><strong>'.($i+1).'. Item No: ';
     echo htmlspecialchars(stripslashes($row['itemno']));
     echo '</strong><br />Item Description: ';
     echo stripslashes($row['itemdesc']);
     echo '<br />Garment Spec: ';
     echo stripslashes($row['gscode']);
     echo '<br />NSN Range: ';
     echo stripslashes($row['nsnrange']);
     echo '</p>';
  }


		
  
  $result->free();
  $db->close();



?>
<a href="newitem.html">Back to add another Item</a></body><br>
<a href="search.html">Search for an item</a></body><br>
<a href="index.html">Back to index</a></body><br>
</body>
</html>
***************************************************

If you search for Item 1 using the search page, the results page links to the mysql db cws and returns the Item No ; Item Description ; Garment Spec ; NSN Range from the Items table.

Based on the user choosing 1 as the variable searchterm and clicking the search button, I want the results page to also return a .jpg of Item 1 to the browser. The .jpgs are stored in a folder within htdocs.

I need code which I can insert into results.php to return the .jpg

Okanem

Posted: Thu Jan 12, 2006 9:46 am
by pickle
Expand your `items` table to contain the filename associated with the entry. Then, when you're displaying the results, you can use that filename in an <img> tag to display the image.

Posted: Thu Jan 12, 2006 10:08 am
by okanem
Thanks pickle -

I have added a column to the items table called image

I have put 1.jpg in the items table under the image column against item 1 and so forth

I really have no idea how to incorporate the 1.jpg filename in an <img> tag within my results.php code.

Please assume the 1.jpg is in the root dir along with the html and php files.

Can someone show me the code to call the file 1.jpg.

Okanem

Posted: Thu Jan 12, 2006 10:13 am
by shiznatix
the code would look like this

Code: Select all

echo '<img src="'.$row['image'].'">';
place that where you are looping through your database results.

Posted: Thu Jan 12, 2006 10:35 am
by okanem
Thanks for the help guys / girls

Okanem