Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

PHP search form, show results on load

Hello, I am working on a Wordpress site which I didn't setup and I found this page already done:

http://www.cirworld.eu/ricerca/

I know very little php, learning it actually. I'd like the search engine to work more as a filter, that is when you open the page you get all the news and then you filter them using the four dropdown menus.

It works fine as a search engine, I'd just like it to show all unfiltered results on load.

That would allow me to get rid of another page in the site.

So, can anybody help me with this? Here's the page's full code:

<?php
session_start();
if(isset($_POST['search'])){
$_SESSION['country']=$_POST['country'];
$_SESSION['work']=$_POST['work'];
}
if(isset($_POST['reset'])){
if(isset($_SESSION['country'])){
unset($_SESSION['country']);
}
if(isset($_SESSION['work'])){
unset($_SESSION['work']);
}
} 
/* Template Name: custom Chain*/
get_header();

?>
<section id="container">
	<?php if(!is_home() && !is_front_page() && get_option('afl_breadcrumbs_enable') == 'open'):?>
    <!--breadcrumbs -->
    <section class="container">
        <div class="container breadcrumbs">
            <div class="page-header">
            <h1><?php the_title();?></h1>
            <div><?php echo get_option('breadcrumbs_text'); ?><a href="<?php echo home_url(); ?>"><?php echo BREADCRUMBS_HOME_TEXT ?></a><?php echo BREADCRUMBS_DIVIDER ?><?php the_title();?></div>
			</div>
        </div>
    </section>
	<?php endif; ?>
	
	<div class="container">
        <div class="row-fluid">
<?php
$args = array(
  'child_of' => 59,
  'hide_empty' => 0
  );

$categories=get_categories($args);
?>
<form action="" method="post">
<div style="float: left; min-width: 25%">
	<label>Area</label>
	<select name="zone" onchange="this.form.submit()">
	<option value="59">Tutte</option>
	<?php
	foreach($categories as $category) { 
	if($category->parent == 59 ){ ?>

	<option value="<?php echo $category->term_id; ?>" <?php if($_POST['zone'] == $category->term_id){ ?> selected <?php } ?> ><?php echo $category->name; ?></option>
	<?php
	} 
	} 
	?></select> 
</div>

<?php if(isset($_POST["zone"])) {
$args = array(
  'child_of' => $_POST["zone"],
  'hide_empty' => 0
  ); }
  else {
  $args = array(
  'child_of' => 59,
  'hide_empty' => 0
  ); 
  }

$categories=get_categories($args);
?>
<div style="float: left; min-width: 25%">
	<label>Paese</label>
	<select name="country" >
	<option value="<?php if(isset($_POST["zone"])){echo $_POST["zone"];} else echo"59"; ?>">Tutti</option>
	<?php 
	foreach($categories as $category) { 
	if($category->parent != 59 ){ ?>


	<option value="<?php echo $category->term_id; ?>" <?php if($_POST['country'] == $category->term_id){ ?> selected <?php } ?>>
	<?php echo $category->name; ?></option>
	<?php
	}
	} 
	?></select> 
</div>

<?php  ?>
<?php
$args = array(
  'child_of' => 65,
  'hide_empty' => 0
  );

$categories=get_categories($args);
?>

<div style="float: left; min-width: 25%">
	<label>Settore</label>
	<select name="industry" onchange="this.form.submit()">
	<option value="65">Tutti</option>
	<?php
	foreach($categories as $category) { 
	if($category->parent == 65 ){ ?>

	<option value="<?php echo $category->term_id; ?>"  <?php if($_POST['industry'] == $category->term_id){ ?> selected <?php } ?> > <?php echo $category->name; ?></option>
	<?php
	} 
	} 
	?></select>
</div>

<?php if(isset($_POST["industry"])) {
$args = array(
  'child_of' => $_POST["industry"],
  'hide_empty' => 0
  );
  }
else {
$args = array(
'child_of' => 65,
  'hide_empty' => 0
  );
}
$categories=get_categories($args);


?>
<div style="float: left; min-width: 25%">
	<label>Prodotto</label>
	<select name="work" >
	<option value="<?php if(isset($_POST["zone"])){ echo $_POST["industry"];} else echo "65"; ?>">Tutti</option>
	<?php 
	foreach($categories as $category) { 
	if($category->parent != 65 ){ 
	 ?>

	<option value="<?php echo $category->term_id; ?>" <?php if($_POST['work'] == $category->term_id){ ?> selected <?php } ?>><?php echo $category->name; ?></option>
	<?php
	}
	} 
	?></select> 
