CodeIgniter Forums

Full Version: only pull one row from db to display problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]dadamssg[/eluser]
Im just trying to display one row of information on a page and i keep getting a 404.

I'm using an 'events' controller and im trying to pull the second segment of the uri as the event id number to query.

heres my event controller
Code:
class Events extends Controller {

    function Events()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->helper('date');
        $this->load->helper('text');
        $this->load->helper('form');
        $this->load->library('session');
        $this->load->model('Eventmodel');
    }

    function index($id = null)
    {
        //load models
        
        
    $newdata = array(
                   'page'  => 'mains',
               );
        $this->session->set_userdata($newdata);
        $data['title'] = "Home";
        $data['heading'] = "All Events";
        
        $page = $this->uri->segment(2);
        if (isset($page) && is_numeric($page))
        {
              $page = $page;
            }
            else
            {
              $page = 867;
            }
        $data['query'] = $this->Eventmodel->get_event($page);
            
    
        $this->load->view('header_view', $data);
        
        
        $this->load->view('event_view', $data);
    }
    
    
    
}

and heres my view
Code:
<?php
session_start();
?>



    <?php foreach($query->result() as $row): ?>

    <h3>&lt;?php echo clean_up($row->title); ?&gt;</h3>
    
    &lt;?php echo easy_date($row->start); ?&gt; -
    &lt;?php echo easy_date($row->end); ?&gt;
    
    <p>
       &lt;?php echo clean_up($row->description); ?&gt;
       <h5>&lt;?php echo "By ".$row->createdby; ?&gt;</h5>
    </p>

    <hr>
    &lt;?php endforeach; ?&gt;



&lt;/body&gt;
&lt;/html&gt;
i think i need to change that foreach thing because im only dealing with one row

my head_view just opens up the html
Code:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
&lt;html &gt;
&lt;head&gt;
&lt;link rel='stylesheet' type='text/css' href='silk.css' /&gt;
&lt;title&gt; &lt;?=$title?&gt; &lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>&lt;?=$heading?&gt;</h1>
<hr>
<ul>
<li>&lt;?=anchor('mains', 'Home')?&gt;</li>
<li>&lt;?=anchor('mains/sports', 'Sports')?&gt;</li>
<li>&lt;?=anchor('mains/performance', 'Performance')?&gt;</li>
<li>&lt;?=anchor('mains/organization', 'Organization')?&gt;</li>
<li>&lt;?=anchor('mains/random', 'Random')?&gt;</li>
</ul>
<hr>

El Forum

[eluser]dadamssg[/eluser]
oh and heres the model im using
Code:
&lt;?php

class Eventmodel extends Model {

    function Eventmodel()
    {
        parent::Model();
    }
    
    function get_event($num)
     {
     $query = $this->db->query("SELECT * FROM test WHERE eventid = '$num'");        
     return $query;    
     }
    
     function get_exists($eventid)    
    {        
    $query = $this->db->query("SELECT * FROM test WHERE end >= NOW() AND eventid = '$eventid'");
    $numrows = $query->num_rows();
    return $numrows;
    }
}
?&gt;

El Forum

[eluser]Craig A Rodway[/eluser]
If you read the documentation, you will see there is a function for returning one row - look for row().

El Forum

[eluser]dadamssg[/eluser]
well now ive learned that its not that. Its that i can't get the uri segment. For whatever reason i just can not assign the 3rd uri to anything. the uri im trying to capture is the number like below

mysite.com/events/867

im trying to use to get it but won't work
Code:
$id = $this->uri->segment(2);
        if (isset($id) && is_numeric($id))
        {
              $page = $id;
            }
            else
            {
              $page = 867;
            }

and i know i have an event with an id of 867 so thats why i used that number. When i go to mysite.com/events/ it pulls up that event like its suppose to but if i try to add a third segment with another event id, say 868(which is also in the db), it pulls up a 404

El Forum

[eluser]dadamssg[/eluser]
*i mean 2nd segment

El Forum

[eluser]Craig A Rodway[/eluser]
In that case, you should read the page about URLs in the user guide.

Quote:The first segment represents the controller class that should be invoked.
The second segment represents the class function, or method, that should be called.
The third, and any additional segments, represent the ID and any variables that will be passed to the controller.

From your example, mysite.com/events will call the index function of the event controller - which will work OK. But when you try to go to mysite.com/events/868, it will not work because the 868 function doesn't exist in the events class.

You have two options - make the URL include index mysite.com/events/index/868; or add a manual route like this using Routing.

Code:
$route['events/(:num)'] = 'events/index/$1';

El Forum

[eluser]dadamssg[/eluser]
awesome, THANK YOU. its working now and i understand that now.