PHP分页。结束数组具有与初始查询不同的值数

时间:2017-04-03 23:40:30

标签: php mysql pagination smarty gammu

到目前为止,我一直在使用MySQL LIMIT限制我的页面以进行任何分页。它一直表现良好。

然而......现在我编写了一个应用程序,它从3个独立的MySQL表中获取数据,之后我使用array_multisort()对它们进行排序。

这里的主要问题是它用于短信(gammu smsd)。如果我写一条包含900个字符的消息 - 它将被拆分为数据库中的7个条目。每139个字符拆分(有时可能会有所不同)  下面的代码根据数据库中的“SequencePosition”字段修复了这个问题。 因此,如果我使用MySQL限制它,我的结果将比我指定的少,因为MySQL不知道我以后对数据做了什么。

我希望你们这对我们有意义。

您如何解决这个问题?你能想到更好的方法吗?我的意思是 - 从表中获取所有数据很少是一个好主意;(

请参阅下面的PHP代码。

<?php    
if(isset($_SESSION['id']))
{

    if(isset($_GET['contact_number']) && ($_GET['contact_name']))
    {
        if (isset($_GET["page"]))
        {
            $page  = $_GET["page"];
        }
        else
        {
            $page = 1 ;
        }
        $gmessages_page = 30; //this is actually a static value, normally it resides in a config file
        $start_from = ($page-1) * $gmessages_page; 

        $contact_number = base64_decode($_GET['contact_number']);
        $contact_name = base64_decode($_GET['contact_name']);

        $query_inbox = "SELECT ReceivingDateTime, SenderNumber, TextDecoded FROM inbox WHERE SenderNumber='$contact_number' ORDER BY ReceivingDateTime";
        $query_sentitems = "SELECT SendingDateTime, DestinationNumber, TextDecoded, Status, SequencePosition FROM sentitems WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";
        $query_outbox = "SELECT SendingDateTime, DestinationNumber, TextDecoded FROM outbox WHERE DestinationNumber='$contact_number' ORDER BY SendingDateTime";


        $result_inbox = $conn->query($query_inbox);
        $result_sentitems = $conn->query($query_sentitems);
        $result_outbox = $conn->query($query_outbox);

        if(!$result_inbox || !$result_sentitems || !$result_outbox)
        {
            die("Błąd połączenia z bazą (!RESULT)");
        }

        $rows_inbox = $result_inbox->num_rows;
        $rows_sentitems = $result_sentitems->num_rows;
        $rows_outbox = $result_outbox->num_rows;

        if($rows_inbox == 0 || $rows_sentitems == 0)
        {
            $conn->close();
        }

        //inbox

        $inbox_person = $contact_name;
        for($j = 0; $j < $rows_inbox ; ++$j)
        {
            $result_inbox->data_seek($j);
            $row_inbox = $result_inbox->fetch_array(MYSQLI_NUM);

            $inbox_date[] = $row_inbox[0];
            $inbox_number = $row_inbox[1];
            $inbox_text[] = $row_inbox[2];
            $sent_status[] = 0;
            $sent_sequence_position[] = 0;
            $inbox_type[] = 1;
        }

        for($j = 0; $j < $rows_sentitems ; ++$j)
        {
            $result_sentitems->data_seek($j);
            $row_sentitems = $result_sentitems->fetch_array(MYSQLI_NUM);

            if($row_sentitems[4] == 1)
            {
                $sent_sequence_position[] = $row_sentitems[4];
                $inbox_date[] = $row_sentitems[0];
                $inbox_text[] = $row_sentitems[2];
                $sent_status[] = $row_sentitems[3];
                $inbox_type[] = 2;                  
            }
            else
            {
                $inbox_text[sizeof($inbox_type) - 1] .= $row_sentitems[2];         
            }
        }


        for($j = 0; $j < $rows_outbox ; ++$j)
        {
            $result_outbox->data_seek($j);
            $row_outbox = $result_outbox->fetch_array(MYSQLI_NUM);
            $inbox_date[] = $row_outbox[0];
            $inbox_text[] = $row_outbox[2];
            $sent_status[] = "QueuedForSending";
            $inbox_type[] = 2;
        }
        $number_of_records = sizeof($inbox_date);
        $number_of_pages = ceil($number_of_records / $gmessages_page);   

        array_multisort($inbox_date, $inbox_text, $inbox_type, $sent_status, $sent_sequence_position);

        $smarty->assign("inbox_date", $inbox_date);
        $smarty->assign("inbox_text", $inbox_text);
        $smarty->assign("inbox_number", $inbox_number);
        $smarty->assign("inbox_person", $contact_name);
        $smarty->assign("inbox_type", $inbox_type);
        $smarty->assign("sent_status", $sent_status);
        $smarty->assign("start_from", $start_from);
        $smarty->assign("gmessages_page", $gmessages_page);
        $smarty->assign("number_of_pages", $number_of_pages);
        $smarty->assign("number_of_records", $number_of_records);
        $smarty->assign("sent_sequence_position", $sent_sequence_position);
        $smarty->assign("page", $page);

        //for $_GET after sending SMS
        $smarty->assign("contact_number_enc", $_GET['contact_number']);
        $smarty->assign("contact_name_enc", $_GET['contact_name']);


        $smarty->display('templates/conversation_body.tpl');
    }
}
else
{
    $smarty->display('templates/login_body.tpl');
}

0 个答案:

没有答案