Welcome Guest, Not a member yet? Register   Sign In
Count entries
#1

[eluser]hostingx[/eluser]
Hello,

i have two questions

first one:

How can i check if an item does exist? so CI wont give any errors:
here's my controller function:
Code:
function item()
    {
        $this->template['link'] = 'home';
        $this->template['title'] = 'Blog item';
        
        $this->template['blog_entry'] = $this->blog_model->Get_entrie($this->uri->segment(3));
        
        $this->_run('blog_item');
    }

And my second question is? how can i count comments that belong to one item?

i was thinking about this:
Code:
function Get_entrie($entryid)
    {
        $this->db->where('id',$entryid);
        return $this->db->get('blog_entry')->row();
    }

But i got errors :O Any other suggestions? because i'm getting crazy Tongue
#2

[eluser]Craig A Rodway[/eluser]
[quote author="hostingx"]How can i check if an item does exist? so CI wont give any errors:
here’s my controller function: [/quote]

Which errors are you getting? What is the code in the current Get_entrie() function?
#3

[eluser]xwero[/eluser]
1) If you are talking about if the segment exists and corresponds with an existing item you can do
Code:
function item()
    {
        
        if($this->uri->segment(3) && $this->blog_model->entry_exists($this->uri->segment(3)))
        {
          $this->template['link'] = 'home';
          $this->template['title'] = 'Blog item';
          $this->template['blog_entry'] = $this->blog_model->Get_entrie($this->uri->segment(3));
          $this->_run('blog_item');
        }
        else
        {
          // default or error redirect
        }
    }
// model
function entry_exists($id)
{
    $query = $this->db->query('select count(*) as tel form table where id=?',array($id));
    $row = $query->row();
    return ($row->tel == 0)?false:true;
}

2) i don't know how you store your comments but i guess a count(*) sql for items with the entry id should do the trick.

edit : forgot the check entry code.
#4

[eluser]hostingx[/eluser]
but what will the syntax be?

because the count function from CI doesnt work!

And i dont mean if the segment was filled in, but i mean, does the database give a record back with that id..

So if you do mysite.com/blog/item/8888888888888888888

That item doesn't exist. So i want to give an error back : "That item cant be fount, sorry"

Hope this is enough information Smile\

Edit:

i get this error:

Fatal error: Call to a member function num_rows() on a non-object in E:\xampp\htdocs\php-developer.nl\php-developer-system\database\drivers\mysql\mysql_driver.php on line 284
#5

[eluser]xwero[/eluser]
see above Smile
#6

[eluser]Craig A Rodway[/eluser]
That method involves double the amount of SQL queries unnecessarrilly and will slow things down.

Again, what is your current code for the Get_entrie function in the blog model so I can help?
#7

[eluser]Negligence[/eluser]
Craig, his approach is necessary for database and application security. I'd hate to see it discouraged here because of the extra overhead (which is minimal), especially since there are a lot of amateur developers learning from the posts here.

This practice should be standard for anyone developing applications. Doing a record check before you operate/use the record is a necessary security measure.
#8

[eluser]Negligence[/eluser]
Also, a simplified version of what I use for validation:
Code:
public function isValidRecord ( $table, $id )
{
    if (is_int($id) && $id > 0)
    {
        $query = $this->CI->db->query("SELECT COUNT(*) AS rows FROM ? WHERE id = ?", array($table, $id));
        $result = $query->row_array();
            
        // We found a match
        if ($result['rows'] == 1)
        {
            return true;
        }    
    }
            
    return false;
}
#9

[eluser]hostingx[/eluser]
Ok, i guess i can use the function as written above Smile thanx!
#10

[eluser]Negligence[/eluser]
No problem, but make sure you're sanitizing the input somewhere before you do the query.




Theme © iAndrew 2016 - Forum software by © MyBB