PHP and forms
-
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)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘PHP and forms’ is closed to new replies.