0

我有一个 PHP 脚本,它在一个漂亮的网格中显示一堆数据库结果并将它们分页。

我试图弄清楚如何创建一种方法来使用下拉系统更改每页显示的结果数量,因此我开始使用 AJAX。

我有一个变量来确定要显示的项目数量,所以我所做的是我创建了更多的页面,这些页面具有完全相同的 PHP 代码,但该变量的编号不同。它做了它打算做的事情,但我不禁觉得我已经以可以想象的最低效的方式来做这件事,因为我必须使用不同的变量创建三个额外的页面。

如何改进?

这是php代码。我曾经将它放在页面本身中,但已将其移动到 ajax 查看的另一个文件中。

<?php


                //Add the file that connects to the database
                include("C://wamp/www/site/config.php");    
                //Selects which database to get the data from
                mysql_select_db("products");

                //Variables for pagination 

                $per_page = 24;                 
                // get the number of items that actually have Oven in their name
                $sql = "SELECT SUM(rowcount)
                        FROM (
                        SELECT COUNT(1) AS rowcount 
                        FROM brand1
                        WHERE description LIKE '%oven%' 
                        UNION ALL 
                        SELECT COUNT(1) 
                        FROM brand2
                        WHERE description LIKE '%oven%'
                        ) AS counts";
                $pages_query = mysql_query ($sql) ;
                //Ceil rounds up to the nearest number so that we don't get pages with decimals on the end
                $pages = ceil(mysql_result($pages_query, 0) / $per_page);

                //append a page=1,2,3 at the end of the URL to indicate which page the user is on

                //set : 1 so that if a person doesn't pick a page it automatically sets them on page 1.
                $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;

                $start = ($page -1) * $per_page;

                //Selects which table to extract the data from
                $i = 0;
                $result = mysql_query("SELECT * FROM brand1 WHERE description LIKE '%oven%' UNION ALL SELECT * FROM brand2 WHERE description LIKE '%oven%' LIMIT $start, $per_page") ;
                $dyn_table = '<table border ="1" cellpadding ="10">';

                //create a variable to store the data in
                while($row = mysql_fetch_array($result))
                    {
                        //Variables to store vendors information, product number and image as retrieved from the database
                        $brand = $row["Brand"];
                        $model_number = $row["Model Number"];
                        $product_image = $row["Image"];

                        //Set up a loop that puts the information into a grid rather than a single line
                        if($i%4==0) //First number sets the column number
                            {
                                // Starts to draw the table. Adds the vendor name, the product number then breaks line to draw the image underneath the titles
                                $dyn_table .= '<tr><td>' .  $brand . " " . $model_number . '<br>' . $product_image .'</td>';
                            }   
                        else
                            {
                                //This line does the same thing, but once the first if condition hits 4, it jumps to this line to contine drawing.
                                $dyn_table .= '<td>' . $brand . " " . $model_number . '<br>' . $product_image . '</td>';
                            }
                        //Simply adds 1
                        $i++;
                    }
                    //Adds the ending of the table
                    $dyn_table .= '</tr></table>';

                    //Draws the table
                    echo $dyn_table;

                    //Set the pagination links at the bottom of the page
                    if ($pages >=1 && $page <= $pages)
                        {
                            for($x=1; $x<=$pages; $x++)
                                {
                                    echo ($x == $page) ? '<strong><a href ="?page='.$x.'">'.$x.'</a></strong> ' : '<a href ="?page='.$x.'">'.$x.'</a> ';
                                }
                        }
            ?>  

还有 AJAX(我刚刚从 W3 中拿了一个例子

  <script>
function loadXMLDoc()
{
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","http://localhost/site/products/ovens/products.php",true);
    xmlhttp.send();
}
</script>
4

2 回答 2

0

By your description, I assume you have something like page1.php, page2.php...?

Make a page.php, read $_GET['page'] to see which page you should be doing, and in HTML link to page.php?page=2 and similar.

As Charles says, that's as much as I can offer with the level of detail you provide.

于 2012-12-10T05:26:49.513 回答
0

在这里,有一个大胖无聊的答案。

您的代码很好,除了来自家庭的整个污点mysql_和史前手动 ajax 的东西。您的分页方法是可靠的,并遵循通常的模式,即找出您可能拥有多少页,然后根据该限制抓取当前页面的结果。你也有所有正确的验证点和点。

你是对的,这有点低效,但除非你有一个方便的地方来粘贴预先计算的最大结果数/最大页数,否则几乎没有办法解决它。你很可能已经有了一个。仔细检查您的数据库结构。您还应该确保您有适当的索引。是时候玩了EXPLAIN

我唯一可以推荐的可能是无关的。看起来您可能需要阅读数据库规范化。您的表名(brand1、brand2)表明您可能有两个结构完全相同的表,唯一的区别是它们包含用于两个不同“品牌”的数据。这可以很容易地在一个表中完成,并将品牌添加为另一列。

于 2012-12-10T05:48:19.680 回答