PHP解析数组添加css

发布于 2024-12-11 02:19:59 字数 408 浏览 0 评论 0原文

我有以下代码:

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

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

它执行它所说的操作,并为每个项目用一个新行分割数组。

但我该如何做同样的事情并允许使用 ie 进行格式化我基本上想说的是

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

用这个撕掉我的头发!

非常感

谢达伦的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薄情伤 2024-12-18 02:19:59
foreach ($output_items as $oi){
    echo "<div class=whatever>$oi</div>";
}

不起作用?或者我没有得到你正在寻找的东西

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

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

谈情不如逗狗 2024-12-18 02:19:59

非常简单,为了更容易阅读,我会这样做:

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

尽管您仍然可以很容易地使用原始代码执行此操作:

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

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

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));
深府石板幽径 2024-12-18 02:19:59

使用字符串插值将它们添加在一起,而不是用 implode() 将它们全部换行:

$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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文