dannel77 Posted July 21, 2006 Share Posted July 21, 2006 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 variablesrequire("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.. :( Quote Link to comment Share on other sites More sharing options...
Vikas Jayna Posted July 21, 2006 Share Posted July 21, 2006 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' Quote Link to comment Share on other sites More sharing options...
dannel77 Posted July 22, 2006 Author Share Posted July 22, 2006 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 variablesrequire("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 connectionmysql_connect("$host","$user","$pass"); // database selectionmysql_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 sidaif ($start > 0) { $numlink .= '<a href="?start='.($start - $limit).'&searchstring='.$searchstring.'">« </a> '; } else { $numlink .= '« ';}// Hämta sidonummerfor ($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.'&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 sidaif ($numrows > ($start + $limit)) $numlink .= '<a href="?start='.($start + $limit).'&searchstring='.$searchstring.'"> »</a> ';else $numlink .= ' » ';$numlink = '<div style="font-size: 11px; font-family: Verdana, Arial, Helvetica, sans-serif; padding: 5px 0;"> '.$numlink.'</div>';// Skriv ut sidornaecho $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> Art nr. </b></FONT></TD><TD BGCOLOR="#D5E2EC" ALIGN="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b> Titel </b></FONT></TD><TD BGCOLOR="#D5E2EC" ALIGN="right"><font face="Verdana, Arial, Helvetica, sans-serif" size="-2" color="Black"><b> Pris SEK </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\"> $row[2] </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\"> $row[3] </FONT></TD>";echo "<TD ALIGN=\"right\"></a><font style=\"font-size: 10px; font-family: Verdana, Arial, Helvetica, sans-serif;\"> $row[5] </font></TD>";echo "<TD ALIGN=\"middle\"> <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> </TD></TR>";}"</center>"?></TABLE> Quote Link to comment Share on other sites More sharing options...
Vikas Jayna Posted August 4, 2006 Share Posted August 4, 2006 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.com/xyz/search_list.php?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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.