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

[eluser]TWP Marketing[/eluser]
Zimoon, your if statement will ALWAYS execute the where() statement. Change to a comparision operator (==) inside the if clause, see below...

Code:
function getactiveCalender($active = TRUE) {
      
    if($active = FALSE) // <-- THIS IS STILL A PROBLEM change it to: 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;      

}

Next:
Using the CI function unix_to_human() "Takes a Unix timestamp as input and returns it in a human readable format with this prototype: YYYY-MM-DD HH:MM:SS AM/PM".

However, you are comparing that output to the timestamp field type which has format "YYYY-MM-DD" or "YYYY-MM-DD HH:MM:SS". The comparison will very likely fail because the two strings are not exactly the same format.

I prefer (your choice, not saying you must do it this way) to use integers for date comparison and only convert to string types for screen display. I store my dates as integer values in the db and convert any user input dates to integer before writing back to a database. Your mileage my vary and there are many other ways to handle this.
#12

[eluser]Zimooon[/eluser]
Allright good idea with the int. But how can i change the current date/time ( f.e. now() or time() ) to int to compare it?
I changed end now to int(20). But how to get YYYY.MM.DD hh:mmConfuseds to an int? when i submit my entry i get f.e. intvalue 604 for a full dateSad
and the code to the one you posted above (active == false) but now it display all as INACTIVE.
#13

[eluser]Zimooon[/eluser]
Hey to get you on my current stand :
My end is atm int(20).
my modelcode is
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 < now){ // Wenn das Enddatum schon vorbei ist
            $row->active = FALSE;   // aktiv auf FALSE setzen
        } // 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;      

}

This is my create_calender controller:
Code:
function create_calender()
  {
      // Check if form is submitted
    if (!$this->ion_auth->logged_in())
    {
            //redirect them to the login page
            redirect('auth/login', 'refresh');
        }

        else
        {
    
        if ($this->input->post('submit')) {
            
            $ctitle = $this->security->xss_clean($this->input->post('ctitle'));
            $ccontent = $this->security->xss_clean($this->input->post('ccontent'));
            $start = $this->security->xss_clean($this->input->post('start'));
            $end = $this->security->xss_clean($this->input->post('end'));
            $active = $this->security->xss_clean($this->input->post('active'));
    
            
            // Add the post
            $this->Calender_model->addCalender($ctitle,$ccontent,$start,$end,$active);
        }

        $data = array();


        $data['calender'] = $this->Calender_model->getCalender();
        $this->load->view('auth/create_calender', $data);
        $this->data['title'] = "Kalender erstellen";
    
    }
  }

Maybe it is a problem to set the entries active/inactive manuall at start?

ATM all my entries are displayed. But all are set active = 1 in my database so it seems to be normal.

How would you set the end? int(20) is ok?
Whats wrong with the model code that it don't write active to 0 when the time expired?
best regards
#14

[eluser]Zimooon[/eluser]
ah and here is my view:

Code:
&lt;?php if(validation_errors()) { ?&gt;
                      <div class="validation_errors">&lt;?php echo validation_errors(); ?&gt;</div>
                    &lt;?php } ?&gt;
                    <br />
                    
                    &lt;!-- News Liste anzeigen --&gt;
                    
                      <a href="/calender/create_calender/">Neuen Kalendereintrag erstellen</a>
                      <br /><br />
                
                      <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>

