1

I cannot seem to find this anywhere.. I apologize if it's very simple.

I am trying to make simple drop-down box in html that displays a number of guestbook entries. I'm using php to process the database requests etc.. I'm fine with all of this. I just want to be able to have the default option on the selection box change depending on what number of entries has been selected.

Is there an easy way to do this? I have a feeling it has to do with a simple javascript call, but I'm still new to all of this.

This is the html:

<form id="displayform" action="guestbook.php" method="POST" >
Currently Displaying  <select name="display" id="display" onchange="document.getElementById('displayform').submit()">
<option value="10" > 10 </option>
<option value="25" > 25 </option>
<option value="50"> 50 </option>
<option value="ALL"> ALL </option>      
</select>   results per page
</form>
Dave
  • 723
  • 7
  • 19

2 Answers2

2

No need for javascript, this can be handled via PHP. Can even be refactored, too!

<?php
  $options = array(10, 25, 50, 'ALL');
  $selOpt = in_array($_POST['display'], $options) ? $_POST['display'] : $options[0];
?>
<form id="displayform" action="guestbook.php" method="POST">
  Currently Displaying
  <select name="display" id="display" onchange="document.getElementById('displayform').submit();">
    <?php foreach ($options as $option){ ?>
    <option value="<?=$option;?>"<?=($selOpt==$option?' select="selected"':'');?>>
      <?=$option;?>
    </option>
    <?php } ?>
  </select>
</form>
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • I'm trying to figure it out... but the onchange script is no longer working with this code. – Dave Apr 25 '12 at 14:55
  • 1
    @Dave: I capitalized the `ID` portion of `GetElementById`. Change that and all should be well. I'll also correct my code, my apologies. – Brad Christie Apr 25 '12 at 14:56
  • AHA, yes. I managed to get the other stuff sorted out. Thanks again, still new to this stuff, coming from C++ its been mostly easy, but the lack of a solid debugger.. bugs me out! – Dave Apr 25 '12 at 14:59
  • @Dave: No debugging? [What do you mean?](http://stackoverflow.com/questions/888/how-do-you-debug-php-scripts). ;-) – Brad Christie Apr 25 '12 at 15:00
  • oh awesome! I'm sorry to keep reviving this, but the line – Dave Apr 25 '12 at 15:27
  • @Dave: Try outputting both values, something like `echo "{$option},{$selOpt};";` and confirm they are both populated and both match, then go from there. Remember that `$selOpt` is coming from the POST value, so as long as it's being ran in the page that you submitted to (guestbook.php it looks like) all should work. – Brad Christie Apr 25 '12 at 15:46
  • Hi, thanks. I figured it out... the code doesn't look as compact as yours, but there was something up with the ternary operator. So I just fleshed it out with an if statement and it worked fine. – Dave Apr 25 '12 at 15:54
  • @Dave: Oops, that was my fault. I bounded between concatenation and breaking it out. Fixed my code as well. ;p – Brad Christie Apr 25 '12 at 15:57
1

You need to use the LIMIT clause in your SQL statement to limit how many items are returned by the query.

<?php
    $limit = (int) $_POST['display'];
    $sql = 'SELECT * FROM guestbook ORDER BY date_enterred LIMIT ' . $limit;
?>

This is a part of pagination. There are a lot of good tutorials for how to do this with PHP that can be found with a quick Google search.

John Conde
  • 217,595
  • 99
  • 455
  • 496