</div>

<?php  ?>
<div class="row-fluid">
<p style="float: left; min-width:50%"><input type="submit" name="search" value="Cerca" ></p>
</form>
  <form action="" method="post">
  <p style="float: left; min-width:50%"><input type="submit" name="reset" value="Reset"></p>
  </form>
</div>
<hr />
<?php
if(isset($_SESSION['country'])){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $category_posts = new WP_Query(array( 'category__and' => array( $_SESSION['country'], $_SESSION['work'] ),'posts_per_page' => '20','paged' => $paged ));
?>
<div class="custom-search-content">
<?php
   if($category_posts->have_posts()) : 
      while($category_posts->have_posts()) : 
         $category_posts->the_post();
?>
<!--
         <h1><a href="<?php the_permalink(); ?>" ><?php the_title() ?></a></h1>
          <div class="custom-search-excerpt">
		  <?php the_excerpt() ?>
		  </div>
-->
<article class="blog-post <?php echo get_post_class() ?>" id="post-<?php the_ID(); ?> ">
	<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');?>
	<?php if (has_post_thumbnail()) { ?>
    <a href="<?php echo $src[0] ?>" rel="prettyPhoto" title="<?php echo $post->post_title ?>"><?php the_post_thumbnail('single'); ?></a>
	<?php } ?>
	<div class="row-fluid">
		<div class="span3"><?php include(TEMPLATEPATH . '/inc/blog-info.php'); ?></div>
		<div class="span9">
			<h2 class="post-title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
			<?php the_excerpt(); ?><span class="read_more"><a href="<?php the_permalink(); ?>">[ Leggi Tutto &rarr; ]</a></span>
		</div>
	</div>
</article>
<hr />
<?php
      endwhile; ?>
	  </div>
	   <nav class="custom-search-nav">
		<div style="float: left; font-size: 16px;"><?php previous_posts_link( '&laquo; PRECEDENTE', $category_posts->min_num_pages) ?></div>
		<div style="float: right; font-size: 16px;"><?php next_posts_link( 'SUCCESSIVO &raquo;', $category_posts->max_num_pages) ?></div>
	   </nav>
	  <?php
   else: 
?>

      Nessun post appartiene all'insieme di categorie selezionate.

<?php
   endif;
   }
?>
</div>
</div>
</section>
<?php get_footer(); ?>
 

Open in new window

0
Daniele Brunengo
Asked:
Daniele Brunengo
1 Solution
 
Ray PaseurCommented:
If you're not experienced in PHP, this is the wrong place to start.  Instead, get some foundation in how the language works.  This article can help you get started and can point you to some good learning resources.  And more importantly it can help you stay away from the many bad code examples that litter the internet!  You don't need to expose yourself to that junk.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
 
Daniele BrunengoIT Consultant, Web DesignerAuthor Commented:
I already wrote something in php, but I think the thing I need here is to just add a line to this file somewhere, that's why I asked for help. I'm on a schedule for this site and this page is beyond my current php knowledge.
0
 
Jason C. LevineNo oneCommented:
This is the section that controls the initial visibility:

<?php
if(isset($_SESSION['country'])){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $category_posts = new WP_Query(array( 'category__and' => array( $_SESSION['country'], $_SESSION['work'] ),'posts_per_page' => '20','paged' => $paged ));
?>

Open in new window


Because the form is using sessions, which I assume are being set when the form is submitted, you would simply add an else clause to the above to handle the instance when the session is not set:

<?php
if(isset($_SESSION['country'])){
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
   $category_posts = new WP_Query(array( 'category__and' => array( $_SESSION['country'], $_SESSION['work'] ),'posts_per_page' => '20','paged' => $paged ));
} else {
$paged = 1;
$category_posts = new WP_Query( // your desired defaults here);
}
?>

Open in new window


When the page loads, there is no session data yet so the Loop is not initialized.  By adding a new condition that feeds something to the Loop at any state, it should load data.  Documentation on feeding arguments to the loop is here:

http://codex.wordpress.org/Class_Reference/WP_Query
0
 
Daniele BrunengoIT Consultant, Web DesignerAuthor Commented:
Thanks, I'll try that out.
0
 
GaryCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Jason C. Levine's comment #a39902492

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now