doraemon0769 2011-08-16 07:03
浏览 31
已采纳

将选择框值发布到php mysql查询的问题

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?

  • 写回答

2条回答 默认 最新

  • douxia6163 2011-08-16 07:11
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置