Jump to content


Photo

Confused on search


  • Please log in to reply
2 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 22 August 2006 - 09:15 AM

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:

$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>

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:

$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.....

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

Thanks,

G

#2 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 22 August 2006 - 12:20 PM

I have made some changes since my last post, which are detailed below:
$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  

It is still not working though, I get no search results and no error...

#3 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 22 August 2006 - 12:39 PM

Not answering your original question but I did notice this:
    if ($i == $currentvalue0) 
    { 
        $checkedStatus = 'SELECTED'; 
    }
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:
    if ($i == $currentvalue0) 
    { 
        $checkedStatus = 'SELECTED'; 
    } else {
        $checkedStatus = '';
    }

Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users