开发者

How to convert array to list without .= in PHP?

开发者 https://www.devze.com 2022-12-16 20:49 出处:网络
I have the following array outputs. Array ( [day] => 17 [eventContent] => event 1 of 17th [eventTitle] => 17th event 1

I have the following array outputs.

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
)
...
...

Now I want to make it like the followings.

For example for 19th

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

I tried the following. It works fine in PHP but not in Codeigniter. So I am hoping someone can tell me another way to do with out .=

Thanks in advance.

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

And the following is the details of the model.

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

--UPDATE--

Controller

Code here

View

Code here

Errors

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

Seve开发者_开发问答rity: 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
)
...
...


This can simplified quite a bit. First, look at Active Record in CI's database class, it will make your DB calls 100% easier.

Why can't you pass the array back to the view and inject it there?

Your view may look like

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

That will loop through your array of event details and make your list.


The array offset does not exist at this point. So the very first time you assign, do not append by using a .. After this first assignment, the index exists, and using a . is OK.

$events[intval($row_event['day'])] = '<li><span class="title">'

Notice = instead of .=

So to give an example:

$myArray = array();
echo $myArray[0]; // Undefined offset

is wrong as the 0th index does not exist.

Similarly, $myArray[0] .= "something"; is wrong because if we expand the .= out, it becomes:

$myArray[0] = $myArray[0] . "something"; // see the problem?

That should take care of the Notice you're seeing, but you should use Scott's solution instead of printing out HTML code inside a PHP string as thats just a very bad practice, that we all realize sooner or later.


Pleaser NEVER create html in your model, thats an abuse of the MVC pattern. And dont concat strings to do that. Write yourself a view, in plain html which (in some places) echos values using php, just as Scott described.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号