PHP MySQL 数据 FOR 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7817821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:25:11  来源:igfitidea点击:

PHP MySQL data FOR loop

phpmysqlfor-loop

提问by abruski

<?php

//function to create a table
function makeTable($table, $columns){
    $numFields = count($columns)-1;

    $query = 'SELECT * FROM '.$table;
    $result = mysql_query($query);
    $arrayResult = mysql_fetch_array($result);
    $num_rows = mysql_num_rows($result);

    for ($x = 1; $x <= $num_rows; $x++){ //1st for loop
        echo '<tr>';
            for ($i = 0; $i <= $numFields; $i++){ //2nd for loop
                echo '<td>'.$arrayResult[$columns[$i]].'</td>';
            }
        echo '</tr>';
    }
}

?>

?>

$columns is an array entered by the user eg: $columns = array ('Column1', 'Column2', 'Column3);. These are the names of the columns which are in a given $table.

$columns 是用户输入的数组,例如:$columns = array ('Column1', 'Column2', 'Column3);。这些是给定 $table 中列的名称。

My idea was to create a function that displays the data from the MySQL table with the info from the $columns array. The problem is in the second for loop. The value of $i is reset every time the first loop is done, so I get the same result over and over again (the number of rows in the table). My question is this: How do I keep the $i in the second loop from resetting?

我的想法是创建一个函数,用 $columns 数组中的信息显示 MySQL 表中的数据。问题出在第二个 for 循环中。每次完成第一个循环时都会重置 $i 的值,因此我一遍又一遍地得到相同的结果(表中的行数)。我的问题是:如何防止第二个循环中的 $i 重置?

Thank you in advance.

先感谢您。

回答by xdazz

The reason you get the same result over and over is not because $i, but $arrayResult.

您一遍又一遍地得到相同结果的原因不是因为$i,而是$arrayResult

The right way is like this:

正确的做法是这样的:

//function to create a table
function makeTable($table, $columns){
    $numFields = count($columns)-1;
    $query = 'SELECT * FROM '.$table;
    $result = mysql_query($query);

    while ($arrayResult = mysql_fetch_array($result)){
        echo '<tr>';
            for ($i = 0; $i <= $numFields; $i++){ //2nd for loop
                echo '<td>'.$arrayResult[$columns[$i]].'</td>';
            }
        echo '</tr>';
    }
}

回答by Nedret Recep

In your code you simple fetch always the first row and regardless of the subsequent cycles you only deal with that first row.

在您的代码中,您总是简单地获取第一行,无论后续周期如何,您都只处理第一行。

Just place

就地

 $arrayResult = mysql_fetch_array($result);

within the first loop just before echo '<tr>';

在 echo 之前的第一个循环中'<tr>'

Anyway, for is not the best choice for iterating the records of a table, consider using while.

无论如何,for 不是迭代表记录的最佳选择,请考虑使用 while。

回答by Rajan Rawal

Why Dont you use while loop? If you use while you even don't need mysql_num_rows($result). Try this

为什么不使用while循环?如果你使用而你甚至不需要mysql_num_rows($result). 尝试这个

function makeTable($table, $columns){
    $numFields = count($columns)-1;

    $query = 'SELECT * FROM '.$table;
    $result = mysql_query($query);

    while($arrayResult = mysql_fetch_array($result)){
        echo '<tr>';
            for ($i = 0; $i <= $numFields; $i++){ //2nd for loop
                echo '<td>'.$arrayResult[$columns[$i]].'</td>';
            }
        echo '</tr>';
    }
}

I m sure, you will get your ans

我敢肯定,你会得到答案

回答by Your Common Sense

  1. Your code won't work ever. Because you didn't read manual entry for mysql_fetch_array()

  2. There is no use for the for loops these days. You need some manual to see how to deal with loops in PHP. foreach and while you will need more often than for.

  3. The idea of creating such a function is wrong. combining SQL and HTML in one function is a sign of VERY BAD design. What you really need is a function to get SQL data into array and a template.

  1. 你的代码永远不会工作。因为你没有阅读 mysql_fetch_array() 的手动输入

  2. 现在 for 循环没有用了。您需要一些手册来了解如何处理 PHP 中的循环。foreach 和 while 需要比 for 更频繁。

  3. 创建这样一个函数的想法是错误的。在一个函数中结合 SQL 和 HTML 是非常糟糕的设计的标志。您真正需要的是将 SQL 数据放入数组和模板的函数。

a function

一个函数

function sqlArr($sql){
  $ret = array();
  $res = mysql_query($sql) or trigger_error(mysql_error()." ".$sql);
  if ($res) {
    while($row = mysql_fetch_array($res)){
      $ret[] = $row;
    }
  }
  return $ret;
}

a code

一个代码

$data = sqlArr("SELECT * FROM table");
include 'template.php';

a template

一个模板

<table border='1'>
<? foreach ($data as $row): ?>
  <tr>
  <? foreach ($row as $col): ?>
    <td><?=$col?></td>
  <? endforeach ?>
  </tr>
<? endforeach ?>
</table>

However, you can put this latter template code into function, if you're gonna use it often.
and call it like

但是,如果您要经常使用它,您可以将后面的模板代码放入函数中。
并称之为

<? drawTable($data) ?>