0

I have a page of images populated with information queried with PHP from a MySQL database. I'm attempting to split the query results into pages, and have a select box to allow users to decide how many results per page.

Problem is, I'm stuck! I'm using javascript to post the select box onChange, but don't know how to get that result into the query ($limit). I also have no idea how to get the page number (represented by $curPage in the query).

Hope you can fix this and fill in the blanks:

<form name=imgNum action="new_arrivals_img.php" method=POST>
  <p>
    <select name=ComboName size=1 onChange="imgNum.submit();">
      <option value="12" SELECTED>12</option> 
      <option>16</option> 
      <option>20</option> 
    </select>
  </p>
</form>

<ul class="new_arrivals_gallery">

  <?php
    $mysql_link = mysql_connect("localhost", "root", "root");   
    mysql_select_db("new_arrivals_imgs") or die("Could not select database");

    $query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
    "ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());

    if(!$query) {
      echo "Cannot retrieve information from database.";
    } else { 
      while($row = mysql_fetch_assoc($query)) { 
        echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
      }
    }
  ?>
</ul>

Also, is it ok that the the form action points to the current page? Or do I have to have two pages?

1
  • <form name="imgNum">, <select name="ComboName"> <option selected="selected"> .... Test your HTML code with validator.w3.org Commented Aug 16, 2011 at 7:18

2 Answers 2

1

change method from post to get, to pass on the variables ... and check for page:

<?php
     $curPage = 0;
     if(isset($_GET['page'])){
        $curPage = (int) $_GET['page'];
     }
?>
<form name=imgNum action="new_arrivals_img.php" method="get">
  <p>
    <select name=ComboName size=1 onChange="imgNum.submit();">
      <option value="12" SELECTED>12</option> 
      <option>16</option> 
      <option>20</option> 
    </select>
    <input type="hidden" name="page" value="<?php echo $curPage; ?>" />
  </p>
</form>

<ul class="new_arrivals_gallery">

  <?php
    if((int) $_GET['ComboName'] > 0){ 
        $limit = (int) $_GET['ComboName']; 
    } else { 
        $limit = 12; 
    }
    $mysql_link = mysql_connect("localhost", "root", "root");   
    mysql_select_db("new_arrivals_imgs") or die("Could not select database");

    $query = mysql_query("SELECT `imgURL`,`imgTitle` FROM `images` ".
    "ORDER BY `imgDate` DESC LIMIT " . $limit * $curPage . ", $limit") or die(mysql_error());

    if(!$query) {
      echo "Cannot retrieve information from database.";
    } else { 
      while($row = mysql_fetch_assoc($query)) { 
        echo "<li><a href='new_arrivals_img/".$row['imgURL']."' class='gallery' title='".$row['imgTitle']."'><img src='new_arrivals_img/thumbnails/".$row['imgURL']."'></a></li>";
      }
    }
  ?>
</ul>
Sign up to request clarification or add additional context in comments.

4 Comments

Excellent! Thank you so much. But how do I get the select box to load showing the current setting? For example, if it defaults to 12, then 12, but if it is changed to 16, then when the page reloads, it shows 16?
hmmm it doesn't seem to be working. When the page reloads, it still shows "12" as the selected number in the combo box.
in combobox you should change for every option <option value="12"<?php if($_GET['page'] == 12 || !isset($_GET['page']){ echo " SELECTED"; } ?>12</option> ... etc
thanks again for solving this for me. I'm curious how difficult it would be to get Ajax involved by using jQuery. I'm stuck as to how to do it. Any advice?
1

it's in $_POST["ComboName"] variable:

$limit = (isset($_POST["ComboName"]) ? $_POST["ComboName"] : 16); // 16 is some default value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.