Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:05:23+00:00 2026-05-27T23:05:23+00:00

Hi everyone I’ve been extracting rows from a SQL table 1 by 1. I

  • 0

Hi everyone I’ve been extracting rows from a SQL table 1 by 1. I wrote this code two years ago and realise how hideously ineffective it is. I’d like to speed things up by typing up a simple for-loop to automate the coding.

$maxRows_dd1 = 10;
$pageNum_dd1 = 0;
if (isset($_GET['pageNum_dd1'])) {
  $pageNum_dd1 = $_GET['pageNum_dd1'];
}
$startRow_dd1 = $pageNum_dd1 * $maxRows_dd1;

$maxRows_dd2 = 10;
$pageNum_dd2 = 1;
if (isset($_GET['pageNum_dd2'])) {
  $pageNum_dd2 = $_GET['pageNum_dd2'];
}
$startRow_dd2 = $pageNum_dd2 * $maxRows_dd2;

$maxRows_dd3 = 10;
$pageNum_dd3 = 2;
if (isset($_GET['pageNum_dd3'])) {
  $pageNum_dd3 = $_GET['pageNum_dd3'];
}
$startRow_dd3 = $pageNum_dd3 * $maxRows_dd3;

… dd4 to dd99 go in between!

$maxRows_dd100 = 10;
$pageNum_dd100 = 99;
if (isset($_GET['pageNum_dd99'])) {
  $pageNum_dd32 = $_GET['pageNum_dd99'];
}
$startRow_dd99 = $pageNum_dd99 * $maxRows_dd99; 

which corresponds to:

mysql_select_db($database_rent, $rent);
$query_dd1 = "SELECT * FROM rent";
$query_limit_dd1 = sprintf("%s LIMIT %d, %d", $query_dd1, $startRow_dd1, $maxRows_dd1);
$dd1 = mysql_query($query_limit_dd1, $rent) or die(mysql_error());
$row_dd1 = mysql_fetch_assoc($dd1);

if (isset($_GET['totalRows_dd1'])) {
  $totalRows_dd1 = $_GET['totalRows_dd1'];
} else {
  $all_dd1 = mysql_query($query_dd1);
  $totalRows_dd1 = mysql_num_rows($all_dd1);
}
$totalPages_dd1 = ceil($totalRows_dd1/$maxRows_dd1)-1;

mysql_select_db($database_rent, $rent);
$query_dd2 = "SELECT * FROM rent";
$query_limit_dd2 = sprintf("%s LIMIT %d, %d", $query_dd2, $startRow_dd2, $maxRows_dd2);
$dd2 = mysql_query($query_limit_dd2, $rent) or die(mysql_error());
$row_dd2 = mysql_fetch_assoc($dd2);

if (isset($_GET['totalRows_dd2'])) {
  $totalRows_dd2 = $_GET['totalRows_dd2'];
} else {
  $all_dd2 = mysql_query($query_dd2);
  $totalRows_dd2 = mysql_num_rows($all_dd2);
}
$totalPages_dd2 = ceil($totalRows_dd2/$maxRows_dd2)-1;

mysql_select_db($database_rent, $rent);
$query_dd3 = "SELECT * FROM rent";
$query_limit_dd3 = sprintf("%s LIMIT %d, %d", $query_dd3, $startRow_dd3, $maxRows_dd3);
$dd3 = mysql_query($query_limit_dd3, $rent) or die(mysql_error());
$row_dd3 = mysql_fetch_assoc($dd3);

if (isset($_GET['totalRows_dd3'])) {
  $totalRows_dd3 = $_GET['totalRows_dd3'];
} else {
  $all_dd3 = mysql_query($query_dd3);
  $totalRows_dd3 = mysql_num_rows($all_dd3);
}
$totalPages_dd3 = ceil($totalRows_dd3/$maxRows_dd3)-1;

… all the way to dd100!!!

How would I use a for loop to speed typing up all this code for each block of code from dd1 to dd100?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:05:23+00:00Added an answer on May 27, 2026 at 11:05 pm

    This is a much more efficient code to do exactly what you do above:

    <?php
    
      // You only need to do these once as they are the same throughout
      mysql_select_db($database_rent, $rent);
      $maxRows = 10;
      // This code gets the total number of rows in the database
      $totalRowsAll = mysql_fetch_assoc(mysql_query("SELECT count(*) AS total FROM rent", $rent));
      $totalRowsAll = (int) $totalRowsAll['total'];
    
    ?>
    <div class="tab_container">
        <div id="tab1" class="tab_content">
          <table width="100%" border="0" cellspacing="5" cellpadding="5" id="1">
    <?php
    
      for ($i = 0; $i < 100; $i++) {
    
        // Calcluate value for this iteration and query database
        $pageNum = (isset($_GET['pageNum_dd'.($i + 1)])) ? (int) $_GET['pageNum_dd'.($i + 1)] : $i;
        $startRow = $pageNum * $maxRows;
        $query = "SELECT * FROM rent LIMIT $startRow, $maxRows";
        $result = mysql_query($query, $rent) or die(mysql_error($rent));
        $totalRows = (isset($_GET['totalRows_dd1'])) ? (int) $_GET['totalRows_dd1'] : $totalRowsAll;
        ${'totalPages_dd'.($i + 1)} = ceil($totalRows / $maxRows) - 1;
    
        // Now print this row
    ?>
            <tr height="100px" align="center">
    <?php
    
        while ($row = mysql_fetch_assoc($query)) {
    
    ?>
    
                <td style="background-color: <?php echo $row['colour']; ?>;" onclick="window.location='pay.php?id=<?php echo $row['dNo']; ?>&user=<?php echo $username; ?>'" onmouseover="this.style.cursor='pointer'"> 
                  <form action="pay.php?id=<?php echo $row['dNo']; ?>&user=<?php echo $username; ?>" method="post">
                    <input type="hidden" id="<?php echo $row['dNo']; ?>">
                    <input type="hidden" value="<?php echo $username; ?>">
                    <button type="submit" class="link" id="t<?php echo $row['dNo']; ?>">
                      <span><?php echo $row['dNo']; ?></span>
                    </button>
                  </form>
                </td>
    <?php
    
        } // End while
    
    ?>
            </tr>
    <?php
    
      } // End for
    
    ?>
          </table>
        </div>
    </div>
    

    …however:

    I’m fairly sure this could be summed up in a single query to get all the results you need, which would be much more efficient and drastically reduce database load. But because of the $_GET['pageNum_dd*'] and $_GET['totalPages_dd*'] variables which are used on a per row basis, I am not 100% sure about this, and I can’t work out how this would be done without knowing more about what is produced. You need to examine whether or not these parameters that can be passed are actually necessary/useful. As it is, they may be cause rows of a varying length, with an unequal number of cells per row – which is probably not what you want.

    The same also goes for the variables $totalPages_dd*, which are assigned below but never used anywhere. They may not be useful, and assigning them may be pointless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Everyone uses source-code control to manage versions (right?) and this provides some level of
everyone, I have this piece of the code: void foo(int var, int var1) {
everyone! I've been at this for a while, and I'm not sure how this
everyone! I couldn't find a tutorial explaining the right way to code this. I
everyone. I have problem replacing existing rows in table with new ones. I use
Everyone has this huge massively parallelized supercomputer on their desktop in the form of
Everyone is familiar with this functionality. If you open up the the outlook address
everyone. I know this is a far treated subject, but I've looked everywhere and
everyone. Here is a small VBA (Excel) function that i wrote, full of MsgBoxes
everyone..i want after i type 0203-ED in textfield ...two character behind that text can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.