A
A
Alexander M2016-02-21 10:28:51
CodeIgniter
Alexander M, 2016-02-21 10:28:51

How to correctly pass id(mysql) parameters not in url in CI?

I'm learning CI. I started to do a training project, got a database of companies and try it.
In general, it turned out to make a working system. But it confuses me that almost all queries are built on fields with keys, and varchar fields.
The database is not large 2k records, it works quickly. But still, how to pass ID parameters not in the URL
Base of car dealerships in the Russian Federation, for example.
How it's done - Example:
Type URL site.ru/russia/moskva We are
on the page of the city, all Moscow car dealerships are displayed here.
In the model, the request looks like this.
...

$this->db->where('city_eng="'.$city.'"');
$query = $this->db->get('select_city_salon_preview')

...
$city -
$uri_segment_city = $this->uri->segment(2);
full search on varchar fields is taken from the controller.
i.e. it is simply taken from the url (moskva) and a full scan in the database and the output of all matching lines.
After reading the book (Schwartz B., Zaitsev P., Tkachenko V. et al. - MySQL. Performance optimization (2nd edition))
I was horrified. what have I done. Although I built an index by city. values ​​are unique. But still.
the same thing when I'm on the page of a car dealership.
/russia/moskva/avtosalon-na-prospekte-123
in model:
...
$this->db->where('name_eng="'.$firm.'"');
$query = $this->db->get('select_firm_info');

...
$firm - берём из контроллера
$uri_segment_firm = $this->uri->segment(3);

also full search on varchar fields.
Is it possible with such a url to somehow use the search by key in the table?
I tried to convey the essence of the problem, if I didn’t write it clearly, tell me I’ll try to describe it.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmitriy, 2016-02-21
@alexander7779

I use a different approach, the principle of CNC generation, there is a table that stores
the CNC address and pure data,
for example, a table: url_chpu_data
url = /russia/moskva/avtosalon-na-prospekte-123
data =
[
'country_id' => 1,
'city_id' => 15,
'company'=>1
];
Naturally, it is stored in a serialized form in the database, by url the index
when entering a NON CNC address, for example /?country_id=1&city_id=15&company=1
the functionality determines what the CNC needs to be generated, receives a city, country, company from the database, translates it into transliteration, forms / russia/moskva/avtosalon-na-prospekte-123
and saves it in the database by pushing the country_id, city_id, company GET request values ​​into a serialized array.
when visiting /russia/moskva/avtosalon-na-prospekte-123, it finds an entry in the database and fills the GET array with the received data, making the functionality work the same way if there was an old version of the link

D
Dmitry Voronkov, 2016-02-21
@DmitryVoronkov

Put it in the url, go and pull it out.
In general, if the database is small and mysql does not slow down, then do not worry. As the saying goes: If it works, don't touch it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question