Jump to content

Echoing search variables..


smonkcaptain

Recommended Posts

Hey all,

 

I'm using $_POST to post search variables from a search form i have made for my site.

 

Below is the code for the search form:

 

<form method="post" action="results.php">
  
  <p class="label">Airline </p><p class="right"> <select style="width: 200px;" name="airline" id="airline">
    <option selected="selected" value=""> All Airlines</option>
   <?php $query=mysql_query("SELECT * FROM photos GROUP BY airline, aircraft ORDER BY airline");
   while($row = mysql_fetch_assoc($query)){
   echo '<option value="'.$row['airline'].'">'.$row['airline'].'</option>';
   }
   ?>
   
   </select></p><br /><br />
  <p class="label">Aircraft </p><p class="right"> <select style="width: 200px;" name="aircraft" id="aircraft">
  <option selected="selected" value=""> All Aircraft</option>
   <?php $query=mysql_query("SELECT * FROM photos GROUP BY aircraft ORDER BY aircraft");
   while($row = mysql_fetch_assoc($query)){
   echo '<option value="'.$row['aircraft'].'">'.$row['aircraft'].'</option>';
   }
   ?>
      
        
      </select></p><br /><br />
  <p class="label">Registration </p><p class="right"><input type="text" style="width: 198px;" class="text" name="registration" id="registration" value="" /></p><br /><br />
  <p class="label">Construction Number </p><p class="right"><input type="text" style="width: 198px;" class="text" name="cn" id="cn" value="" /></p><br /><br />
  <p class="label">Location </p><p class="right"> <select class="select" style="width: 200px;" name="location" id="location">
        <option selected="selected" value=""> All Locations</option>
   <?php $query=mysql_query("SELECT * FROM photos GROUP BY location ORDER BY location");
   while($row = mysql_fetch_assoc($query)){
   echo '<option value="'.$row['location'].'">'.$row['location'].'</option>';
   }
   ?>
        
        
        
        
        </select></p><br /><br /><br />
  
  <div class="options2"></div><br /><br />
  
  <p class="label">Genre </p><p class="right"><select class="select" style="width: 200px;" name="class" id="class">
    <option selected="selected" value=""> All Genres</option>

    <option> Airliner</option>
    <option> Cargo</option>
    <option> Executive/Private</option>
    <option> General Aviation</option>
  </select></p><br /><br />
  <p class="label">Aspect/Subject </p><p class="right"><select class="select" style="width: 200px;" name="aspect" id="aspect">

        <option selected="selected" value=""> All Aspects</option>
    <option> Cabin View</option>
    <option> Close Up</option>
    <option> Flight Deck</option>
    <option> Front View</option>
        <option> Rear View</option>
    <option> Side Profile</option>
  </select></p><br /><br />
  <p class="label">Scheme </p><p class="right"> <select class="select" style="width: 200px;" name="scheme" id="scheme">
    <option selected="selected" value=""> All Schemes</option>
    <option> Basic</option>
    <option> Extra Stickers</option>
    <option> Hybrid</option>
        <option> Special</option>
    <option> Standard</option>
  </select></p><br /><br />
  <p class="label">Situation/Phase</p><p class="right"> <select class="select" style="width: 200px;" name="situation" id="situation">
    <option selected="selected" value=""> All Situations</option>
        <option> Approach</option>
    <option> Departure</option>
    <option> Ground</option>
  </select></p><br /><br />
  <p class="label">Time</p><p class="right"> <select class="select" style="width: 200px;" name="tod" id="tod">
    <option selected="selected" value=""> All Times of Day</option>
    <option> Dawn and Dusk</option>
    <option> Day</option>
        <option> Night</option>
  </select></p><br /><br /><br />
  
  <p class="label">Order By</p><p class="right">
  
  
  
  <select class="select" style="width: 110px;" name="order" id="order">
    <option selected="selected" value=""> Photo #</option>
	<option> Aircraft</option>
        <option> Airline</option>	
    <option> Date of Photo</option>		
    <option> Location</option>
    <option> Registration</option>		
  </select>
      
      
      </p>
  
  <p class="right">
  
  <br /><br />
  <input type="submit" name="submit" id="submit" value="  Search  " /> 
  <input type="reset" name="reset" id="reset" value="  Reset  " />
  
  </form>

 

 

