2

I have two tables in my database one for "LOUNGES" and one for "CITIES". There isn't an explicit connection between them. I mean I haven't put a column "city_id" in the "LOUNGES TABLE". I just take the "name" column from "CITIES" and choose it for a "LOUNGE".

I am using Codeigniter and DataMapperPHP, so at the moment I my index function in lounge.php is listing all the "CITIES" from the "CITIES" table and I have a column called "search" which is the name of the function name in the lounge.php and from there it is easy I just say "where('city', 'London') and I have all the lounges in London where it is listed.

My Question is how to make it better. I mean to make it so to have only one function "view" for example and the links to be generated automatically without me having to add a new function with the city name when I have to add a new city?

Any Ideas how this should work ?

This is an example of the whole thing to see it in code:

public function index()
 {
   $cities = new City_model();
$cities->where('active', '1')->order_by('id', 'ascen')->get();

$recent_cities = array();
foreach ($cities as $city)
{
  $single_city = array
  (
    'id' => $city->id,
    'city_name' => $city->city_name,
    'search' => $city->search,
  );
  array_push($recent_cities, $single_city);
}

$data = array
(
  'cities' => $recent_cities,
  'main_content' => 'templates/city_lounges',
  'active_state' => 'lounge',
);

$this->parser->parse('template_main', $data);
 }

public function london()
{
$lounges = new Lounge_model();
$lounges->where('city', 'London')->order_by('date_added', 'desc')->get();

$recent_lounges = array();
foreach ($lounges as $lounge)
  {
  $id = $lounge->id;
  $fetch_slug = $lounge->club_slug;

  $trimpdate = $lounge->date_added;
  $date = explode(" ", $trimpdate);


  $single_lounge = array
  (
    'id' => $lounge->id,
    'club_name' => $lounge->club_name,
    'club_logo' => $lounge->club_logo,
    'facebook_page' => $lounge->facebook_page,
    'date_added' => $date[0],
    'city' => $lounge->city,
  );
  array_push($recent_lounges, $single_lounge);
}

$data = array
(
  'lounges' => $recent_lounges,
  'main_content' => 'templates/content_lounges',
  'active_state' => 'lounge',
);

// Parse it to the template
$this->parser->parse('template_main', $data);
}
Eddwin Paz
  • 2,842
  • 4
  • 28
  • 48
Unsparing
  • 6,985
  • 2
  • 17
  • 33

1 Answers1

1

I have found the better way of doing it. I made a connection between the 2 tables and I am using the URI to get the segment from the link which also goes in the query to select only the lounges from the cities.

Now it seems like a stupid question to ask...

Unsparing
  • 6,985
  • 2
  • 17
  • 33