• Resolved fyllhund

    (@fyllhund)


    Hi , I’m trying to figure out how I could sort categories with a checkbox I.E If I press a certain category in the checkbox the posts comes up.

    E.G
    Category 1 []
    Category 2 []
    Category 3 []

    Submit.

    If i choose any of those categories or all of them the posts show up upon submit.

    Cheers!

Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter fyllhund

    (@fyllhund)

    <form method="post" action="" id="formen">
    
     	<?php
     	$categories=get_categories('child_of=4&hide_empty=0');
     	foreach($categories as $category) {
     		echo '<div class="formo">';
     		echo "<input type='checkbox' name='place_id[]' value='$category->term_id' />";
     		echo '<span>'.$category->cat_name.'</span>';
     		echo '</div>';
     	}//end foreach
    
     	$place_id=$_POST['place_id'];
     	while (list ($key,$val) = @each ($place_id)) {
     		$v = "$val,";
    
     	}//end while
     	?>
     	<br />
     	<input type="submit" value="<?php echo(__('Sort','rab')); ?>" id="formbottom">
     </form>
     <?php
     if(empty($v)) {
     	query_posts('cat=4&posts_per_page=10');
     }else{
     	query_posts(array('category__and'=>array($v),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));
     }//end if
      ?>

    What I’ve came up with. Problem is that it doesn’t work for multiple choices.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this: https://pastebin.com/jTyNHPt8

    Thread Starter fyllhund

    (@fyllhund)

    Works great! Cheers.

    Thread Starter fyllhund

    (@fyllhund)

    Hi , I have one problem though. The query doesn’t stick if you navigate with previous entries/pagination.

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this query [untested]:

    <?php
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
     if(empty($cat_array)) {
            query_posts('cat=4&posts_per_page=10&paged='.$paged);
     }else{
            query_posts(array('category__and'=> $cat_array,'posts_per_page'=>2,'orderby'=> 'title', 'order'=> 'DESC','paged' => $paged));
     }//end if
    ?>

    Thread Starter fyllhund

    (@fyllhund)

    Hi , thanks for the reply, does not work unfortunately. I think the main problem is that when the page refreshes it forgets the checkbox option and therefore loses the query.

    Thread Starter fyllhund

    (@fyllhund)

    I think I’ve come to an solution to this problem and that is to save the user input (checkbox values) into a session. However I can’t get it to work.

    Cheers!

    Thread Starter fyllhund

    (@fyllhund)

    I.E (sorry for spam , couldn’t edit my reply)

    <?php $_SESSION['cat_array'] = $_POST["place_id"]; ?>
    Thread Starter fyllhund

    (@fyllhund)

    I seem to have solved it by using (ignore the prints)
    however WordPress seems to be excluding my session statements

    <?php
    $cat_array = (isset($_POST['place_id'])) ? $_POST['place_id'] : array();
    
    $kategorival = (isset($_SESSION['checked'])) ? $_SESSION['checked'] : array();  
    
        echo '<pre>';
    
        print_r($_POST['place_id']);
        echo '</pre>';
    ?>
    
    <form method="post" action="" id="formen">
    
    <?php
            $categories=get_categories('child_of=4&hide_empty=0');
            $checked = array();
            foreach($categories as $category) {
    
                    echo '<div class="formo">';
                    echo "<input type='checkbox' name='place_id[$category->term_id]' value='$category->term_id' ";
                    echo (in_array($category->term_id, $cat_array)) ? 'checked="checked"' : '';
                    echo "/>";
                    echo '<span>'.$category->cat_name.'</span>';
                    echo '</div>';
    
                    if (in_array($category->term_id, $cat_array)) {
                    	 $checked[$category->term_id] = $category->term_id;
                    }
    
            }//end foreach
            $_SESSION['checked'] = $checked ;
            echo '<pre>';
    //
            print_r($_SESSION['checked']);
            echo '</pre>';
    
    ?>
            <br />
               <p id="submitarn">
          <input type="submit" value="<?php echo(__('Sort','rab')); ?>" id="formbottom">
         <a id="resetarn" href="2-14"><input type="button" value="Reset"></a>
          </p>
     </form>
    
     <?php
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
     if(empty($cat_array)) {
            query_posts('cat=4&posts_per_page=10&paged='.$paged);
     }else{
            query_posts(array('category__and'=> $kategorival,'posts_per_page'=>10,'orderby'=> 'title', 'order'=> 'DESC','paged' => $paged));
     }//end if
    ?>

    Thread Starter fyllhund

    (@fyllhund)

    I solved it by using sessions for all pages except the first query page.

    [74 lines of code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    Thread Starter fyllhund

    (@fyllhund)

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Sort categories by checkbox’ is closed to new replies.