多维数组的分页返回不可靠的结果

时间:2015-04-21 21:01:55

标签: php arrays recursion paging

我创建了一个函数来显示由父id(pid)排列的多维数组(来自DB表)。我需要添加分页功能,但是当我添加" 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页和第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">&nbsp;L *</span> ' . $item['pgname']; } // child page with decendents
                                        if ((!isset($pagelist[$pid])) && ($item['pid'] != 1)) { echo '<span class="el">&nbsp;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
            )

      )

)

0 个答案:

没有答案