Welcome Guest, Not a member yet? Register   Sign In
Set Post active by current date
#1

[eluser]Zimooon[/eluser]
Hey guys,

I want to set my calender entries offline when the current date/time > the datetime i set for the end of it. At the moment my entries can be manually set activ or not.
Here is my current code:

Model:
Code:
function getactiveCalender($active = FALSE) {
    if($active = TRUE)
    {
     $this->db->where('active',1);
    }
    $query = $this->db->get('calender');
    $calender = array();

    foreach ($query->result() as $row) {
        $posts[] = array(
            'id' => $row->id,
            'ctitle' => $row->ctitle,
            'ccontent' => $row->ccontent,
            'start' => $row->start,
            'end' => $row->end,
            'active' => $row->active
                    
        );
    }

    return $calender;

}
Something like
$now = time();
if $now > $end {
etc?

Best option would be to set active == 0 by now>end else active == 1 or stuff like that.


Thanks for help

Best regards
#2

[eluser]toopay[/eluser]
I'm not really sure i'm understand with what your mention above. If, you are about sorting your record, i don't see you have date field. Also, i don't understand why you didn't returning the query result, and send back an empty array like that.
#3

[eluser]Zimooon[/eluser]
Ok this was a bit confusing.

The above code hasn't a datefield yet, it is based on my news which get set activ/inactiv by edit and not by date/time as i want in the calender.
The start and end are inputed by hand . Now i want that the current date, now(), implemented . And if now() is bigger than $end , $active should be set to 0 , so that the calenderpost isnt shown.
#4

[eluser]TWP Marketing[/eluser]
Ximooon, Your first if clause will always evaluate to TRUE and always execute the db->where() action. Use "==" to do a comparision, not "=", which is an assignment.

I have added an if structure inside your foreach loop and changed the code to return the calendar array, which I think is what you want, but I'm not sure. The code will return your entire calendar file if $active is passed as FALSE, which is the default action. There are better ways to sort the file.

Model:
Code:
function getactiveCalender($active = FALSE) {
    if($active = TRUE) //<-- THIS WILL NOT WORK. It should be: if($active == TRUE)
    {
     $this->db->where('active',1);
    }
    $query = $this->db->get('calender');
    $calender = array();

    foreach ($query->result() as $row) {
//--&gt;
        if($row->end < now()){ // if end time has already passed
            $row->active = FALSE; // set active to FALSE
        } // end if row->end < now
//--&gt;
        $calendar[] = array(
            'id' => $row->id,
            'ctitle' => $row->ctitle,
            'ccontent' => $row->ccontent,
            'start' => $row->start,
            'end' => $row->end,
            'active' => $row->active
        );

    }

    return $calender;

}
#5

[eluser]Zimooon[/eluser]
allright thank you .
Now the major problem : it still shows all entries wheter active is 1 or 0 ..
#6

[eluser]Zimooon[/eluser]
When the entries are set to active==1 they should be posted. active == 1 should be set to active == 0 when now()>end . When active == 0 the entry should not be posted.
#7

[eluser]Zimooon[/eluser]
hmm can't get it done Sad
#8

[eluser]toopay[/eluser]
Like i state at my first response, i don’t see you have date field! Unless you show your table structure (including the field type) i can't help you. I can't, like TWP Marketing did, assume that 'end' field is date type field and so on.
#9

[eluser]Zimooon[/eluser]
so hey again, thank your for your patience.

Code:
end     timestamp         on update CURRENT_TIMESTAMP     NO     CURRENT_TIMESTAMP     ON UPDATE CURRENT_TIMESTAMP

Code:
active     tinyint(1)         UNSIGNED     YES     NULL

$end is my datefield. For now it works that i can show which are active and which are not in my view:
Code:
<table>
                        <tr>
                          <th>ID Titel Von Bis Kalendereintrag</th>
                          <th class="thAktion">Aktion</th>
                        </tr>&lt;?php foreach($calender as $calender_row): ?&gt;  
                         <tr class="&lt;?php echo alternator('even', 'odd');?&gt;">
                            <td>&lt;?php echo $calender_row['id'];
                                      echo $calender_row['ctitle'];
                                      echo $calender_row['start'];  
                                      echo $calender_row['end'];
                                      echo $calender_row['ccontent'];
                                      echo unix_to_human(time(), TRUE, 'eu') ;
                                 ?&gt;
                            
                            </td>
                        
                            <td>
                              &lt;?php if($calender_row['active'] != 1 ):?&gt;     <img src="/_/img/rotes_x.gif" alt="Offline" title="Offline"> &lt;?php endif;?&gt;    
                              &lt;?php if($calender_row['active'] == 1 ):?&gt;     <img src="/_/img/gruener_hacken.jpg" alt="Online" title="Online">    &lt;?php endif; ?&gt;
                              <a href="/calender/updateCalender/&lt;?php echo $calender_row['id']; ?&gt;" class="editLink" title="Bearbeiten">Bearbeiten</a>
                              <a href="/calender/deleteCalender/&lt;?php echo $calender_row['id']; ?&gt;">Löschen</a>
                            
                          
                            </td>
                          </tr>
                           &lt;?php endforeach ?&gt;
                      </table>

This is my view. With this i get right images ( rotes_x.gif for active != 1 and the jpg for active ==1) . Everything with this model:

Code:
function getactiveCalender($active = TRUE) {
      
      
    if($active = FALSE)
    {
     $this->db->where('active',0);
    }
  
    $query = $this->db->get('calender');
    $calender = array();

    foreach ($query->result() as $row) {
//--&gt;
        if($row->end < unix_to_human(time(), TRUE, 'eu')){
            $row->active = FALSE;  
        } // end if row->end < now
//--&gt;
        $calender[] = array(
            'id' => $row->id,
            'ctitle' => $row->ctitle,
            'ccontent' => $row->ccontent,
            'start' => $row->start,
            'end' => $row->end,
            'active' => $row->active
        );
    
          
    }
          
    return $calender;      

}

But in my database all actives are set to one . It shows me the right posts with the red cross ( for active != 1) but i don't want them to be shown and the active set to 0 when
Code:
if($row->end < unix_to_human(time(), TRUE, 'eu')){
            $row->active = FALSE;  
        } // end if row->end < now

the current time is bigger then end time.
As said above the Active get set right in the view but not written to 0 . Also the posts are still shown. inactive but shown.

best regards
#10

[eluser]TWP Marketing[/eluser]
[EDIT, twp]
Zimooon,
I think your field type (timestamp) is NOT what you want for the start and end date fields. That fieldtype causes the field to be set automatically and you want the dates to be set by either you or your user. I use type Integer with a length of 20 for date fields. I'm not suggesting that you change your type, because I don't know exactly what you are doing with the field, but type timestamp may be a problem here.
[EDIT, twp]
Timestamp is ok, it will work, within the limits imposed by the type (between 1970 UTC and '2038-01-19 03:14:07' UTC).

FYI, I do really dislike using dates. They are a subject of problems because of the wide range of possible formats. My error above, and, with care your usage should work.




Theme © iAndrew 2016 - Forum software by © MyBB