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
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));
}


?>

 

Link to comment
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));
}


?>

 

 

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!

 

Link to comment
Share on other sites

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));
}


?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.