Get some desired rows from table by its order

21
December 28, 2018, at 05:40 AM

I'm working for a page where it shows a tables from database. Let's say I have 100 rows. How to show 21st-30th row?

In the table, there's some fields, not only email, name, and description. So, I used mysqli_fetch_assoc

for($num=21;$num<=30;$num++){
  $row=mysqli_fetch_assoc($result);
  echo "<tr>";
  echo "<td>".$num."</td>";
  echo "<td>".$row['email']."</td>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['description']."</td>";
  echo "</tr>";
}

The $num shows the desired number but the table itself shows the data from 1st-10th. I want it shows the data from 21st-30th

Answer 1

You can already fix your code with sql queries (for faster performance) adding LIMIT 20, 10 (get 10 records beginning with row 21)

but if you have other logic to be applied on your code (as if your displaying dynamic records)

$row=mysqli_fetch_assoc($result);
for($num=0; $num<=$result->num_rows; $num++) {
    if($num>=21 && $num<=30){
        echo "<tr>";
        echo "<td>".$num."</td>";
        echo "<td>".$row['email']."</td>";
        echo "<td>".$row['name']."</td>";
        echo "<td>".$row['description']."</td>";
        echo "</tr>";
    }
}

other thing you might consider is using foreach

$row=mysqli_fetch_assoc($result);
$num = 0;
foreach (range(21, 30) as $row) {
    $num++;
    echo "<tr>";
    echo "<td>".$num."</td>";
    echo "<td>".$row['email']."</td>";
    echo "<td>".$row['name']."</td>";
    echo "<td>".$row['description']."</td>";
    echo "</tr>";
}
Answer 2

Please, try with sql query, where you defined limit of rows like start from row no to end of row no. I wrote one sql query at bellow:

Syntax:

SELECT column1, column2, ...
FROM table_name
LIMIT offset, count;

Example:

 SELECT * from MYTABLE LIMIT 20,10;
Answer 3

Two possibilites you can doing this way

First

SELECT * from MYTABLE LIMIT 20, 10

Second

// 0,1,2,3....19 19 is record of 20
for($num=19;$num<=29;$num++){
 }
Answer 4

You may try like below.

SELECT * FROM Tablename WHERE id BETWEEN 21 AND 30

Where, id -> Will be the unique key of the table.

Thanks.

READ ALSO
How to use phpseclib in codeigniter?

How to use phpseclib in codeigniter?

I placed the phpseclib files in my application/third_party folder, I have included the path in my controller as shown below

53
strtotime() expects parameter 1 to be string

strtotime() expects parameter 1 to be string

I am write code in for loop , for multiple insert value using codeigniter framework

55
why there is performance difference when retrieving data from view vs underlying select of that view

why there is performance difference when retrieving data from view vs underlying select of that view

I am doing query on view with single predicates which gives me the record in 4-7 seconds, but when i try to retrieve the record with same predicate and directly with underlying query from that view it gives me records in less then secondsI am using MySQL

27