Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a function in my database class, which gives me a list of events a certain user is attending

function getEventList($uid)
{
    $date = date('Y-m-d H:i:s');
    $eventSQL = "SELECT em.eventID, e.eventName, e.eventTime FROM eventmembers em INNER JOIN events e ON em.eventID = e.eventID WHERE userIDInvited = :uid";
    $eventPrepare = $this->prepare($eventSQL);
    $eventPrepare->bindParam(':uid', $uid);
    $eventPrepare->execute();
    return ($eventPrepare->rowCount() > 0)
        ? $eventPrepare->fetchAll() : array();
}

To access these results I'm simply doing this

//php

$eventList = $db->getEventList($userid);
if($eventList == NULL){
$smarty->assign('events', 'No events'); 
}
else{
$count = sizeof($eventList);
for($i = 0; $i < $count; $i++)
{
    $smarty->assign('events', $eventList);
}   
}

html

<p>MY EVENTS</p>
    {foreach from=$events item=result}
        <a href="https://localhost/FinalYear/events/{$result.eventID}.php">{$result.eventName}</a><br>
        {$result.eventTime}<br>({$result.timeRemain} Days remaining)
        <br>
        <br>
{/foreach}

What I want to do now is to add, that there is also saying how many days are remaining till this event occurs.

What I did was simply adding this in the for-loop.

$startTimeStamp = strtotime($date);
   $endTimeStapm = strtotime($eventList[$i]['eventTime']);
   $timeDiff = abs($endTimeStapm - $startTimeStamp);
   $numberDays = $timeDiff/86400;
   $numberDays = intval($numberDays);

The problem now was, then when, the user attends more than one event, the $numberDays will be always overwritten. So I tried to add $numberDays to the end of the current array index. But this gives me just an endless-loop. Without $numberDays var_dump gives me this output, when I have to entries

array(2) { [0]=> array(3) { ["eventID"]=> string(1) "1" ["eventName"]=> string(7) "Konzert" ["eventTime"]=> string(19) "2013-03-28 00:00:00" } [1]=> array(3) { ["eventID"]=> string(1) "2" ["eventName"]=> string(4) "Oper" ["eventTime"]=> string(19) "2013-03-30 00:00:00" } } array(2) { [0]=> array(3) { ["eventID"]=> string(1) "1" ["eventName"]=> string(7) "Konzert" ["eventTime"]=> string(19) "2013-03-28 00:00:00" } [1]=> array(3) { ["eventID"]=> string(1) "2" ["eventName"]=> string(4) "Oper" ["eventTime"]=> string(19) "2013-03-30 00:00:00" } } 

But I also want at the end of every entry the days remaining. The problem is, that I can't do it in another loop, because that would mean that I would need to make another loop in the .html-file inside the other loop. So it would be run through before the other loop has jumped to its next entry.

Any suggestions how to solve this?

My database-table looks like this

table events

eventID, eventName, eventDescription, eventLocaion, eventTime, eventHost, privacy Settings

table eventmembers

eventID, userIDInvited, InvitationStatus
share|improve this question

closed as too localized by Stephan, mattytommo, mdm, ecatmur, Frank Shearar Mar 27 '13 at 9:39

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
You are assigning in a loop $smarty->assign('events', $eventList); each time it will override the previous one and value in events will be always the last one –  Prasanth Bendra Mar 26 '13 at 11:55
    
Hi @PrasanthBendra - it doesn't make any difference if I write it in the for-loop or out of the for-loop. As soon as I add my time calculation it always results in an endless-loop. –  loomie Mar 26 '13 at 12:06

2 Answers 2

up vote 2 down vote accepted

You can walk through array with FOR not FOREACH loop.

$data = array(0 => array('name' => 'John', 'age' => '12'), 1 => array('name' => 'Megan', 'age' => '18'));
for ($i = 0; $i < sizeof($data); $i++) {
// get your needed timestamp, for example
$time = time();
// add this to each sub-array end
$data[$i]['time'] = $time;
}
share|improve this answer
    
Thanks! Seems like I messed it up with my $endTimeStapm = strtotime($eventList[$i]['eventTime']); I thought it's not possible because this didn't work, thanks! –  loomie Mar 26 '13 at 12:54

A number of ways you could solve this:

Calculate the number of days remaining in the foreach loop, and print it out immediately; or add a separate array with number of days remaining. You could even create a class with (array) fields for each aspect of the event. As you already observed, right now you are overwriting the value...

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.