Welcome Guest, Not a member yet? Register   Sign In
error by getting events from database.
#1

[eluser]shinokada[/eluser]
The following code give errors in CI. If I use it in normal php, it does not give any.

Could anyone point out what I need to correct?

Thanks in advance.

Code:
$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() as $row_event)
        {
   //loading the $events array with evenTitle and eventContent inside the <span> and <li>. We will add then inside <ul> in the calender
$events[intval($row_event->day)] .= '<li><span class="title">'.stripslashes($row_event->eventTitle).'</span><span class="desc">'.stripslashes($row_event->eventContent).'</span></li>';


Quote:Severity: Notice

Message: Undefined offset: 17

Filename: models/mcalendar_one.php

Line Number: 32
...

Line Number 32 is this part.

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

Whole code of model is here.

Code:
function getEvents(){
    
    //check if time is set in the URL
    if(isset($_GET['time']))
        $time = $_GET['time'];
    else
        $time = time();
    
    
    $today = date("Y/n/j", time());
    $current_month = date("n", $time);
    $current_year = date("Y", $time);
    $current_month_text = date("F Y", $time);
    $total_days_of_current_month = date("t", $time);
    $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() as $row_event)
        {
   //loading the $events array with evenTitle and eventContent inside the <span> and <li>. We will add then inside <ul> in the calender
        $events[intval($row_event->day)] .= '<li><span class="title">'.stripslashes($row_event->eventTitle).'</span><span class="desc">'.stripslashes($row_event->eventContent).'</span></li>';
        }
    print_r ($events);
     return $events;
    
    }

Original code is from here.http://iambari.com/2009/05/05/create-an-...nd-jquery/
#2

[eluser]JHackamack[/eluser]
I believe this is because you're telling PHP to add to an existing value of $events[intval($row_event->day)], but if you're calling a day that didn't have any events yet you're trying to add to something that doesn't exist.

If it works the way you want you can turn of error reporting so the notice doesn't appear, but thats not very PHP like. The best way would be to use the following code:

$events[intval($row_event->day)][] =

Then in your view you use foreach $events[$day] as $event

rather than just echoing a string.




Theme © iAndrew 2016 - Forum software by © MyBB