Jump to content

Confused on search


genista

Recommended Posts

Hi,

When my users register they fill out the county they live in. They can then go and search for suppliers in that county, or (for flexibility) search on other counties. The problem I have is that I can get my drop down to display the county that the user lives in but then I need to search for suppliers against that who are obviously in a different table. It is the code that searches for the supplier that is not working (no errors).

My code look slike so:

[code=php:0]
$id = $_SESSION['username'];
$query1 = "select * from users where username='$id'";

    //now we pass the query to the database
$result1=mysql_query($query1) or die("Could not get data.".mysql_error());

    //get the first (and only) row from the result
    $row = mysql_fetch_array($result1, MYSQL_ASSOC);

$county=$row['county'];
?>



<html>
<form name="form" action="suppliersearch.php" method="get">
  <p>County:
    <name="q" </td><td>
    <?php
    $currentvalue0=$row['county'];
   
    $counties = array('Anglesey', 'Angus', 'Argyll', 'Avon', 'Ayrshire', 'Banffshire', 'Bedfordshire', 'Berkshire', 'Berwickshire', 'Borders', 'Buckinghamshire', 'Bute', 'Caithness', 'Cambridgeshire', 'Central Scotland', 'Cheshire', 'Clackmananshire', 'Cleveland', 'Clwyd', 'Cornwall', 'County Antrim', 'County Down', 'County Durham', 'County Fermanagh', 'County Londonderry', 'County Tyrone', 'Cumbria', 'Denbighshire', 'Derbyshire', 'Devon', 'Dorset', 'Dumfries and Galloway', 'Dunbartonshire', 'Durham', 'Dyfed', 'East Ayrshire', 'East Lothian', 'East Sussex', 'East Yorkshire', 'Edinburgh', 'Essex', 'Fife', 'Glamorgan', 'Gloucestershire', 'Grampian', 'Greater London', 'Greater Manchester', 'Guernsey', 'Gwent', 'Gwynedd', 'Hampshire', 'Herefordshire', 'Hertfordshire', 'Highlands and Islands', 'Humberside', 'Inverness-shire', 'Isle of Arran', 'Isle of Man', 'Isle of Skye', 'Isle of Wight', 'Jersey', 'Kent', 'Lanarkshire', 'Lancashire', 'Leicestershire', 'Lincolnshire', 'Lochaber', 'London', 'Londonderry', 'Lothian', 'Merseyside', 'Middlesex', 'Moray', 'Nottinghamshire', 'Orkneys', 'Outer Hebrides', 'Oxfordshire', 'Peeblesshire', 'Perthshire', 'Powys', 'Shropshire', 'Somerset', 'South Yorkshire', 'Staffordshire', 'Stirlingshire', 'Strathclyde', 'Suffolk', 'Surrey', 'Sutherland', 'Swansea', 'Tayside', 'Tyne and Wear', 'Warwickshire', 'West Lothian', 'West Midlands', 'West Sussex', 'West Yorkshire', 'Wester Ross', 'Wiltshire', 'Worcestershire');
echo '<select name="county"><option value="01" />Aberdeenshire';
for ($i=1; $i <= sizeof($counties); $i++)
{
//adds a leading zero if needed
$lz = strlen($i) == 1 ? '0'.$i : $i;
$checkedStatus = '';
    if ($i == $currentvalue0)
    {
        $checkedStatus = 'SELECTED';
    }
    echo '<option value="'.$lz.'" '.$checkedStatus.'>'.$counties[$i-1];
}
echo '</select>';
    ?>
    </p>
  <input type="submit" name="Submit" value="Search" />
</form>
<html>[/code]

This displays the county that the user lives in and gives them all the other counties to search on as well. Here is the next part of the same page:

[code=php:0]
$query = "select county, supplierid, username from suppliers where county = '$county' order by username";

$numresults=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($numresults);
$row = mysql_fetch_array($numresults, MYSQL_ASSOC);
$county=$row['county'];
/*$query = "select county, supplierid, username from suppliers where county = "$county";

  order by username"; // EDIT HERE and specify your table and field names for the SQL query 

$numresults=mysql_query($query) or die(mysql_error());
$numrows=mysql_num_rows($numresults);
$row = mysql_fetch_array($numresults, MYSQL_ASSOC);*/

print("Welcome to the supplier search page <b>".$_SESSION["username"]."</b><br>");
print("<a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a>");


  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $county = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($county == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>Please enter a search</p>";
  exit;
  }
more code for displaying results.....
[/code]

So I have two different queries, any help would be greatly appreciated.

Thanks,

G
Link to comment
https://forums.phpfreaks.com/topic/18281-confused-on-search/
Share on other sites

I have made some changes since my last post, which are detailed below:
[code=php:0]
$query = "select county, supplierid, username from suppliers where county = '$county' order by username"; 

$numresults=mysql_query($query) or die(mysql_error()); 
$numrows=mysql_num_rows($numresults);

print("Welcome to the supplier search page <b>".$_SESSION["username"]."</b><br>");
print("<a href=\"logout.php?".session_name()."=".session_id()."\">Logout</a>");


  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $county = trim($var); //trim whitespace from the stored variable 
[/code]

It is still not working though, I get no search results and no error...
Link to comment
https://forums.phpfreaks.com/topic/18281-confused-on-search/#findComment-78563
Share on other sites

Not answering your original question but I did notice this:
[code]    if ($i == $currentvalue0)
    {
        $checkedStatus = 'SELECTED';
    }[/code]
I see you're not clearing the value of $checkedStatus once there has been a match. My guess is that if you view the HTML for the page that this script creates then you'll find the rest have been given the SELECTED status.  Try this:
[code]    if ($i == $currentvalue0)
    {
        $checkedStatus = 'SELECTED';
    } else {
        $checkedStatus = '';
    }[/code]
Link to comment
https://forums.phpfreaks.com/topic/18281-confused-on-search/#findComment-78568
Share on other sites

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.