• Resolved Galaxy_High

    (@galaxy_high)


    Hello everyone,

    I have created a form that allows visitors to my site choose how they want the posts to be displayed. They can order by title, date, modified, rand and by comment_count in either ascending or descending order.

    When a visitor clicks a link the changes made previously revert back to their default state. How do I make the browser remember the selection?

    I am using a switch / case statements at the moment… should I be using if’s?

    Here is my code:

    <?php
    echo '<div id="refine">';
    echo '<p>Sorted ';
    
    switch ($_POST['selection']){
    
    	case 'title':
    		echo $_POST['title'] . ' <strong>alphabeticly</strong>';
    		$orderby = "title";
    		break;
    
    	case 'date':
    		echo $_POST['date'] . ' by <strong>date posted</strong>';
    		$orderby = "date";
    		break;
    
    	case 'modified':
    		echo $_POST['modified'] . ' by <strong>date modified</strong>';
    		$orderby = "modified";
    		break;
    
    	case 'rand':
    		echo $_POST['rand'] . ' <strong>randomly</strong>';
    		$orderby = "rand";
    		break;
    
    	case 'comment_count':
    		echo $_POST['comment_count'] . ' by <strong>popularity</strong>';
    		$orderby = "comment_count";
    		break;
    
    	default:
    		echo ' <strong>alphabeticly</strong>';
    		break;
    	}
    
    echo ' in <strong>';
    
    switch ($_POST['order']){
    
    	case 'Acsending':
    		echo $_POST['Acsending'] . 'ascending';
    		break;
    
    	case 'Decending':
    		echo $_POST['Decending'] . 'descending';
    		break;
    
    	default:
    		echo 'ascending';
    		break;
    	}
    
    echo '</strong> order</p>';
    
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
    	if ($_POST['order'] == "Acsending"){$order = "asc";}
    	elseif ($_POST['order'] == "Decending"){$order = "desc";}
    	}
    
    ?>
    
    <form id="selection" action="" method="post">
    	<select name="selection">
    		<option name="title" value="title" <?php echo (isset($_GET['selection']) && $_GET['selection'] == 'title') ? ' selected="selected"' : '' ; ?>>Alphabetic</option>
    		<option name="date" value="date" <?php if(!isset($_GET['submit']) && $_GET['submit'] == 'date') { echo 'selected="selected"' ;} ?>>Posted</option>
    		<option name="modified" value="modified" <?php echo (isset($_GET['selection']) && $_GET['selection'] == 'modified') ? ' selected="selected"' : '' ; ?>>Modified</option>
    		<option name="rand" value="rand" <?php echo (isset($_GET['selection']) && $_GET['selection'] == 'rand') ? ' selected="selected"' : '' ; ?>>Random</option>
    		<option name="comment_count" value="comment_count" <?php echo (isset($_GET['selection']) && $_GET['selection'] == 'comment_count') ? ' selected="selected"' : '' ; ?>>Popularity</option>
    		</select>
    	<input type="submit" name="order" value="Acsending">
    	<input type="submit" name="order" value="Decending">
    	</form>
    
    <?php
    
    echo '</div><!--end refine-->';
    
    $orderby = 'title';
    $order = 'asc';
    
    $cats = array(2, 3, 4, 5, 6 ,7, 8, 9, 10);
    foreach ($cats as $c){
    	$posts = query_posts($query_string . '&orderby=' . $orderby . '&order=' . $order . '&cat=' . $c);
    
    if($posts) :
    	while (have_posts()) :
    		the_post();
    ?>
    
    [html content]

    Thanks for looking,
    Sam

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Galaxy_High

    (@galaxy_high)

    I got my page to remember which &order= to remember by creating a cookie session. Still struggling with the &orderby= cookie.

    Any takers?

    <?php
    echo '<div id="refine">';
    echo '<p>Sorted ';
    
    switch ($_POST['selection']) {
    
    case 'title':
    echo $_POST['title'] . ' <strong>alphabetically</strong>';
    $orderby = "title";
    break;
    
    case 'date':
    echo $_POST['date'] . ' by <strong>date posted</strong>';
    $orderby = "date";
    break;
    
    case 'modified':
    echo $_POST['modified'] . ' by <strong>date modified</strong>';
    $orderby = "modified";
    break;
    
    case 'rand':
    echo $_POST['rand'] . ' <strong>randomly</strong>';
    $orderby = "rand";
    break;
    
    case 'comment_count':
    echo $_POST['comment_count'] . ' by <strong>popularity</strong>';
    $orderby = "comment_count";
    break;
    
    default:
    echo ' <strong>alphabetically</strong>';
    break;
    
    }
    
    echo ' in <strong>';
    
    switch ($_POST['order']) {
    
    case 'Ascending':
    echo $_POST['Ascending'] . 'ascending';
    break;
    
    case 'Descending':
    echo $_POST['Descending'] . 'descending';
    break;
    
    default:
    echo 'ascending';
    break;
    
    }
    
    echo '</strong> order</p>';
    
    if ($_SERVER["REQUEST_METHOD"] == "POST"){
    	if ($_POST['order'] == "Ascending"){$order = "asc";}
    	elseif ($_POST['order'] == "Descending"){$order = "desc";}
    	}
    ?>
    
    <form id="selection" action="" method="post">
    <select name="selection">
    <option name="title" value="title">Alphabetic</option>
    <option name="date" value="date">Posted</option>
    <option name="modified" value="modified">Modified</option>
    <option name="rand" value="rand">Random</option>
    <option name="comment_count" value="comment_count">Popularity</option>
    </select>
    <input type="submit" name="order" value="Ascending" title="Ascending">
    <input type="submit" name="order" value="Descending" title="Descending">
    </form>
    
    <?php
    session_start();
    if (isset($_POST['order'])){
    	$_SESSION['selection'] = $_POST['selection'];
    	$_SESSION['order'] = $_POST['order'];}
    
    echo $_SESSION['order'] . "\n";
    echo $_SESSION['selection'];
    echo '</div><!--end refine-->';
    ?>
    
    $cats = array(2, 3, 4, 5, 6 ,7, 8, 9, 10);
    foreach ($cats as $c){
    $posts = query_posts( $query_string . '&orderby=' . $_SESSION['selection'] . '&order=' . $_SESSION['order'] . '&cat=' . $c );
    
    if( $posts ) :
    	while (have_posts()) :
    		the_post();
    ?>
    
    [html content]

    Thanks for looking,
    Sam

    Thread Starter Galaxy_High

    (@galaxy_high)

    Fixed. I needed to change Descending and Ascending to desc and asc, respectively, to coincide with &order= in my query.

    Thanks for looking.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP and forms’ is closed to new replies.