0

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?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Anonimo
  • 55
  • 6

1 Answers1

2

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.

ADyson
  • 57,178
  • 14
  • 51
  • 63
  • One more thing, in the implode when separated by a comma is it possible to make a paragraph? – Anonimo Jul 28 '23 at 16:10
  • You can put anything you want as the separator – ADyson Jul 28 '23 at 16:10
  • To make the paragraph I put it like this, `"'.implode(",/n ", $p).'"`, but it didn't work – Anonimo Jul 28 '23 at 16:12
  • Well a newline in plain-text is `\n` not `/n`, for a start. And also you're doing it in a HTML attribute whose purpose is not known to me. I've no idea whether the context in which you end up seeing that data will recognise plain-text newline characters or not. If it ends up being displayed in a HTML context, `
    ` is the HTML newline tag (and that's assuming the intervening code doesn't strip or encode HTML tags in the source data).
    – ADyson Jul 28 '23 at 16:14
  • It doesn't work, I already tried: `"'.implode(",\n ", $p).'"`, `"'.implode(",
    , $p). '"`, `"'.implode(", ", $p).'"`, `"'.implode(",

    ", $p).'"`. None resulted
    – Anonimo Jul 28 '23 at 16:29
  • Are you using Bootstrap's tooltips? If so then, as I suspected, it doesn't allow HTML content by default. Did you check the documentation? Look at https://getbootstrap.com/docs/5.0/components/tooltips/#options and find the `html` option. See also https://stackoverflow.com/questions/13704789/can-i-use-complex-html-with-twitter-bootstraps-tooltip for more info – ADyson Jul 28 '23 at 17:51