Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show array in a td inside the while

I have the following code:

$result7 = mysqli_query($conn, $sql7);
$resalm = mysqli_query($conn, $contalm);

$p = array();
while($row_alm = mysqli_fetch_array($resalm)) {
  $p[] = $row_alm['nome'];
}

while($rows_cursos7 = mysqli_fetch_array($result7)) {
  $tabela8 .= '<td><button type="button" class="btn btn-info btn-xs" data-toggle="tooltip" data-placement="top" title='.$p.'>'.$rows_cursos7['Almoço'].'</button></td>';
}

The problem is that if you do it inside the while print_r($p) it returns the data coming from the database.

But inside the button title='.$p.' it only returns array and not the data that comes from the database. Can you help?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to show all the items from $p inside the second loop, you’ll also need to loop that array, or at least implode it (depending on the exact nature of the output you want to see).

For example:

Using implode

while($rows_cursos7 = mysqli_fetch_array($result7)) {
  $tabela8 .= '<td><button type="button" class="btn btn-info btn-xs" data-toggle="tooltip" data-placement="top" title="'.implode(", ", $p).'">'.$rows_cursos7['Almoço'].'</button></td>';
}

Using a loop:

while($rows_cursos7 = mysqli_fetch_array($result7)) {
  $tabela8 .= '<td><button type="button" class="btn btn-info btn-xs" data-toggle="tooltip" data-placement="top" title="';

  foreach ($p as $i)
  {
    $tabela8 .= $i." - ";
  }

  $tabela8 .= '">'.$rows_cursos7['Almoço'].'</button></td>';
}

N.B. It’s unclear if this is the output you actually want, as that wasn’t clear from the question, but it shows you a couple of ways you can access the individual array elements, which you can then adapt to get the output you are really trying to achieve.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading