Welcome Guest, Not a member yet? Register   Sign In
Set Posts activ/inactiv
#1

[eluser]Zimooon[/eluser]
Hey guys,
i got for my news/blog a variable called active. when i write/edit posts i can set it to 0 or 1. Now i want only to show posts that are flagged with 1 .
i got a post model to get posts but when i do sth like if($active==1) or stuff it wont work. Where would you implement it and how?

best regards
#2

[eluser]eoinmcg[/eluser]
what does the query in your model look like?

try something like this:

Code:
function get_active_posts()
{

    $query = $this->db->where('active', 1)
                ->get('posts');
                
    return $query;

}
#3

[eluser]Zimooon[/eluser]
Code:
function getPosts() {
    $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
            
            
        );
    }

    return $posts;
}

This is my standard model code to get all posts ( to view them in adminpanel).

How to write this to check active?
#4

[eluser]TWP Marketing[/eluser]
Zimooon, pass the active var to your function, which defaults to FALSE (0),
If you pass TRUE (1) the function should set the query where clause:

Code:
function getPosts($active = FALSE) {
    if($active == TRUE)
    {
     $this->db->where('active',1);
    }
    $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
            
            
        );
    }

    return $posts;
}
#5

[eluser]Zimooon[/eluser]
Allright thank you that could work.
Now until now i had the view like this:
Code:
<?php if (isset($posts) ) : foreach ($posts as $p):      ?>
          
  
          <h1 id="news">&lt;?php echo $p['title']; ?&gt;</h1>
        
          
          &lt;?php echo $p['content']; ?&gt;
          <br />
          &lt;?php echo $p['date']; ?&gt;
      
          

          &lt;?php endforeach; else: ?&gt;
          

           <h1 id="news">Keine aktuellen News.</h1>
            
          &lt;?php endif; ?&gt;
          
          </article>

My controller calls the method :

Code:
function index()
    {
    
      $data = array(
      'title'           => 'Aktuell'.$this->config->item('pageTitle'),
      'metaDescription' => 'Aktuell',
      'metaKeywords'    => 'Aktuell, '.$this->config->item('pageMetaKeywords'),
      'mainnav'         => 'aktuell',
      'subnav'          => '',
      'subsubnav'       => '',
      'hasSubnav'       => true
    );
    
    
    
    $data['posts'] = $this->posts_model->getactivePosts();
      $this->load->view('v-aktuell', $data);
    }

and the modell looks like the code you posted above.
Now still nonactive posts are shown Sad
don get it why..

best regards
#6

[eluser]TWP Marketing[/eluser]
You appear to have two functions:
getPosts()
and
getactivePosts()

Which one are you calling? Please show the entire function here.

ALSO
What is the data type used in your database for the field 'active'? if it is a string character, it will return TRUE whether it contains '0' or '1'. It should be an integer or boolean type.
#7

[eluser]Zimooon[/eluser]
Hey

I call this function: getactivePosts() :

Code:
}      

  function getactivePosts($active = FALSE) {
    if($active == TRUE)
    {
     $this->db->where('active',1);
    }
    $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
            
            
        );
    }

    return $posts;
}

In my database active is :

typ: tinyint
length/set : 1
standard: NULL
attribute: unsigned
Null: "checked"
#8

[eluser]Zimooon[/eluser]
ok got the error..

if($active == TRUE)

->

if($active = TRUE) . now it works <3
thx for help

best regards
#9

[eluser]TWP Marketing[/eluser]
Zimoon,
Now you have two errors which do not work as you want them to do.

First, in your controller, pass the $active flag to the model function.
Code:
function index()
    {
    
      $data = array(
      'title'           => 'Aktuell'.$this->config->item('pageTitle'),
      'metaDescription' => 'Aktuell',
      'metaKeywords'    => 'Aktuell, '.$this->config->item('pageMetaKeywords'),
      'mainnav'         => 'aktuell',
      'subnav'          => '',
      'subsubnav'       => '',
      'hasSubnav'       => true
    );
    
    
    $active = TRUE; //<--- assign the value of $active
    $data['posts'] = $this->posts_model->getactivePosts($active); //<--- PASS THE $active FLAG
      $this->load->view('v-aktuell', $data);
    }

Second, in your model file:

[quote author="Zimooon" date="1303224924"]ok got the error..

if($active == TRUE) //<--- THIS IS CORRECT, you are doing a comparison

->

if($active = TRUE) . now it works <3 //<--- THIS IS NOT CORRECT, it must not be an assignment
thx for help

best regards[/quote]

Your if clause must use "==" to make a comparison. If you use "=", you are doing an assignment to the variable and the if() will ALWAYS execute the code inside the if statement.




Theme © iAndrew 2016 - Forum software by © MyBB