2012-01-13 54 views
0

我原本有一個燈箱,一旦打開,只是從列表中的數據庫中提取所有品牌。問題是這個列表可能會很長,所以我將品牌分成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

一些爲你的僞代碼:

$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>"; 
+0

感謝,它幾乎存在,但不是很 - 這裏的HTML輸出我得到:http://jsfiddle.net/3mqKP/1/ – martincarlin87 2012-01-13 12:40:08

+0

得到它的工作,完美,謝謝! – martincarlin87 2012-01-13 12:49:45

相關問題