Link to home
Start Free TrialLog in
Avatar of Daniele Brunengo
Daniele BrunengoFlag for Italy

asked on

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

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
Avatar of Daniele Brunengo

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, I'll try that out.
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.