Page 1 of 1

Trouble with my search

Posted: Wed Feb 25, 2004 1:13 pm
by PatJunkins
Trying to do an exact search on the partnnumber field then if not there in the description field.

Code: Select all

$pn = ($pn == "" ? $pn = "%" : $pn = $pn); 
$description = ($description == "" ? $description = "%" : $description = $description); 

$query = mysql_query("SELECT part_number,description,price,weight FROM suzukiparts WHERE part_number LIKE '%$pn%' AND description LIKE '%$description%'")or die(mysql_error()); 

if (mysql_num_rows($query) > 0) { 
while(list($o_part_number,$o_description,$o_price,$o_weight) = mysql_fetch_row($query)) { 
if (stristr($o_description, "USE PART#")) { 
list($p1,$part_num) = explode("# ",$o_description); 
print "<body bgcolor="orange"><table border="0"><font face="arial" size="-1"><center><b><h4>Updated Part Number, Click on new number <a href="spart3.php?pn=$part_num">$part_num</a>\n"; 
&#125; 
else &#123; 
print "<body bgcolor="yellow"><table border="0" width="100%" align="left"><tr valign="top"><td width="20%"><b><big>Part# </b></big> $o_part_number</td><td width="25%"><b><big>Desc </big></b> $o_description</td><td width="15%"><b><big>Price</big></b> $o_price</td><td width="5%"><b><big>Quantity </big></b></td><td width="20%"><form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" type="image" SRC="images/add2cart.gif" alt="Add to cart..." name="I1" target="_self"><input type="hidden" name="return" value="http://www.alpha-sports.com/footer.html"><input type="hidden" name="price" value="$o_price"><input type="hidden" name="name" value="$o_description"><INPUT TYPE="TEXT" NAME=quantity VALUE="1" SIZE="2" MAXLENGTH="2"><input type="hidden" name="custom1" value="$o_description"> <input type="hidden" name="custom2" value="Stock# $o_part_number"> <input type="hidden" name="sh" value="$o_weight"> <INPUT TYPE="HIDDEN" NAME="redirect" TARGET="main1" VALUE="1"> <input type="hidden" name="add" value="1"><input alt="Add to cart..." name="I3" src="images/add2cart.gif" type="image"><INPUT TYPE="hidden" name="redirect" value="2"></form><td width="5%"><form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" target="_top"><input alt="Go to shopping cart checkout..." name="I4" src="new_images/checkoutButton_61x22.gif" type="image"></form></td></tr></table>\n"; 
&#125; 
&#125; 
&#125; else &#123; 
print "<body bgcolor="red"><table border="0"><font face="arial" size="-1"><center><b><h4>Part number is no longer in the price book. Email us the part number, model and description for the price."; 
&#125;

Posted: Wed Feb 25, 2004 1:57 pm
by xisle
so whats the problemo, any errors?
Also if you could put line breaks in your code, more people may try to help.
Ayup! ;)

Posted: Wed Feb 25, 2004 2:19 pm
by Draco_03
hey you should edit your post and insted of the tag

Code: Select all

put

Code: Select all

it'll help for ppl who want to read it

Re: Trouble with my search

Posted: Wed Feb 25, 2004 7:45 pm
by PatJunkins

Code: Select all

<?php

$connect = mysql_connect("a", "b", "c");
mysql_select_db("m");


$part_number = ($part_number == "" ? $part_number = "%" : $part_number = $part_number);
$description = ($description == "" ? $description = "%" : $description = $description);

$query = mysql_query("SELECT part_number,description,price,weight FROM suzukiparts WHERE part_number LIKE '%$part_number%' AND description LIKE '%$description%'")or die(mysql_error());

if (mysql_num_rows($query) > 0) {
  while(list($o_part_number,$o_description,$o_price,$o_weight) = mysql_fetch_row($query)) {
    if (stristr($o_description, "USE PART#")) {
      list($p1,$part_num) = explode("# ",$o_description);
      print "<body bgcolor="orange"><table border="0"><font face="arial" size="-1"><center><b><h4>Updated Part Number, Click on new number <a href="spart.php?part_number=$part_num">$part_num</a>\n";
      } 
		else {
      print "<body bgcolor="yellow"><table border="0" width="100%" align="left"><tr valign="top"><td width="20%"><b><big>Part# </b></big> $o_part_number</td><td width="25%"><b><big>Desc </big></b> $o_description</td><td width="15%"><b><big>Price</big></b> $o_price</td><td width="5%"><b><big>Quantity </big></b></td><td width="20%"><form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" type="image" SRC="images/add2cart.gif" alt="Add to cart..." name="I1" target="_self"><input type="hidden" name="return" value="http://www.alpha-sports.com/footer.html"><input type="hidden" name="price" value="$o_price"><input type="hidden" name="name" value="$o_description"><INPUT TYPE="TEXT" NAME=quantity VALUE="1" SIZE="2" MAXLENGTH="2"><input type="hidden" name="custom1" value="$o_description"> <input type="hidden" name="custom2" value="Stock# $o_part_number"> <input type="hidden" name="sh" value="$o_weight"> <INPUT TYPE="HIDDEN" NAME="redirect" TARGET="main1" VALUE="1"> <input type="hidden" name="add" value="1"><input alt="Add to cart..." name="I3" src="images/add2cart.gif" type="image"><INPUT TYPE="hidden" name="redirect" value="2"></form><td width="5%"><form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" target="_top"><input alt="Go to shopping cart checkout..." name="I4" src="new_images/checkoutButton_61x22.gif" type="image"></form></td></tr></table>\n";
    } 
	}
} else {
    print "<body bgcolor="red"><table border="0"><font face="arial" size="-1"><center><b><h4>Part number is no longer in the price book. Email us the part number, model and description for the price.";
}
	 

?>
?>

Posted: Wed Feb 25, 2004 7:46 pm
by PatJunkins
That should be better... Sorry about that. :)

Posted: Thu Feb 26, 2004 8:53 am
by Draco_03
i don't know what s your probleme but there's error in your html code..
you don't close your form, and you don t close the end of the last line

Code: Select all

<?php

$connect = mysql_connect("a", "b", "c");
mysql_select_db("m");


$part_number = ($part_number == "" ? $part_number = "%" : $part_number = $part_number);
$description = ($description == "" ? $description = "%" : $description = $description);

$query = mysql_query("SELECT part_number,description,price,weight FROM suzukiparts WHERE part_number LIKE '%$part_number%' AND description LIKE '%$description%'")or die(mysql_error());

if (mysql_num_rows($query) > 0) {
  while(list($o_part_number,$o_description,$o_price,$o_weight) = mysql_fetch_row($query)) {
    if (stristr($o_description, "USE PART#")) {
      list($p1,$part_num) = explode("# ",$o_description);
      print "<body bgcolor="orange"><table border="0"><font face="arial" size="-1"><center><b><h4>Updated Part Number, Click on new number <a href="spart.php?part_number=$part_num">$part_num</a>\n";
      }
      else {
      print "
<body bgcolor="yellow">
<table border="0" width="100%" align="left">
<tr valign="top">
  <td width="20%"><b><big>Part# </b></big>$o_part_number</td>
  <td width="25%"><b><big>Desc </big></b> $o_description</td>
  <td width="15%"><b><big>Price</big></b> $o_price</td>
  <td width="5%"><b><big>Quantity </big></b></td>
  <td width="20%">
<form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" type="image" SRC="images/add2cart.gif" alt="Add to cart..." name="I1" target="_self">
  <input type="hidden" name="return"  value="http://www.alpha-sports.com/footer.html">
  <input type="hidden" name="price" value="$o_price">
  <input type="hidden" name="name" value="$o_description">
  <INPUT TYPE="TEXT" NAME=quantity VALUE="1" SIZE="2" MAXLENGTH="2">
  <input type="hidden" name="custom1" value="$o_description">
  <input type="hidden" name="custom2" value="Stock# $o_part_number">
  <input type="hidden" name="sh" value="$o_weight">
  <INPUT TYPE="HIDDEN" NAME="redirect" TARGET="main1" VALUE="1">
  <input type="hidden" name="add" value="1"><input alt="Add to cart..." name="I3" src="images/add2cart.gif" type="image">
  <INPUT TYPE="hidden" name="redirect" value="2"></form><td width="5%">

//Should you close your form ??? 
</form>

<form METHOD="POST" ACTION="http://www.alpha-sports.com/cgi-bin/cart.cgi" target="_top">
  <input alt="Go to shopping cart checkout..." name="I4" src="new_images/checkoutButton_61x22.gif" type="image"></form></td></tr></table>\n";
    }
   }
} else {
    print "
<body bgcolor="red">
<table border="0"><font face="arial" size="-1">
<center><b><h4>Part number is no longer in the price book. Email us the part number, model and description for the price.";

// this has to be in to.. unless you have it after the code you posted
</h4></b></center>

}
   
?>

Posted: Thu Feb 26, 2004 10:06 am
by PatJunkins
What about the search logic though... I come up with multiple results instead of just one. Want to find exact or look in description...

THX

Posted: Thu Feb 26, 2004 11:23 am
by Draco_03
i m not sure at all.. but a hint is iin your query put LIMIT 1 ..

Posted: Thu Feb 26, 2004 6:10 pm
by llanitedave
It's hard to read your logic -- apparently it is for you as well.

You need more white space, and some well-placed comments.
(I'm not all that accomplished at sight-reading code yet)

Do you want to find an exact match for the part number?
Failing that, to find an exact match for description?

If so, don't use "Like %$part_number%" in your queries.

Use "= $part_number" instead.

Otherwise, you're almost guaranteed to find multiple results.

Posted: Thu Feb 26, 2004 8:47 pm
by PatJunkins
I did try that but then it wouldn't find anything. Do I have to trim the data of white spaces or is PHP and MySql a little more forgiving?

TIA