Welcome Guest, Not a member yet? Register   Sign In
URL Formatting etc
#1

[eluser]JimmyJ[/eluser]
Think i posted this in the wrong thread before:-

Hey folks

I was wondering if anyone could help me. Trying to work out cl, but having a few troubles just getting to grips with it (never used a framework before).

Anyway, i'm pulling data from a database, and trying to format the string into seo urls and pass the id.

Here's my code for welcome.php

Code:
<?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    
    function index()
    {
        
        $this->load->database();
        $sql = "SELECT region_id, region_name FROM regions ORDER BY region_id ASC";
        $regions['query'] = $this->db->query($sql);
        
        $this->load->view('home', $regions);
    }
    
}
?>

and here's the code for home.php

Code:
<ul>
        &lt;?php foreach($query->result() as $region): ?&gt;
        
        <li><a href="/detail/&lt;?php echo  $region->region_name; ?&gt;/&lt;?php echo  $region->region_id; ?&gt;/property-in-&lt;?php echo  $region->region_name; ?&gt;.html">&lt;?php echo  $region->region_name; ?&gt;</a></li>
        
        &lt;?php endforeach;?&gt;
    </ul>

As you can see home.php is a mess. I would like to format the urls, lowercase and hyphens within the index function in welcome.php

Can anyone give me any tips to work this out?

Thanks
#2

[eluser]jcopling[/eluser]
If I'm understanding you correctly, I would try something like this:
Code:
<ul>
   &lt;?php foreach($query->result() as $region): ?&gt;
      &lt;?php $region_name = strtolower($region->region_name);?&gt;
      <li>&lt;?php echo anchor('detail/'.$region_name.'/'.$region->region_id.'/property-in-'.url_title($region_name, 'dash').'.html',$region->region_name);?&gt;</li>        
   &lt;?php endforeach;?&gt;
</ul>
This is assuming that there is a 'detail' function in your Welcome controller. If not then you will need to add "controllername/" before the "detail" in the URL.
You will also need to make sure that you add the URL Helper first with:
Code:
$this->load->helper('url');
#3

[eluser]JimmyJ[/eluser]
How do i add the detail function in my welcome controller.

I was kind of hoping to do most of the stringtolower and url_title in the welcome controller and add the base url. I just cant work it out. Possibly in a foreach loop?
#4

[eluser]jcopling[/eluser]
I apologize for the assumptions that I made in my previous reply. I will try to be as thorough as possible.

Your Welcome controller should end up looking something like this:
Code:
&lt;?php

class Welcome extends Controller {

    function Welcome()
    {
        parent::Controller();    
    }
    
    
    function index()
    {
        $this->load->helper('url');
        $this->load->database();
        $sql = "SELECT region_id, region_name FROM regions ORDER BY region_id ASC";
        $query = $this->db->query($sql);
        foreach($query->result() as $row){
            $region_name = strtolower($row->region_name);
            $data['links'][] = anchor(base_url().index_page().'detail/'.$region_name.'/'.$row->region_id.'/property-in-'.url_title($region_name, 'dash').'.html',$row->region_name);
        }
        $this->load->view('home', $data);
    }
}
?&gt;
Then the home.php view will look something like this:
Code:
<ul>
    &lt;?php foreach($links as $link){ ?&gt;
        <li>&lt;?php echo $link;?&gt;</li>
    &lt;?php } ?&gt;
</ul>
#5

[eluser]JimmyJ[/eluser]
Thankyou very very much! It's been doing my head in all night.

Just trying to get my head round it, but i'm sure i'll get there.

Thanks




Theme © iAndrew 2016 - Forum software by © MyBB