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

2
  • 1
    Define $i=0; before the first loop then (and leave the first for ( ; $i<= expression empty). You'll probably find out that this isn't what you actually want then. Investigate using foreach and while instead. Commented Oct 19, 2011 at 7:26
  • Yeah, it is a poor choice of loop Commented Oct 19, 2011 at 7:48

4 Answers 4

3

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>';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

-1

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

Comments

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

7 Comments

:D This my second day of PHP learning. The code works when the mysql_fetch_array is put in another position. Why is it bad combining SQL data and HTML?
LOl you are sure in the position to judge :) indeed PHP is your language. go on.
I'm judging by the "helpful" explanations you provide and the way you treat other people. Don't answer any of my future questions.
you have no experience to judge, silly :) And I'll answer whatever questions I find necessary to.
but you can't judge if someone is ignorant or not in the area you are learning for the very first day - can you? :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.