gpt4 book ai didi

php - 如何在 PHP 中将数组转换为不带 .= 的列表?

转载 作者:行者123 更新时间:2023-12-03 21:35:39 25 4
gpt4 key购买 nike

我有以下数组输出。

Array
(
[day] => 17
[eventContent] => event 1 of 17th
[eventTitle] => 17th event 1
)

Array
(
[day] => 19
[eventContent] => event 1 of 19th
[eventTitle] => 19th event 1
)

Array
(
[day] => 05
[eventContent] => event 1 of 5th
[eventTitle] => 5th event 1
)

Array
(
[day] => 17
[eventContent] => event 2 of 17th
[eventTitle] => 17th event 2
)

Array
(
[day] => 19
[eventContent] => event 2 of 19th
[eventTitle] => 19th event 2
)

Array
(
[day] => 19
[eventContent] => event 3 of 19th
[eventTitle] => 19th event 3
)
...
...

现在我想让它像下面这样。

例如19号

<li>
<span class="title">19th event 1</span>
<span class="desc">event 1 of 19th</span>
</li>
<li>
<span class="title">19th event 2</span>
<span class="desc">event 2 of 19th</span>
</li>
<li>
<span class="title">19th event 3</span>
<span class="desc">event 3 of 19th</span>
</li>

我尝试了以下方法。它在 PHP 中运行良好,但在 Codeigniter 中运行良好。所以我希望有人能告诉我另一种处理 out 的方法。=

提前致谢。

$events[intval($row_event->day)] .= '<li><span class="title">'
.stripslashes($row_event->eventTitle).
'</span><span class="desc">'.stripslashes($row_event->eventContent).
'</span></li>';

下面是模型的详细信息。

$events = array();

//query the database for events between the first date of the month and the last of date of month
// $result = mysql_query("SELECT DATE_FORMAT(eventDate,'%d') AS day,eventContent,eventTitle FROM eventcal WHERE eventDate BETWEEN '$current_year/$current_month/01' AND '$current_year/$current_month/$total_days_of_current_month'");

$query = $this->db->query("SELECT DATE_FORMAT(eventDate,'%d') AS day,eventContent,eventTitle FROM eventcal WHERE eventDate BETWEEN '$current_year/$current_month/01' AND '$current_year/$current_month/$total_days_of_current_month'");
foreach ($query->result_array() as $row_event)
{

$events[intval($row_event['day'])] .= '<li><span class="title">'
.stripslashes($row_event['eventTitle']).'</span><span class="desc">'
.stripslashes($row_event['eventContent']).'</span></li>';
...
...

--更新--

Controller

Code here

查看

Code here

错误

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 17

Filename: models/mcalendar_one.php

Line Number: 38

Array
(
[day] => 17
[eventContent] => event 1 of 17th
[eventTitle] => 17th event 1
)

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 19

Filename: models/mcalendar_one.php

Line Number: 38

Array
(
[day] => 19
[eventContent] => event 1 of 19th
[eventTitle] => 19th event 1
)
...
...

最佳答案

这可以简化很多。首先,看看 CI 的数据库类中的 Active Record,它会让你的 DB 调用变得 100% 容易。

为什么不能将数组传递回 View 并将其注入(inject)到 View 中?

你的 View 可能看起来像

<?php foreach ($row_event as $event) : ?>
<li>
<span class="title"><?php echo $event->eventTitle; ?></span>
<span class="desc"><?php echo $event->eventContent; ?></span>
</li>
<?php endforeach; ?>

这将遍历您的事件详细信息数组并生成您的列表。

关于php - 如何在 PHP 中将数组转换为不带 .= 的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com