2014-05-18 111 views
0

我想創建多列表的記錄與每個列表4記錄而不知道有多少記錄。但是,我無法弄清楚如何處理數學。我手動輸入$ n == 5 || $ n == 9等知道它是有條件的,並不能完全解決問題。任何人都可以幫助我如何處理。而且,只有在記錄總數不能被4除以4的情況下,下面的列表才能正常工作。如果可以,它將在最後創建一個空列表。創建每4個記錄的列表

$query = "SELECT * FROM `table` WHERE `field` = $whatever"; 
if ($result = $con->query($query)){ 
$n = 1 
$row_cnt = $result->num_rows; 
$total_lists = round($row_cnt/4, 0); 
$current_list = 1; 
echo "<ul>List $current_list of $total_lists"; 
    while ($row = $result->fetch_assoc()) { 
    echo "<li>$row['something']</li>"; 
     if ($n == 5 || $n == 9 || $n == 13 || $n == 17 || $n == 21 || $n ==25 || $n ==29 || $n == 33 || $n == 37 || $n == 41 || $n == 45 || $n == 49 || $n == 53 || $n == 57 || $n == 61 || $n == 65 || $n == 69 || $n == 73 || $n == 77){ 
     echo "</ul>"; 
     $current_list = $current_list + 1; 
     echo "<ul>List $current_list of $total_lists"; 
     } 
    $n = $n + 1; 
    } 
echo "</ul>"; 
} 

在此先感謝您的幫助。 :)

解決:

$query = "SELECT * FROM `table` WHERE `field` = $whatever"; 
if ($result = $con->query($query)){ 
$n = 0 
$row_cnt = $result->num_rows; 
$total_lists = ceil($row_cnt/4); 
$current_list = 1; 
echo "<ul>List $current_list of $total_lists"; 
    while ($row = $result->fetch_assoc()) { 
     $n++; 
    echo "<li>$row['something']</li>"; 
     if ($row_cnt > 4) { 
    if ($n % 4 === 0) { 
    echo "</ul>"; 
    $current_list = $current_list + 1; 
    echo "<ul>List $current_list of $total_lists"; 
     } 
     } 
    } 
echo "</ul>"; 
} 
+1

有人聽說過模運算符:http://www.php.net/manual/en/language.operators.arithmetic.php? – CodeZombie

+0

或者,您可以將數組結果('array_chunk()')四捨五入,然後將它們分組爲四,然後相應地循環它們。 – user1978142

回答

2

你其實並不是太遙遠的軌道,從你可能想幹什麼,你只需要使用一個額外的工具來完成它:在%或模運算符。

模數運算符將返回除法問題的其餘部分:

$x = 5 % 2; // 1 

在你的邏輯尋找,你的動作需要採取當你的增量器($n)減1除以4將具有的0剩餘:

if (($n - 1) % 4 === 0) 
{ 
    //your <ul> insertion could go here. 
} 

下面是PHP手冊頁面的鏈接討論數學運算:

http://us1.php.net/manual/en/language.operators.arithmetic.php

+0

非常感謝!第一次遇到模數運算符,它適用於我的情況! :) –

+0

順便說一下,如果記錄的總數可以平均除以4,我仍然有最後一頁是空白的。我想我可以用另一種愚蠢的低效方式來處理:p –

+0

@MichaelEugeneYuen你有' $ current_list'變量(跟蹤你當前的列表),並且你之前計算了要輸出的列表總數('$ total_lists')。你可以檢查這兩個數字是否相等。如果他們這樣做,你可以在打開最終的空白列表之前將其「斷開」:http://us2.php.net/break – myesain

0

或者,您可以在數據庫結果上使用簡單的array_chunk()。提供適當的分組(在本例中爲四),並通過foreach循環它們。考慮這個例子:

// dummy data (values from db) 
$values_from_db = array(
    array('id' => 1, 'something' => 'list1'), 
    array('id' => 2, 'something' => 'list2'), 
    array('id' => 3, 'something' => 'list3'), 
    array('id' => 4, 'something' => 'list4'), 
    array('id' => 5, 'something' => 'list5'), 
    array('id' => 6, 'something' => 'list6'), 
    array('id' => 7, 'something' => 'list7'), 
    array('id' => 8, 'something' => 'list8'), 
    array('id' => 9, 'something' => 'list9'), 
); 

$values_from_db = array_chunk($values_from_db, 4); // group them by four's 

?> 

<?php foreach($values_from_db as $key => $value): ?> 
    <ul style="list-style-type: none;"> 
    <?php foreach($value as $index => $element): ?> 
     <li><?php echo $element['id'].'.'.$element['something']; ?></li> 
    <?php endforeach; ?> 
    </ul> 
<?php endforeach; ?> 
+0

謝謝。我會試試這個,也:) –

相關問題