Jump to content


Photo

Problem with search results


  • Please log in to reply
7 replies to this topic

#1 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 15 October 2006 - 07:17 PM

Hi all,

I have a script that allows you to search for suppliers in a given county. I have the counties stored as numeric values in the database. The problem comes with trying to change the numeric value into the name of the county:

<html>
<title><?php print $title ?></title>
<form name="form" action="<?=$_SERVER["PHP_SELF"]?>" method="get">
  <p>County:
    <name="county">
    <?php
    //$currentvalue0=$row['county'];
    
    $counties = array(1=>'Aberdeenshire', '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" />Please Select a County'; 
/*for ($i=1; $i <= sizeof($counties); $i++) 
{ */
 foreach($counties as $_key => $_county)
  {
  echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';
  //echo '<option value="'.($_key < 0 ? '0'.$_key : $_key).'"/>'.$_county.'</option>';
}  
//}
 /* $lz = strlen($i) == 1 ? '0'.$i : $i; 
echo '<option value="'.$lz.'" selected />'.$counties[$i-1]; 
} 
echo '</select>'; */
    ?>
    </p>
  <input type="submit" name="Submit" value="Search" />
</form>
<html>

<?php
  
$var = $counties[(int)$_GET['county']] ;
$county = trim( $counties[ intval($_GET['county']) ] );

The problem lies with the last two lines above, when the page loads I get an undefined index on $var = $counties...
But then when you try to search on any of the counties it returns the county name, but no search results despite there being values in the database.

If you need the rest of the code posted please let me know.

Thanks,

G

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 October 2006 - 07:25 PM

When the page first loads there is no $_GET data as you haven't submitted the form, hence the undefined index error as $_GET['county'] does not yet exist.
<?php
if (isset($_GET['county'])) {  
     $var = $counties[(int)$_GET['county']] ;
     $county = trim( $counties[ intval($_GET['county']) ] );
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 15 October 2006 - 07:34 PM

Ok, thats great fixed the first problem, but it is still displaying the county name but no results...
Is there enough code here or do you wnat the rest (you know what here it is anyway if you need it):

<?php

//HERE IS THE OFFENDING CODE THAT HAS JUST BEEN FIXED AND THEN EVERYTHING BELOW IT:

  if (isset($_GET['county'])) { 
$var = $counties[(int)$_GET['county']] ;

$county = trim( $counties[ intval($_GET['county']) ] );
//$var= $county;
// rows to return
$limit=10; 

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

// check for a search parameter
if (!isset($var))
  {
  echo "<p>Please enter a search</p>";
  exit;
  }

/*$query = "select county, area_covered1, area_covered2, area_covered3, supplierid, username from suppliers where county =\"$county\" order by username"; */
$query = "select county
     , area_covered1
     , area_covered2
     , area_covered3
     , supplierid
     , username 
  from suppliers 
 where '$county' in
       ( county         
       , area_covered1 
       , area_covered2 
       , area_covered3 )
order 
    by username";
$numresults=mysql_query($query) or die(mysql_error());  
$numrows=mysql_num_rows($numresults); 
  

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . /*$counties[$_GET['county']]*/$var . "&quot; returned zero results</p>";

 }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results

  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";


// begin to show results set
echo "Results:   ";
$count = 1 + $s ;

// now you can display the results returned   
while ($row= mysql_fetch_array ($result)) {
  $title = $row ["username"];
  $supplier = $row ["supplierid"];
  echo "<p>$count.)&nbsp; <a href=\"supplierinfo.php?id=$supplier\">$title</a></p>" ;

  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&county=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&county=$var\">Next 10 &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  }
?>


#4 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 16 October 2006 - 06:25 PM

I have been looking at this again, and am really stumped - I cant see where the problem lies when I am turning the numeric value into the name of the county and pulling back the search results, I can get them working independantly, but not together.

Please help!


G

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 October 2006 - 07:05 PM

It would help if you post some sample data, then we can se what you are trying to process.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 16 October 2006 - 07:52 PM

Its still the code above, I have solved the initial undefined index problem, but I am still not getting returnwed results even though they are in the database, I simply get no results found...

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 October 2006 - 07:58 PM

If I know what your data looks like I can set up some similar data and test your code. As you are unwilling to respond to requests for information about your problem I am unsubscribing from this thread.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 genista

genista
  • Members
  • PipPipPip
  • Advanced Member
  • 149 posts

Posted 16 October 2006 - 09:52 PM

Ok dude, here are the code from my two earlier posts put together in this one post:

<html>
<title><?php print $title ?></title>
<form name="form" action="<?=$_SERVER["PHP_SELF"]?>" method="get">
  <p>County:
    <name="county">
    <?php
    //$currentvalue0=$row['county'];
    
    $counties = array(1=>'Aberdeenshire', '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" />Please Select a County'; 
/*for ($i=1; $i <= sizeof($counties); $i++) 
{ */
 foreach($counties as $_key => $_county)
  {
  echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';
  //echo '<option value="'.($_key < 0 ? '0'.$_key : $_key).'"/>'.$_county.'</option>';
}  
//}
 /* $lz = strlen($i) == 1 ? '0'.$i : $i; 
echo '<option value="'.$lz.'" selected />'.$counties[$i-1]; 
} 
echo '</select>'; */
    ?>
    </p>
  <input type="submit" name="Submit" value="Search" />
</form>
<html>

<?php

//HERE IS THE OFFENDING CODE THAT HAS JUST BEEN FIXED AND THEN EVERYTHING BELOW IT:

  if (isset($_GET['county'])) { 
$var = $counties[(int)$_GET['county']] ;

$county = trim( $counties[ intval($_GET['county']) ] );
//$var= $county;
// rows to return
$limit=10; 

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

// check for a search parameter
if (!isset($var))
  {
  echo "<p>Please enter a search</p>";
  exit;
  }

/*$query = "select county, area_covered1, area_covered2, area_covered3, supplierid, username from suppliers where county =\"$county\" order by username"; */
$query = "select county
     , area_covered1
     , area_covered2
     , area_covered3
     , supplierid
     , username 
  from suppliers 
 where '$county' in
       ( county         
       , area_covered1 
       , area_covered2 
       , area_covered3 )
order 
    by username";
$numresults=mysql_query($query) or die(mysql_error());  
$numrows=mysql_num_rows($numresults); 
  

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . /*$counties[$_GET['county']]*/$var . "&quot; returned zero results</p>";

 }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results

  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";


// begin to show results set
echo "Results:   ";
$count = 1 + $s ;

// now you can display the results returned   
while ($row= mysql_fetch_array ($result)) {
  $title = $row ["username"];
  $supplier = $row ["supplierid"];
  echo "<p>$count.)&nbsp; <a href=\"supplierinfo.php?id=$supplier\">$title</a></p>" ;

  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&county=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link
  $news=$s+$limit;

  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&county=$var\">Next 10 &gt;&gt;</a>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  }
?>


I have echo'd out the query and it is returning the name of the county, ie:

select county , area_covered1 , area_covered2 , area_covered3 , supplierid , username from suppliers where 'Angus' in ( county , area_covered1 , area_covered2 , area_covered3 ) order by username

As you can see Angus is searched on rather than the numeric value which is the value stored in the database. I need to retrieve the numeric value then assign the county name to it.

Thanks,

G





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users