Displaying results set issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Displaying results set issue

Post by genista »

Hi all,

I have a script that allows a user to search for suppliers in a given county. The counties are stored as numbers in the database, and I have got some help changing these values to the name of the county that has been 'searched on.'

The problem that I have has arisen through trying to display the actual county to the user rather than the numeric value taken from the database. By running the following code it returns a 'no results found' even thought there are records in the database. Here is the code:

Code: Select all

<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="'.($_key < 0 ? '0'.$_key : $_key).'" selected />'.$_county.'</option>';
}  
//}

    ?>
    </p>
  <input type="submit" name="Submit" value="Search" />
</form>
<html>

<?php

  $var = $counties[$_GET['county']] ;
  $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;
  }

/*$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: "" . $counties[$_GET['county']] . "" returned zero results</p>";

 }

Thanks in adance for any help you can give.


Thanks,


G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

<name="country"> isn't a tag.

An aside, every single option you write out has "selected" in them.

Additionally, using PHP_SELF is considered a security hole. If you want to submit back to the current page you can use "#" as the action.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

I will look into the php self matter after I have this fixed, thanks for the heads up though.

In response to your post about the tag, I am not sure what you mean and how it should be, this hasn't been a problem up until I was trying to get the county name.

I also take it then that this has to be changed (referring to your post about every item will be selected)?:

echo '<option value="'.($_key < 0 ? '0'.$_key : $_key).'" selected

Thanks,


G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yes, both need to be changed.

The "selected" bit should only be output if something was selected and you're displaying the list again.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Sorry I am now completly lost, if this is the offending piece of code what can I do to change it? I just cant see how from where I am sat. here is what i have now:

Code: Select all

echo '<select name="county"><option value="01" />Please Select a County'; 

 foreach($counties as $_key => $_county)
  {

  echo '<option value="'.($_key < 0 ? '0'.$_key : $_key).'" selected />'.$_county.'</option>';
and here is how it was before I messed with it:

Code: Select all

echo '<select name="county"><option value="01" />Please Select a County'; 

for ($i=1; $i <= sizeof($counties); $i++) 
{
$lz = strlen($i) == 1 ? '0'.$i : $i; 
echo '<option value="'.$lz.'" selected />'.$counties[$i-1]; 
} 
echo '</select>';
This above worked, but alas I was told to change it to the first piece of code.

Thanks,

G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Pull out "selected," it's not needed for your functionality as far as I can see.

"Please select a country" should probably have a closing option tag after it since you are placing content into the option. It would follow that the slashes in the opening option tags be removed as well.

As for the change to an inline ternary, I don't see the value. A blind sprintf() will accommodate perfectly fine.
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

Ok so here is what I have with sprintf, I have posted the problem below the code here:

Code: Select all

echo '<select name="county"><option value="01" />Please Select a County'; 

 foreach($counties as $_key => $_county)
  {
  echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';

}  

    ?>
    </p>
  <input type="submit" name="Submit" value="Search" />
</form>
<html>

<?php
  
  $var = /*@$_GET['county']*/$counties[$_GET['county']] ;
 
$county = trim( $counties[ intval($_GET['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;
  }
I get an undefined index error when any of the counties are under 100 and I believe that it is this line that is causing the issue:

echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';

Thanks,
G
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would guess that you need to copy your intval() call to the $var assignment line too... although I'm not sure I even understand the need to zero pad your numbers to begin with if you're just going to strip them out afterward. :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

How come in your $counties array, only bonnie Siorrachd Obar Dheathain has a key assigned?
(#10850)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

arborint wrote:How come in your $counties array, only bonnie Siorrachd Obar Dheathain has a key assigned?
For initializations and numeric indiced arrays, the following numbers will be automatically incremented:

Code: Select all

[feyd@home]>php -r "$arr = array(1=>'asdf', '1234'); var_dump($arr);"
array(2) {
  [1]=>
  string(4) "asdf"
  [2]=>
  string(4) "1234"
}
genista
Forum Commoner
Posts: 57
Joined: Fri Aug 18, 2006 3:56 pm

Post by genista »

ok now I am completly lost, Aberdeenshire is selected because it is a search query that I happened to be using, I hope it might help if I post the complete code:

Code: Select all

<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 = /*@$_GET['county']*/$counties[$_GET['county']] ;
  //$county = trim($var); //trim whitespace from the stored variable
$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: "" . $counties[$_GET['county']] . "" returned zero results</p>";

 }

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

// get results
// line 60
  $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: "" . $counties[$_GET['county']] . ""</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);
//80
//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\"><< 
  Prev 10</a>&nbsp&nbsp;";
  }
//line 90
// 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++;
  }
//line 100
// 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 >></a>";
  }
//line 109
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
  
?>
Edited: Added code after accidently submitting
shneoh
Forum Commoner
Posts: 38
Joined: Mon Sep 25, 2006 2:23 am
Location: Malaysia

Post by shneoh »

Suggestion:

You are trying to form a select dropdown bar for the user to choose which country in this codes:

Code: Select all

<name="county">
    <?php    
    $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'; 

 foreach($counties as $_key => $_county)
  {
  echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';
}  
    ?>
Your HTML code is not completed. Change to this:

Code: Select all

<select name="county">
    <?php    
    $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'; 

 foreach($counties as $_key => $_county)
  {
  echo '<option value="'.sprintf("%03d",$_key).'"/>'.$_county.'</option>';
}  
    ?></select>
To know better if you might did something wrong during formation of the query string, suggest you echo the query to check.
Post Reply