Foreach within While

Asked

Viewed 959 times

1

Captures in an array the values received via checkbox marked post, these checkboxes have the values equal to the names of the table fields, need to make a query with while and print the fields referring to array:

<?php
    $campos = '`'.implode('`, `', $_POST['check']).'`';
    $sql = mysql_query("SELECT id,$campos FROM chamado where status = 1");
    while($busca=mysql_fetch_array($sql)){
?>

    <tr class="odd gradeX">
        <td width="2%"><?php echo $busca['id'];?></td>
        <?php
            foreach ($quantidade_item as $tags) {
                echo '<td width="2%">'.$busca[$tags].'</td>';
            }
        ?>
    </tr>

<?php
    }
?>

What happens is that when executing the code, if it is verified that only a checkbox has been marked, the listing appears normally, but, if you check more than one checkbox, the listing displays only the data of the first column, the other columns return me Undefined index -

  • 2

    I didn’t quite understand the problem. You can explain it better/in other words?

  • 1

    Yes, you failed to report the problem. What happens is that when executing the code, if it is verified that only a checkbox has been checked, the listing appears normally, but if you check more than one checkbox, the listing displays only the data from the first column, the other columns return me Undefined index

  • Okay, and what gives var_dump($_POST['check']); if you put it in the first line of that code that you show`

  • Sorry for the delay, I had problems with my hosting, well, when executing the code and inform the line you said returns: array(3) { [0]=> string(8) "cpf_cnpj" [1]=> string(13) "data_cadastro" [2]=> string(10) "id_cliente" } , and in the example I marked 3 check

  • and if you check only one checkbox as the var_dump? puts your HTML too to understand better. You don’t need``` in $fields.

  • Just a check array(1) { [0]=> string(8) "cpf_cnpj" }, when checking the query works normally, when passing more checks of the undefined variables and returns in the table only the values of a column

  • I don’t quite understand your problem but I have a tip for you, don’t use mysql_query because it is obsolete, I advise you to use PDO or Mysqli.

Show 2 more comments

1 answer

0


If I understand what you want to change this line:

foreach ($quantidade_item as $tags) {

I don’t see in your code where that variable comes from $quantidade_item and semantically it doesn’t make much sense to me because I think what you want is to iterate the fields that come from $_POST with the data you got from the comic book, right?

In that case I suggest you change the code to:

foreach ($_POST as $tags) {

but put the $_POST so it is not good practice (I put it just to clarify the example). Another thing you can change is to take ``` in the query fields as it does not need.

Final code:

<?php
    $_campos = $_POST['check'];
    $campos = implode(', ', $_campos);
    $sql = mysql_query("SELECT id,$campos FROM chamado where status = 1");
    while($busca=mysql_fetch_array($sql)){
?>
    <tr class="odd gradeX">
        <td width="2%"><?php echo $busca['id'];?></td>
        <?php
            foreach ($_campos as $tags) {
                echo '<td width="2%">'.$busca[$tags].'</td>';
            }
        ?>
    </tr>

<?php
    }
?>
  • 1

    Very good ségio, we managed to solve the problem, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.