Welcome Guest, Not a member yet? Register   Sign In
Format url
#1

[eluser]JimmyJ[/eluser]
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]Gavin Vickery[/eluser]
You could format the values returned from your SQL, and assign them to a new variable before you send it to the view

Code:
function index()
{
        
    $this->load->database();
    $sql = "SELECT region_id, region_name FROM regions ORDER BY region_id ASC";
    $query = $this->db->query($sql);

    $viewData['link_html'] = "";
    foreach($query->result() as $row)
    {
        $viewData['link_html'] .= '<li>';
        $viewData['link_html'] .= '<a href="/detail/'.$row->region_name.'/'.$row->region_id.'/property-in-'.$row->region_name.'.html">';
        $viewData['link_html'] .= $region->region_name;
        $viewData['link_html'] .= '</a>';
        $viewData['link_html'] .= '</li>\n';
    }
        
    $this->load->view('home', $viewData);
}

This makes your view very easy

Code:
<ul>&lt;?=$link_html?&gt;</ul>

Obviously you don't need to break out the link in the index() function that much, I just did it for clarity.

Hope that helps.




Theme © iAndrew 2016 - Forum software by © MyBB