Welcome Guest, Not a member yet? Register   Sign In
Redirect with "pretty" urls after form submit
#1

[eluser]Greg Aker[/eluser]
I am tackling my first "real" project with Code Igniter. As a nice way to learn, a family member is letting me re-design/code their companies website.

I am using FreakAuth_light for the admin backend, and to change/update the site when an admin is logged in, I am using a lot of ajax in place editing, etc (learning that too =). So far, most everything is working nicely. They don't really care how it's done, so long as they can change text & add photos...they don't want to have to learn any code, and so far, there will be no need for them to use any code.

But I have run into one problem so far.

I am setting up a "projects blog" where he will be able to post new projects he's done, along with photos, etc. I have my form built, but I'm having issues getting it to redirect back to the page with "pretty urls" when the form is submitted.

Truncated form:
Code:
<?=form_open('featuredProject/projectUpdate/'. $featured->project_id);?>
</form>

So I have added the "project_id" to the url of where it is to be updated. Probably not the best way to do it, but you can see why below.

Here is my form controller:

Code:
function projectUpdate()
    {
        $urlSegment = $this->uri->segment(3);
        $this->db->where('project_id', $urlSegment);
        $this->db->update('featuredproject', $_POST);
        
        $urlTitle = $this->db->query("SELECT url_title FROM featuredproject WHERE project_id = $urlSegment");
        redirect ("featuredProject/article/$urlTitle" );

    }

When it goes back to the "featuredProject/article" page, it sets the "$urlTitle" to NULL.
I have tried putting this: redirect ("featuredProject/article/" . $urlTitle ); and redirect ('featuredProject/article/' . $urlTitle );

Here is the "article" controller.
Code:
function article()
    {
        $data['title'] = "Featured Project";
    
        $title = $this->uri->segment(3);
        $query = $this->db->getwhere('featuredproject', array('url_title' => $title));
        $data['featured'] = $query->row();
        
//COMMENTED OUT FOR THE TIME BEING SO I KNOW I CAN SEE ERROR OUTPUT IF IT DOESN'T WORK.
        //if ( $title == '0' ) {
        //redirect('featuredProject/');  
        //}
        
        $this->load->view('featuredProject/featuredProjectEntry_view', $data);
    }


Any ideas?

Many Thanks in Advance,

-greg
#2

[eluser]Chris Newton[/eluser]
The query you're pulling is an resource object, not a single piece of data. You need something like this;

$temp=$query->row();
$temp2=$temp->columnname;

OR

$temp=$query->first_row('array');
if you have a column named featured, you could access it;

$temp['featured']

Review the docs on generating query results.
http://ellislab.com/codeigniter/user-gui...sults.html


Also, if you're going to be doing a lot of this stuff... you might want to research using models for pulling out data, as well as active record. That way you don't have to write the same, or a similar query over and over and over.... you can just pull right from the model if it's loaded.

And on a style note, I personally avoid numbered segment variables like the plague. I prefer associative URI string name/value pairs.... helps me keep things straight in my head, and makes it easier to add / change orders of things in the URI string later if you find you have more or different data to pass. I do this so often, I created a simple uri helper that gets and sets URI data. It's nice for things like this... if (get_uri('title'){ do this }
http://codeigniter.com/wiki/URI_helper/




Theme © iAndrew 2016 - Forum software by © MyBB