1

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

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

这里的主要问题是它用于 SMS 消息(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');
}
4

0 回答 0