简体   繁体   中英

PHP MySQL data FOR loop

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

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?

Thank you in advance.

The reason you get the same result over and over is not because $i , but $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>';
    }
}

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>' ;

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

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

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

  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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM