Jump to content

[SOLVED] Returning multiple results from a search query.


vomitbomb

Recommended Posts

At the moment I have to type in the full (exact) last name of an order to access it.

 

Say if an order was for russel and another for rusker, I'd like to be able to type rus and have both orders come up.

 

I am trying to find out how to do this so I can then have multiple results displayed and I can click on the right one. (in the case 2 or more people share the same last name) as you can imagine this can cause serious problems :|

 

Here is my search query, any information is appreciated.

 

//creating query
$sql = "SELECT * FROM orders WHERE lname LIKE '$search'";
$result = mysql_query($sql, $conn);
$row = mysql_fetch_array( $result );

Ok I've thought of a way to get to choose between multiple search results (ones with the same last name).

 

I haven't been using $_GET but I thought this would be the easiest way.

 

This code doesn't work, but any tips on how I can make it happen? just guessing this stuff.

 

//create a query
$sql = "SELECT * FROM orders WHERE lname LIKE '$search'";
$result = mysql_query($sql, $conn);

while($row = mysql_fetch_array( $result )){
echo "<a href=showorders.php?searchf=$row['fname']searchl=$row['lname']>";
echo "".$row['fname'];
echo "".$row['lname'];
echo "</a>";
}

This actually works, but only for the last name, how would I add a first name into the URL (I commented that out)?

 

$sql = "SELECT * FROM orders WHERE lname LIKE '$search'";
$result = mysql_query($sql, $conn);

while($row = mysql_fetch_array( $result )){
echo "<a href=showorders.php?search=";
//echo "".$row['fname'];
//echo "searchl=";
echo "".$row['lname'];
echo ">";
echo "".$row['fname'];
echo "".$row['lname'];
echo "<br>";
echo "</a>";
}

$sql = "SELECT * FROM orders WHERE lname LIKE '$search'";
$result = mysql_query($sql, $conn);

while($row = mysql_fetch_array( $result )){
echo "<a href='showorders.php?search=$row[fname]&searchl=$row[lname]'>$row[fname] $row[lname]</a><br>";
}

 

Obviously, you'll have to incorporate more code if you want to search for the first name too.

Cool it's parsing values like a champion now, next problem..

 

How do I use SELECT to search for 2 strings?  :-\

 

ps. Here is the code I got working.

$sql = "SELECT * FROM orders WHERE lname LIKE '$search'";
$result = mysql_query($sql, $conn);

while($row = mysql_fetch_array( $result )){
echo "<a href=showorders.php?search=";
echo "".$row['lname'];
echo "&searchl=";
echo "".$row['fname'];
echo ">";
echo "".$row['fname'];
echo "".$row['lname'];
echo "<br>";
echo "</a>";
}

 

and here is the code at the script it's being sent to.

 


$search = $_GET['search'];
$searchl = $_GET['searchl'];

$sql = "SELECT * FROM orders WHERE lname LIKE '%$search%'";

I want this SELECT to use both $search and $searchl to get the exact result.

 

 

Thanks Conker, your code is much nicer. But I'm pretty happy with my working mess :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.