0

I have the following code:

while($row = mysql_fetch_array($result)){
$output_items[] = $row["title"]; } // while

print(implode("\n", $output_items));

Which does what it says and splits the array with a new line for each item.

But how do I do the same and allow formatting with i.e. I basically want to say

foreach of the $output_items echo "<div class=whatever>$output_items</div> etc etc

Tearing my hair out with this!

Many thanks for all help

Darren

3 Answers 3

4
foreach ($output_items as $oi){
    echo "<div class=whatever>$oi</div>";
}

doesn't work? or i did not get what you are searching for

Sign up to request clarification or add additional context in comments.

Comments

1

Pretty simple, to make it easier to read I'd do something like this:

while($row = mysql_fetch_array($result))
{
    echo '<div class="whatever">';
    echo $row["title"];
    echo '</div>' . "\n";
} // while

Although you could still do this with your original code pretty easily:

while($row = mysql_fetch_array($result)){
$output_items[] = '<div class="whatever">' . $row["title"] . '</div>'; } // while

print(implode("\n", $output_items));

Comments

0

Rather than implode() them all with line breaks, use string interpolation to add them together:

$out_string = "";

// Loop over your array $output_items and wrap each in <div />
// while appending each to a single output string.
foreach ($output_items as $item) {
  $out_string .= "<div class='whatever'>$item</div>\n";
}

echo $out_string;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.