使用for循环加速PHP代码输入

时间:2011-12-27 17:40:35

标签: php loops for-loop

大家好,我一直在从SQL表1中提取行1.我两年前编写了这段代码,并意识到它是多么无效。我想通过输入一个简单的for循环来自动化编码来加快速度。

$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到dd99介于两者之间!

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

对应于:

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;

......一直到dd100 !!!

我如何使用for循环来加速从dd1到dd100的每个代码块输入所有代码?

2 个答案:

答案 0 :(得分:3)

了解arraysfor()循环。

答案 1 :(得分:1)

这是一个更有效的代码,可以完全按照上面的操作执行:

<?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>

... <强>然而

我很确定这可以在一个查询中总结,以获得您需要的所有结果,这将更有效并大幅减少数据库负载。但是由于每行使用的$_GET['pageNum_dd*']$_GET['totalPages_dd*']变量,我对这一点并不是100%肯定,如果不了解更多信息,我无法理解如何做到这一点。生产什么。您需要检查这些可以传递的参数是否真正必要/有用。实际上,它们可能会导致行长不同,每行的单元格数不等 - 这可能不是您想要的。

同样适用于变量$totalPages_dd*,它们在下面分配但从未在任何地方使用过。它们可能没用,分配它们可能毫无意义。