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

By : shin
Source: Stackoverflow.com
Question!

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

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
)
...
...
By : shin


Answers

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.



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'])] = '
By : Anurag


This video can help you solving your question :)
By: admin