Solved

PHP search form, show results on load

Posted on 2014-03-03
7
299 Views
Last Modified: 2014-10-18
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
Comment
Question by:Daniele Brunengo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39900636
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
 

Author Comment

by:Daniele Brunengo
ID: 39900965
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
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 500 total points
ID: 39902492
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
 

Author Comment

by:Daniele Brunengo
ID: 39902662
Thanks, I'll try that out.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40388733
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question