Я создал функцию для отображения многомерного массива (из таблицы БД), упорядоченного по родительскому идентификатору (pid). Мне нужно добавить возможность подкачки страниц, но когда я добавляю «LIMIT $ offset, $ recsperpage» к моему запросу SELECT, я получаю ненадежные результаты. В моей тестовой БД 14 записей, с LIMIT $ recsperpage, установленным на 10, он показывает только родительские элементы.
Например, без LIMIT результаты:
Home
About Us
Our History
Our Founders
Our Clients
Faq
Services
Corporate Services
Property Services
Employment Services
Foundation Services
Licensing Services
Tax Services
Contact Us
Если для $ recsperpage установлено значение 10, результаты на странице 1 будут следующими:
Home
About Us
Services
Тогда на странице 2:
Contact
Мои подстраницы отсутствуют. Что еще хуже, если я установлю $ recsperpage в 6, я получу те же результаты на страницах 1 & 2, но затем на странице 3 появляется ошибка «Неопределенное смещение: 1», указывающая на вызов функции (как показано ниже). Я действительно в замешательстве, и я мог бы помочь.
Вот мой запрос:
mysqli_select_db($isiteadmin, $database_isiteadmin);
$query_getText = "SELECT page_id, pid, page_name, link_name, headline, deck, ordr FROM webtext ORDER BY pid, ordr LIMIT $offset, $recsperpage";
$getText = mysqli_query($isiteadmin, $query_getText) or die('Sorry, could not connect to database.');
$row_getText = mysqli_fetch_assoc($getText);
Вот моя конструкция массива:
// Initialize the storage array:
$pagelist = array();
while (list($page_id, $pid, $page_name, $link_name, $headline, $deck, $ordr ) = mysqli_fetch_array($getText, MYSQL_BOTH)) {
// Add to the array:
$pagelist[$pid][$page_id] = array('pgid'=>$page_id,'pid'=>$pid, 'pgname'=>$page_name, 'linkn'=>$link_name,'hdline'=>$headline,'deck'=>$deck,'ordr'=>$ordr );
}
Вот вызов функции:
// Send the first array element
// to the make_list() function:
make_list($pagelist[1]);
Вот функция:
function make_list ($parent, $depth = 0) {
// Need the main $pagelist array plus other variables used within this function:
global $pagelist, $counter, $contentdir, $pid;
// Loop through each limited subarray:
foreach ($parent as $pid=>$item) {
// Display the item:
?>
<tr class="pglist <?php if (isset($pagelist[$pid])) { echo 'haschild'; } else { echo ''; } ?> <?php if ($counter++ % 2) {echo 'hilite';} ?> <?php echo 'T' . $depth; ?>">
<td class="pgname "><?php if ((!isset($pagelist[$pid])) && ($item['pid'] == 1)) { echo $item['pgname']; } // category page without decendents
if ((isset($pagelist[$pid])) && ($item['pid'] == 1)) { echo '* ' . $item['pgname']; } // category page with decendents
if ((isset($pagelist[$pid])) && ($item['pid'] != 1)) { echo '<span class="el"> L *</span> ' . $item['pgname']; } // child page with decendents
if ((!isset($pagelist[$pid])) && ($item['pid'] != 1)) { echo '<span class="el"> L</span> ' . $item['pgname']; } // child page without decendents ?></td>
<td class="hdline"><?php echo $item['hdline']; ?></td>
<td class="deck"><?php echo $item['deck']; ?></td>
<td class="order"><?php echo $item['ordr']; ?></td>
</tr>
<?php
// Check for sub-items:
if (isset($pagelist[$pid])) {
// Call this function:
make_list($pagelist[$pid], $depth = ($depth + 1));
$depth = ($depth - 1);
} // End check for subitems
} // End of FOREACH loop.
} // End of make_list() function.
Вот var_dump массива:
Array
(
[1] => Array
(
[2] => Array
(
[pgid] => 2
[pid] => 1
[pgname] => Home
[linkn] => home
[hdline] => This is the headline for the home page
[deck] =>
[ordr] => 1
)
[3] => Array
(
[pgid] => 3
[pid] => 1
[pgname] => About Us
[linkn] => about
[hdline] => This is the headline for About Us
[deck] =>
[ordr] => 2
)
[6] => Array
(
[pgid] => 6
[pid] => 1
[pgname] => Service Areas
[linkn] => practice
[hdline] => This is the Service Areas page headline
[deck] =>
[ordr] => 4
)
[21] => Array
(
[pgid] => 21
[pid] => 1
[pgname] => Contact Us
[linkn] => contact
[hdline] => Contact Us
[deck] =>
[ordr] => 99
)
)
[3] => Array
(
[10] => Array
(
[pgid] => 10
[pid] => 3
[pgname] => Our History
[linkn] => history
[hdline] => This is the Our History page headline
[deck] =>
[ordr] => 1
)
[11] => Array
(
[pgid] => 11
[pid] => 3
[pgname] => Our Clients
[linkn] => clients
[hdline] => This is the Our Clients page headline
[deck] =>
[ordr] => 2
)
[12] => Array
(
[pgid] => 12
[pid] => 3
[pgname] => FAQ
[linkn] => faq
[hdline] => Frequently Asked Questions
[deck] =>
[ordr] => 3
)
)
[6] => Array
(
[7] => Array
(
[pgid] => 7
[pid] => 6
[pgname] => Corporate Services
[linkn] => corporate
[hdline] => This is the Corporate Services page headline
[deck] =>
[ordr] => 1
)
[9] => Array
(
[pgid] => 9
[pid] => 6
[pgname] => Property Services
[linkn] => intellectual
[hdline] => This is the Property Services page headline
[deck] =>
[ordr] => 2
)
[13] => Array
(
[pgid] => 13
[pid] => 6
[pgname] => Employment Services
[linkn] => employment
[hdline] => This is the Employment Services page headline
[deck] =>
[ordr] => 4
)
[14] => Array
(
[pgid] => 14
[pid] => 6
[pgname] => Foundation Services
[linkn] => formation
[hdline] => This is the Foundation Services page headline
[deck] =>
[ordr] => 5
)
[15] => Array
(
[pgid] => 15
[pid] => 6
[pgname] => Licensing Services
[linkn] => contracts
[hdline] => This is the Licensing Services page headline
[deck] =>
[ordr] => 6
)
[16] => Array
(
[pgid] => 16
[pid] => 6
[pgname] => Tax Services
[linkn] => tax
[hdline] => This is the Tax Services page headline
[deck] =>
[ordr] => 7
)
)
[10] => Array
(
[41] => Array
(
[pgid] => 41
[pid] => 10
[pgname] => Our Founders
[linkn] => founders
[hdline] => Our Founders
[deck] => This is info about our company founders.
[ordr] => 1
)
)
[17] => Array
(
[18] => Array
(
[pgid] => 18
[pid] => 17
[pgname] => News Stories
[linkn] => news
[hdline] => News Stories
[deck] =>
[ordr] => 1
)
[19] => Array
(
[pgid] => 19
[pid] => 17
[pgname] => Upcoming Events
[linkn] => events
[hdline] => Upcoming Events
[deck] =>
[ordr] => 2
)
)
)
Задача ещё не решена.
Других решений пока нет …