Welcome Guest, Not a member yet? Register   Sign In
404 get query from model by id from uri
#1

[eluser]mirag3[/eluser]
When im trying go to localhost/kakobikes_portal/news/1 i getting 404

htacess:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /kakobikes_portal/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

routing:
Code:
$route['news/(.*)'] = 'news/show_news/$1';

controler:
Code:
class News extends CI_Controller {

  function __construct(){
  
    parent::__construct();
    
    $data['idn']= $this->uri->segment(3);
    }
  
  public function show_news($idn = NULL)
{
  if ( ! empty($idn))
  {
    $this->load->model('news_model', $data);  
    $data['news'] =  $this->news_model->Get_news($idn);
  $this->load->view('news_view', $data);
   }
   else
    {
        
    }
}
}

model:
Code:
class News_model extends CI_Model {

  function __parent::{
  }
  
  function Get_news($idn)
{
    $this->get->$data['$idn'];
    $q = $this->db->get('portal_news')
                  ->where('id_news' = $data['$idn']);
  
  return $q->result();
}

}

view:
Code:
Name:&lt;?= $obj->news_name ?&gt;
Date:&lt;?= $obj->date_time ?&gt;
Author:&lt;?= $obj->author ?&gt;


Any ideas? Thanks a lot for any help.
#2

[eluser]Samus[/eluser]
Try?

Code:
$route['news/(:num)'] = 'news/show_news/$1';

On further look, it seems your whole script is wrong.

controller:

Code:
class News extends CI_Controller {

  function __construct(){
  
    parent::__construct();
    
    }
  
  public function show_news($idn = NULL)
{
  if ( ! empty($idn))
  {
    $this->load->model('news_model');  
    $data['news'] =  $this->news_model->Get_news($idn);
  $this->load->view('news_view', $data);
   }
   else
    {
        
    }
}
}

model:

Code:
class News_model extends CI_Model {

  function __parent::{
  }
  
  function Get_news($idn)
{
    $q = $this->db->get_where('portal_news', array('id_news' => $idn));

if($q->num_rows() > 0) :
  
  return $q->row();
else :
return false;
endif;
}

}

view:

Code:
Name:&lt;?= $news->news_name; ?&gt;
Date:&lt;?= $news->date_time; ?&gt;
Author:&lt;?= $news->author; ?&gt;

It's 1am here, but i'm pretty sure that would work, lemme know. Smile
#3

[eluser]mirag3[/eluser]
i was do same changes, but i alredy getting 1 error.

controller:
Code:
class News extends CI_Controller {

  public function show_news($idn = NULL)
{
  if ( ! empty($idn))
  {
    $data['idn']= $this->uri->segment(3);
    $this->load->model('news_model');  
    $data['query'] = $this->news_model->get_news($data['idn']);
    $this->load->view('news_view', $data);
   }
   else
    {
        
    }
}
}  
/* E

model:
Code:
class News_model extends CI_Model {
  
  function __construct()
    {
        parent::__construct();
    }
    
  function Get_news($idn)
{
    $data= $this->uri->segment(3);
    $q = $this->db->where('id_news', $data)
                  ->get('portal_news');
  
  return $q->result();
}

}

view:
Code:
Name:&lt;?= $query->news_name; ?&gt;

error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: views/news_view.php

Line Number: 1
#4

[eluser]mirag3[/eluser]
with your code i getting this.

Code:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting '(' in D:\xampp\htdocs\kakobikes_portal1\application\models\news_model.php on line 5
#5

[eluser]Samus[/eluser]
[quote author="mirag3" date="1333517029"]with your code i getting this.

Code:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting '(' in D:\xampp\htdocs\kakobikes_portal1\application\models\news_model.php on line 5
[/quote]

model:
Code:
class News_model extends CI_Model {

  function __construct()
    {
        parent::__construct();
    }
  
  function Get_news($idn)
{
    $q = $this->db->get_where('portal_news', array('id_news' => $idn));

if($q->num_rows() > 0) :
  
  return $q->row();
else :
return false;
endif;
}

}

Is that better?

And on your code, you're returning it with result(), which returns an array of objects. You're going to need to foreach() your results.

So the view would be something like:

Code:
&lt;?php foreach($query as $q) {
echo $q->news_name; ?&gt;
}
?&gt;

If you're just returning one row from your database, i'd suggest you use row() as it returns just a single row object, so you wouldn't need to use the foreach, like in my example above.

Also you don't need to use $data= $this->uri->segment(3);

Especially not in your model, $idn in your controller is your third URI.

News is 1st
show_news is 2nd
$idn is 3rd

So all you need to do is pass $idn from your controller unto your model method, like I have done above.
#6

[eluser]mirag3[/eluser]
i have this in view:
&lt;?php echo $data['row']->news_name; ?&gt;

error:
Message: Undefined variable: data
Message: Trying to get property of non-object
#7

[eluser]Samus[/eluser]
[quote author="mirag3" date="1333537942"]i have this in view:
&lt;?php echo $data['row']->news_name; ?&gt;

error:
Message: Undefined variable: data
Message: Trying to get property of non-object[/quote]

Try reading my post




Theme © iAndrew 2016 - Forum software by © MyBB