简体   繁体   English

选择特定的MYSQL行

[英]Selecting specific MYSQL rows

I have a page that's suppose to create a Div for every 9 entries in the database. 我有一个页面想为数据库中的每9个条目创建一个Div。 Every Div will consist of 3 ULs and every UL will consist of 3 LIs. 每个Div将包含3个UL,每个UL将包含3个LI。 Something like this: 像这样:

Demo 演示

So, every LI is where each entry is displayed and every Div is essentially a unique page. 因此,每个LI都是显示每个条目的位置,每个Div本质上都是一个唯一的页面。

This is my code so far: 到目前为止,这是我的代码:

    $sql = mysql_query("SELECT * FROM `reviews`") or die(mysql_error());
    $e_count = mysql_num_rows($sql);
    $r_pages = ceil($e_count / 9);
    $x = 1;
    $y = 1;
    if($e_count > 9){ // if there are more than 9 entries in the database           
        for($x=1;$x<=$r_pages;$x++){ // creates a div for every 9 items in the database 
            echo '<div class="rp_pages" id="rp_page_'.$x.'">';
                for($y=1;$y<=3;$y++){ // creates 3 ULS in each "page" 
                    echo '<ul>';
                       // 3 lis should appear here      
                    echo '</ul>';                       
                }
            echo '</div>';
        }                           
    }

The problem is, i don't want to use multiple queries with a LIMIT in them to select the respective entries. 问题是,我不想使用多个带有LIMIT的查询来选择相应的条目。 Can this be done with just a single query? 只需一个查询就能完成吗?

I only see one query (the first select). 我只看到一个查询(第一个选择)。 After that, instead of using it to count the number of rows, parse each row and extract the review text, this way: 之后,不使用它来计算行数,而是分析每行并提取审阅文本,方法是:

Let's imagine that the reviews table has a text column where the review text is saved. 假设reviews表有一个text列,用于保存评论文本。 Having your code: 拥有您的代码:

$sql = mysql_query("SELECT * FROM `reviews`") or die(mysql_error());
$e_count = mysql_num_rows($sql);
$r_pages = ceil($e_count / 9);
$x = 1;
$y = 1;
if($e_count > 9){ // if there are more than 9 entries in the database           
    for($x=1;$x<=$r_pages;$x++){ // creates a div for every 9 items in the database 
        echo '<div class="rp_pages" id="rp_page_'.$x.'">';
            for($y=1;$y<=3;$y++){ // creates 3 ULS in each "page" 
                echo '<ul>';

                // Here the code that extract the rows from the query.
                for($z=1;$z<=3;$z++){
                    echo '<li>';
                    if($data = mysql_fetch_array($sql, MYSQL_ASSOC)) {
                        echo $data['text']; // Use some type of parser to show special characters like < or > as text, so you are safe from code injection.
                    }
                    echo '</li>';
                }


                echo '</ul>';                       
            }
        echo '</div>';
    }                           
}

As you can see, it creates a new loop to create the 3 li s and in each li extracts the text selected in the query, then echo es it. 如您所见,它创建了一个新循环来创建3个li并在每个li提取查询中选择的text ,然后对其进行echo

As http://php.net/manual/en/function.mysql-fetch-array.php says: 正如http://php.net/manual/en/function.mysql-fetch-array.php所说:

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead. 返回与获取的行相对应的数组,并将内部数据指针向前移动。

So the next time you fetch a row, it will be the next one. 因此,下次您提取一行时,它将是下一个。

PD: PHP's mysql is deprecated. PD:不推荐使用PHP的mysql Use mysqli instead. 改用mysqli Is the same but with another library that is currently under developement, not like mysql . 是相同的,但与另一个正在开发中的库类似,而不是像mysql

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM