Welcome Guest, Not a member yet? Register   Sign In
Getting variable from uri into controller, then model, then view
#1

[eluser]ToddyBoy[/eluser]
I've been racking my brain for a few hours with this and it's starting to hurt. I'm sure it's a simple solution.

I have a uri - www.mysite.com/lms/listings/edit/3

I have a route
Code:
$route['lms/listings/edit/:num'] = "lms/listings/edit_listing/$1";
which should mean that from the above URI, it should point to the listings controller (located in the lms folder) and run the edit_listing function using the number 3 as a variable.

The controller
Code:
function edit_listing($id)
{
    $this->load->model('lms/listings_model');
    $data['detail'] = $this->listings_model->edit($id);
    $this->load->view('lms/edit_listing', $data);
}

The model
Code:
function edit($id)
{
    $this->db->where('listing_id', $id);
    $query = $this->db->get_where('listings');
    return $query;
}

When I open the edit_listing view, I have this code
Code:
<?php
  $details = $detail->row_array();
?>        
<form action="" method="post">
  <fieldset>
    <label for="listing_address" id="address-label">Listing address</label>
    &lt;input type="text" name="listing_address" id="listing_address" value="&lt;?php echo $details['listing_address']; ?&gt;" /&gt;
    <br />
    <div id="update-listing">
      &lt;input type="button" name="submit" id="submit" value="Update listing" /&gt;
    </div>
  </fieldset>
&lt;/form&gt;

There will be more input fields in the form, I just put field in there to test that I can get the data, which I can't.

I am getting an error when the page loads.
A PHP Error was encountered
Severity: Notice
Message: Undefined index: listing_address
Filename: lms/edit_listing.php
Line Number: 72

The head bashing on the table bit is that I have run something nearly exactly the same as this, but without having to rely on the segment from the URI to pass through the chain of events to produce data from the database using basically the same code as above.

I have the URI helper autoloaded so I don't think that is the problem. I don't need to run a loop as there is only 1 row of data from the table being returned from the model.

Can anyone save my head from further torture?
#2

[eluser]ToddyBoy[/eluser]
After reading more about URI segments, I changed the controller to this.
Code:
function edit_listing()
{
    $id = $this->uri->segment(4);
    $this->load->model('lms/listings_model');
    $data['detail'] = $this->listings_model->edit($id);
    $this->load->view('lms/edit_listing', $data);
}

I didn't think I had to explicitly assign the URI segment to a variable to use it in the model. I thought that if I put a variable name when calling the function, it can be used.

Doesn't matter. The main thing is, it works and my head can now start to heal.




Theme © iAndrew 2016 - Forum software by © MyBB