Jump to content


Photo

Please help.. Modifying search function


  • Please log in to reply
3 replies to this topic

#1 dannel77

dannel77
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 21 July 2006 - 01:01 PM

Hi!
Could someone please help me to modify this code so it'll be possible to search by articlenumber (code_no) on my homepage. Right now the search function works fine, but only when searching for "item", "maingroup" or "secondgroup".

<?php
// required variables
require("config.php");

$chose_allowed = array('item', 'maingroup', 'secondgroup');
$chose = isset($_GET['chose']) && in_array($_GET['chose'], $chose_allowed) ? $_GET['chose'] : $chose_allowed[0];

$searchstring = isset($_GET['searchstring']) ? mysql_escape_string($_GET['searchstring']) : '';

?>

Have tried to put "code_no" in the $chose_allowed row (instead of 'item') and then it works, but then it's no longer possible to search for item name.. :(



#2 Vikas Jayna

Vikas Jayna
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationNoida, Delhi, India

Posted 21 July 2006 - 03:40 PM

You need to add another element to the array rather than replacing it with 'item' like this:
$chose_allowed = array('item', 'maingroup', 'secondgroup','code_no');

But this is only one side of the coin. You need to ensure that the querystring to this script contains 'chose=code_no' otherwise the script will pick up '$chose_allowed[0]' which is 'item'. This is probably why it worked when 'item' was replaced with 'code_no'
Vikas Jayna,
Project Manager - Jeevansathi.com
7 yrs. exp. in LAMP
Certified Mysql DBA
Zend Certified Engineer

#3 dannel77

dannel77
  • Members
  • PipPip
  • Member
  • 22 posts

Posted 22 July 2006 - 12:29 AM

thx for the tip, if you could please take a look at the code of the page and tell me where this 'chose=code_no' should be put in I'd be really grateful. I'm not sure where the "querystring" is.. The whole page (search_list.php) looks like this:

<?php
// required variables
require("config.php");

$chose_allowed = array('item', 'maingroup', 'secondgroup');
$chose = isset($_GET['chose']) && in_array($_GET['chose'], $chose_allowed) ? $_GET['chose'] : $chose_allowed[1];

$searchstring = isset($_GET['searchstring']) ? mysql_escape_string($_GET['searchstring']) : '';


?>

<html>
<head>

<title>Search List</title>

<link rel="stylesheet" HREF="master_style.css">
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#000000" VLINK="#000000" ALINK="#F70404">

<FORM NAME="itemsform">

<CENTER>
<font style="font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;">
<?php echo $txt_search_result ?> "<?php echo $searchstring ?>"</FONT>

<HR WIDTH=95% size=1 COLOR="#000000">


<center>
<?php

// database connection
mysql_connect("$host","$user","$pass");

// database selection
mysql_select_db("$database");

// Nu bestämmer vi antal per sida och kollar upp totala antalet
$limit = 40; // Antal per sida
$where = "$chose LIKE '%$searchstring%'";

$result = @mysql_query("SELECT COUNT(*) FROM products WHERE $where")
or die("Error fetching number in DB<br>".mysql_error());
$numrows = @mysql_result($result, 0); // Antal i databasen

// Sedan kollar vi om startvariabeln är satt
$start = isset($_GET['start']) && intval($_GET['start']) >= 0 ? intval($_GET['start']) : 0;

// Då räknar vi ut hur många sidor det blev
$pages = intval($numrows/$limit);
if ($numrows % $limit) {
  $pages++;
}

$numlink = '';

// Hämta länk till föregående sida
if ($start > 0) {

  $numlink .= '<a href="?start='.($start - $limit).'&amp;searchstring='.$searchstring.'">&laquo;&nbsp;</a> ';
} else {
  $numlink .= '&laquo;&nbsp; ';
}

// Hämta sidonummer
for ($i = 1; $i <= $pages; $i++) {
  $newoffset = $limit*($i-1);
  if ($start == $newoffset)
    $numlink .= '<font style="color: #000000;" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;"><b>'.$i.'</b></font> ';
  else
    $numlink .= '<a href="?start='.$newoffset.'&amp;searchstring='.$searchstring.'"><font style="color: #2B65EC;" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px;"><u>'.$i.'</u></font></a> ';
}

// Hämta länk till nästa sida
if ($numrows > ($start + $limit))
  $numlink .= '<a href="?start='.($start + $limit).'&amp;searchstring='.$searchstring.'">&nbsp;&raquo;</a> ';
else
  $numlink .= '&nbsp;&raquo; ';

$numlink = '
<div style="font-size: 11px; font-family: Verdana, Arial, Helvetica, sans-serif; padding: 5px 0;">
  '.$numlink.'
</div>
';

// Skriv ut sidorna
echo $numlink;

?>
<table cellpadding="2" cellspacing="1" border="1" frame="border" RULES="NONE" borderstyle="color: #D5E2EC;">
<TR><TD BGCOLOR="#D5E2EC">
<TD BGCOLOR="#D5E2EC" ALIGN="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b>&nbsp;Art nr.&nbsp;</b></FONT></TD>
<TD BGCOLOR="#D5E2EC" ALIGN="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b>&nbsp;Titel&nbsp;</b></FONT></TD>
<TD BGCOLOR="#D5E2EC" ALIGN="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b>&nbsp;Pris&nbsp;SEK&nbsp;</b></FONT></TD>
<TD BGCOLOR="#D5E2EC"></TD></TR>
<?php
// Nu kan vi hämta datan från databasen och skriva ut den
$result = @mysql_query("SELECT * FROM products WHERE $where ORDER BY item ASC LIMIT $start, $limit")
or die("Error fetching data<br>".mysql_error());

while ($row = @mysql_fetch_array($result)){
$color = ($coloralternator++ %2 ? "D5E2EC" : "EBF1F5");
echo "<TR BGCOLOR=\"#$color\"><TD ALIGN=\"middle\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\"><IMG SRC=\"thumbs/$row[6]\" BORDER=\"0\"></a></TD>";
echo "<TD ALIGN=\"left\"><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">&nbsp;$row[2]&nbsp;</FONT></TD>";
echo "<TD ALIGN=\"left\"></a><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"><a href=\"item_show.php?code_no=$row[2]\" TARGET=\"main\">&nbsp;$row[3]&nbsp;</FONT></TD>";
echo "<TD ALIGN=\"right\"></a><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\">&nbsp;$row[5]&nbsp;</font></TD>";
echo "<TD ALIGN=\"middle\">&nbsp;<INPUT TYPE=\"hidden\" NAME=\"id$row[2]quant\" VALUE=\"1\">
<a href=\"Javascript:top.center.cart.addItem('$row[3]','$row[5]', document.itemsform.id$row[2]quant.value, '$row[2]', '$row[7]')\"><img src=\"images/buy.gif\" border=\"0\"></a>&nbsp;&nbsp;</TD></TR>";
}
"</center>"
?>
</TABLE>

#4 Vikas Jayna

Vikas Jayna
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts
  • LocationNoida, Delhi, India

Posted 04 August 2006 - 04:06 PM

This script search.php should be called as search_list.php?chose=code_no i.e. if the domain name of your server is "yourserver.com" and the file search_list.php is located in the folder "xyz", then you need to type "http://yourserver.co...?chose=code_no" in the browser to execute the same. This means that if there is an application that shows the link to search_list.php then this has to be done in that application
Vikas Jayna,
Project Manager - Jeevansathi.com
7 yrs. exp. in LAMP
Certified Mysql DBA
Zend Certified Engineer




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users