Welcome Guest, Not a member yet? Register   Sign In
Creating Dynamic URL links?
#1

[eluser]scm22ri[/eluser]
Hi Everyone,

I'm attempting to figure out a way to make dynamic URLS for my CI project.

I'm getting my data from a mysql database and I'm displaying that data on my website. My goal is to make those list items link-able.

For my test project I'm pulling all 50 states. When a user clicks on each individual state I want them to be brought to another page echoing out that state. I haven't figured out how to do this yet. How would I do it?

model_get.php

Code:
<?php
class Model_get extends CI_Model{

Below is my syntax
  function stateData(){  
   $this->db->distinct();
   $this->db->select('state');
   $this->db->order_by('state','ASC');
    $query = $this->db->get('dealers');
    return $query->result();
  }
?>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

My main controller site.php

Code:
public function dealers(){
      $this->load->model("model_get");
   $data["results"] = $this->model_get->stateData();
   $this->load->view("site_header");
   $this->load->view("site_nav");
   $this->load->view("content_dealers", $data);
   $this->load->view("site_footer");  
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

My content_dealers.php

Code:
<div id="content">

&lt;?php
echo '<h1><b>Search By State</b></h1>';

foreach ($results as $row){
  $state = $row->state;
  echo $state.'<br>';
}



?&gt;
</div>
#2

[eluser]PravinS[/eluser]
Update your content_dealers.php page as


Code:
<div id="content">

&lt;?php
echo '<h1><b>Search By State</b></h1>';

foreach ($results as $row){
  $state = $row->state;
  echo '<a href="LINK_TO_PAGE">'.$state.'</a><br>';
}

?&gt;
</div>

just replace the "LINK_TO_PAGE" with your page link
#3

[eluser]scm22ri[/eluser]
Hi,

Thanks for the reply. I would have to make a different function for every state/city. I know that isn't realistic and I also know their is a solution to my problem but I'm not seeing it. I'm not asking anyone to write the syntax for me but what would the syntax to my solution look like? If you were me, how would you attack this issue?

For example, I want a function to echo all of the states and another function to echo all of the cities within those specific states. In regular PHP this is easy using the while and GET but in codeigniter for me it's a little more convoluted. Any help would be appreciated, thanks everyone.
#4

[eluser]micfai[/eluser]
This is relatively easy to do using routes.

You can do something like this if your url was "/dealers".

Code:
$route['dealers/(:[a-z]+)'] = 'dealers/index';

This would allow you to have one file but loads the state data. Here is an example of one I have done that is similar to what you are looking to do. I do have a few extra things for this specific application but it should give you the general idea.

Code:
public function view()
{
  $this->auth->restrict('MyFiles.Content.Edit');

  $id = (int)$this->uri->segment(5);
  
  if (empty($id))
  {
   Template::set_message(lang("file_invalid_id"), 'error');
   redirect(SITE_AREA.'/content/myfiles');
  }

  Template::set('files', $this->myfiles_model->find($id));

  Template::set('toolbar_title', lang("file_edit_heading"));
  Template::set_view('content/view');
  Template::set("toolbar_title", "View File");
  
  Template::render();
  
}




Theme © iAndrew 2016 - Forum software by © MyBB