作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下数组输出。
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
查看
错误
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/
我是一名优秀的程序员,十分优秀!