无需多次查询即可动态实现偏移和限制的最佳方法?

时间:2012-01-13 11:46:03

标签: php mysql refactoring

我最初有一个灯箱,一旦打开,只需从列表中的数据库中获取所有品牌。问题是这个列表可能很长,所以我将品牌拆分成10个列表并将它们向左浮动,以便它们水平而不是垂直填充灯箱,因为有大约40个品牌。

我的代码最初的问题是它不会覆盖,如果突然,另外10个品牌被添加到数据库,因为我的代码没有考虑它们。

其他问题是我对自己使用多个查询感到厌恶,所以我想知道是否有人可以提供任何有关如何实现以下更多优雅和效率的指示:

$sql2 = "SELECT page_id, name, url
             FROM " . DBTABLE_SITEMAP ."
             WHERE parent_id=$frames
             AND isActive=1
             AND isDeleted=0
             ORDER BY position ASC
             LIMIT 10";

$result2 = mysql_query($sql2, $db_conn);

if (mysql_num_rows($result2) > 0) {
    echo '<ol class="subnav-glasses">';

    while ($myrow2 = mysql_fetch_array($result2)) {
        $this_sub_page_id   = $myrow2["page_id"];
        $this_sub_name      = htmlentities($myrow2["name"]);
        $this_sub_url       = $myrow2["url"];


        echo "\n<li><a href=\"$this_sub_url\" title=\"$this_sub_name\">$this_sub_name</a>";                                
        echo '</li>';
    }
    echo "</ol>";
}


$sql3 = "SELECT page_id, name, url
             FROM " . DBTABLE_SITEMAP ."
             WHERE parent_id=$sitemap_designer_frames
             AND isActive=1
             AND isDeleted=0
             ORDER BY position ASC
             LIMIT 10
             OFFSET 10";

$result3 = mysql_query($sql3, $db_conn);

if (mysql_num_rows($result3) > 0) {
    echo '<ol class="subnav-glasses">';

    while ($myrow3 = mysql_fetch_array($result3)) {
        $this_sub_page_id   = $myrow3["page_id"];
        $this_sub_name      = htmlentities($myrow3["name"]);
        $this_sub_url       = $myrow3["url"];


        echo "\n<li><a href=\"$this_sub_url\" title=\"$this_sub_name\">$this_sub_name</a>";                                
        echo '</li>';
    }
    echo "</ol>";
}


$sql4 = "SELECT page_id, name, url
             FROM " . DBTABLE_SITEMAP ."
             WHERE parent_id=$sitemap_designer_frames
             AND isActive=1
             AND isDeleted=0
             ORDER BY position ASC
             LIMIT 10
             OFFSET 20";

$result4 = mysql_query($sql4, $db_conn);

if (mysql_num_rows($result4) > 0) {
    echo '<ol class="subnav-glasses">';

    while ($myrow4 = mysql_fetch_array($result4)) {
        $this_sub_page_id   = $myrow4["page_id"];
        $this_sub_name      = htmlentities($myrow4["name"]);
        $this_sub_url       = $myrow4["url"];


        echo "\n<li><a href=\"$this_sub_url\" title=\"$this_sub_name\">$this_sub_name</a>";                                
        echo '</li>';
    }
    echo "</ol>";
}


    $sql5 = "SELECT page_id, name, url
             FROM " . DBTABLE_SITEMAP ."
             WHERE parent_id=$sitemap_designer_frames
             AND isActive=1
             AND isDeleted=0
             ORDER BY position ASC
             LIMIT 10
             OFFSET 30";

$result5 = mysql_query($sql5, $db_conn);

if (mysql_num_rows($result5) > 0) {
    echo '<ol class="subnav-glasses">';

    while ($myrow5 = mysql_fetch_array($result5)) {
        $this_sub_page_id   = $myrow5["page_id"];
        $this_sub_name      = htmlentities($myrow5["name"]);
        $this_sub_url       = $myrow5["url"];


        echo "\n<li><a href=\"$this_sub_url\" title=\"$this_sub_name\">$this_sub_name</a>";                                
        echo '</li>';
    }
    echo "</ol>";
}

1 个答案:

答案 0 :(得分:1)

一些伪代码:

$sql2 = "SELECT page_id, name, url
         FROM " . DBTABLE_SITEMAP ."
         WHERE parent_id=$frames
         AND isActive=1
         AND isDeleted=0
         ORDER BY position ASC";

$result2 = mysql_query($sql2, $db_conn);

$i=0;
while ($myrow = mysql_fetch_array($result2))
{
  if ($i % 10 === 0)
  {
     if ($i > 0)
       echo "</ol>";

     echo "<ol>";
  }

  echo "<li>dsoajda</li>";

  $i++;
}

if ($i > 0)
  echo "</ol>";