If you can't help me , maybe could you show me how you would do it?
(Admin/user write : Start End Content and title. The entry is shown until the current datetime is bigger than end(Date + Time) . F.e. 28.04.2011 15:50 is $end and the current time is 28.04.2011 15:43. At 15:50 the entry goes inactiv.(maybe with active == 0 but don't have to be)
How to setup my database for this(espacially for end) and how to format the date for output (dd.mm.YY hh:mm) .
Please help me Smile

best regards
#15

[eluser]Zimooon[/eluser]
Allright guys, to keep you updated ( nobody reads this post Big Grin) :
i think the problem is my dateformat. now() gives me a string back f.e. NOW: 1304003142 and my input with my DB (timestamp) gives me something back like 2020-04-03 11:11:11 . They can't be compared Sad
Now i changed as some helpfull person mentioned above to int(20) again , but here is the problem : it just saves the 2020 and stops. No Unix timecode as now() . I tried to change it with strtotime() and human_to_unix but without success Sad either it just saved 0 or 0000-00-00 etc or just the normal date .
My new code for the model :
Code:
function getactivePosts($active = FALSE) {
    if($active == TRUE)
    {
      $date = $this->db->where('date');
      $nowDate = now();  

      
     $this->db->where('active','1');
     $this->db->where('date >', $nowDate);                                        
    }
    $query = $this->db->get('posts');
    $posts = array();

    foreach ($query->result() as $row) {
        $posts[] = array(
            'id' => $row->id,
            'title' => $row->title,
            'content' => $row->content,
            'date' => $row->date,
            'active' => $row->active
                    
        );

    }

Well at least i try Big Grin
#16

[eluser]Zimooon[/eluser]
i changed now the $nowDate = now(); to $nowDate = unix_to_human(now()); with date as integer(Year saved only by stupid integer Big Grin) and tada it just show me the posts who are marked with active == 1 and which are under the current Year(2011). So now gogo change the freakin format of date to string or whatever its called(12312344 stuff like that) to compare it better and get the write active == 0 when date<now!
Feels like my diary of fail right here Big Grin

best regards
#17

[eluser]TWP Marketing[/eluser]
Zimooon, you've made a lot of changes and I'm not sure that I've understood all of them.

I'm going to reference the PHP manual here: http://www.php.net/manual/en/ I keep a copy of this open in my browser.

First, to display an integer time (defined as the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970)
use date('d-m-Y H:iConfused',$int_time). This will display as dd-mm-yyyy hh:mmConfuseds
You can adjust the output format as you wish. Read the PHP date format descriptions in the php manual here: http://www.php.net/manual/en/function.date.php

$int_time = time(); // this returns the integer current time (Unix time in seconds since midnight, January 1, 1970) ie; 1234567890

You can get some history of unix time here: http://en.wikipedia.org/wiki/Unix_time

Second, to convert string time "dd-mm-yyyy hh:mmConfuseds AM/PM" to integer, you could use this format:
Code:
...
$int_time = strtotime("10 September 2000"); // <-- This string date may have many different formats
...
now you would save $int_time to your db in an integer type field

Read the php manual on strtotime() here: http://us3.php.net/manual/en/function.strtotime.php


NEXT:
your Controller calls this function:
Code:
...
$data['calender'] = $this->Calender_model->getCalender();
...
BUT, your model function is named getactiveCalender().
So I'm not sure which function you intend to call in the controller.
but I think your controller should have these lines:
Code:
...
$active = TRUE; // set the flag to get only active calendar records
$data['calender'] = $this->Calender_model->getactiveCalender($active);
...

As I read your model code, getactiveCalendar() will return ALL calendar records by default.
If you pass $active = FALSE from the controller, or you don't pass anything, it will return ONLY IN-active records.
IS THIS WHAT YOU WANT TO HAPPEN?
#18

[eluser]Zimooon[/eluser]
Hey , yeah i did a lot and most of all tried a lot. I use atm UNIX Timestamp as INT(20) and it works well. My posts get shown when they are flagged as active(by user manually , to have a controll over the shown articles) and when the by the user posted endtime is bigger then the time now. It really works fine now .
Your change vom timestamp to dateformat works well but if possible i want something that changes everything in my view which has a timestamp format(134000 etc you know waht i mean) to a Format like this: dd.mm.YY hh:mm.
But i don't think this is any codeigniter question anymore . I want it to implement it into my header so every view will get this. I think about js or stuff maybe someone got something?
Thank you for your help guys espacially TWP Marketing.

have a nice weekend.
#19

[eluser]TWP Marketing[/eluser]
[quote author="Zimooon" date="1304082998"]
...
Your change vom timestamp to dateformat works well but if possible i want something that changes everything in my view which has a timestamp format(134000 etc you know waht i mean) to a Format like this: dd.mm.YY hh:mm.
But i don't think this is any codeigniter question anymore . I want it to implement it into my header so every view will get this.
...
[/quote]

Zimooon, Your welcome.
To display integer dates, use the date() function (standard php func) and set the output format as you desire:
Code:
...
$str_time = date('d.m.y H:i',$int_time); // <-- $str_time should contain "dd.mm.yy hh:mm" ie: "25.04.11 12:30"
...
You can find the date string format codes in the php manual.

I'm not clear about doing "something in your header". I would do this conversion in the model function which prepares the view data, before it is passed to the view.




Theme © iAndrew 2016 - Forum software by © MyBB