Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP search form, show results on load

Posted on 2014-03-03
7
Medium Priority
?
320 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 111

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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

722 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