On the results page i have this script:

 

if(!empty($_POST)) {
   $criteria= implode('</strong> > <strong>', array_map('htmlentities', $_POST));
}


<p class="resulttext">Your search <?php if(!empty($_GET)) { echo 'for ';} ?> <strong><?php  echo $criteria;?></strong> returned <strong><?php echo $num;?></strong> photo results, these are displayed below.</p>

 

 

to echo the search variable.

For example if someone used the form to search for 'British Airways' and 'London Heathrow', the results page would read:

 

results.php?airline=British+Airways&location=London+Heathrow

 

Your search for British Airways > London Heathrow has returned X photo results....

 

 

Although this is working; because there is often alot of form options not used, the url turn's out to be:

 

results.php?airline=British+Airways&location=London+Heathrow&aircraft=&registration=&blah=&blah=&submit=search .....etc

 

Which then reads as:

 

Your search for British Airways > London Heathrow > > > > > SEARCH has returned X photo results....          as seen below in this picture:

 

5374000.jpg

 

 

Is there anyway to stop this from happening and just echo the search field which !=""

 

Thanks In Advanced!

Link to comment
https://forums.phpfreaks.com/topic/232505-echoing-search-variables/
Share on other sites

You could use a foreach loop and empty() to check and see if each element in your $_POST array is empty and if it is don't use it. Maybe even use array_push() to apply the results that aren't empty to a new array and implode that new array with the html you are wanting to use. Something like what is below. I haven't tested it but I think it should work.

<?php 

$criteria = array();
if(!empty($_POST)) {
  	foreach ($_POST as $_POST) { 
	if (!empty($_POST)) {
	array_push($criteria, $_POST);
	}
}

$userserch = implode('</strong> > <strong>', array_map('htmlentities', $criteria));
}


?>

 

You could use a foreach loop and empty() to check and see if each element in your $_POST array is empty and if it is don't use it. Maybe even use array_push() to apply the results that aren't empty to a new array and implode that new array with the html you are wanting to use. Something like what is below. I haven't tested it but I think it should work.

<?php 

$criteria = array();
if(!empty($_POST)) {
  	foreach ($_POST as $_POST) { 
	if (!empty($_POST)) {
	array_push($criteria, $_POST);
	}
}

$userserch = implode('</strong> > <strong>', array_map('htmlentities', $criteria));
}


?>

 

 

Thanks that works!

 

However, it's still showing 'Submit Query':

 

5374747.jpg

 

Here is the code for the submit form input:

 

<input type="submit" name="submit" id="submit" value=""/>

 

Thanks!

 

Try this I added array_pop() it should remove the last element of the array. but I am guessing that submit_query is the last element in the $_POST array.

 

<?php 

$criteria = array();
if(!empty($_POST)) {
        array_pop($_POST);
  	foreach ($_POST as $_POST) { 
	if (!empty($_POST)) {
	array_push($criteria, $_POST);
	}
}

$userserch = implode('</strong> > <strong>', array_map('htmlentities', $criteria));
}


?>

Try this I added array_pop() it should remove the last element of the array. but I am guessing that submit_query is the last element in the $_POST array.

 

<?php 

$criteria = array();
if(!empty($_POST)) {
        array_pop($_POST);
  	foreach ($_POST as $_POST) { 
	if (!empty($_POST)) {
	array_push($criteria, $_POST);
	}
}

$userserch = implode('</strong> > <strong>', array_map('htmlentities', $criteria));
}


?>

 

Thankyou very much! Top man..

 

Yes, the submit_query was the last element in the array.

 

Thanks once again  :D

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.