skip to Main Content

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

3

Answers


  1. 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++){
    
     }
    
    Login or Signup to reply.
  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;
    
    Login or Signup to reply.
  3. 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>";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search