T
T
Tabasov2014-10-20 08:17:00
MySQL
Tabasov, 2014-10-20 08:17:00

Codeigniter how to create a directory correctly?

Good afternoon, I'm studying Codeigniter, I don't have much experience in PHP development.
At the moment, the task is to create a catalog of two related tables, unfortunately I can’t figure out how to do it correctly)
Table structure

mysql> desc items;
+----------+------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+------------------+------+-----+---------+----------------+
| id       | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| items_id | int(11) unsigned | YES  | MUL | NULL    |                |
| title    | varchar(128)     | YES  |     | NULL    |                |
| userfile | varchar(128)     | YES  |     | NULL    |                |
+----------+------------------+------+-----+---------+----------------+
4 rows in set (0.06 sec)

mysql> desc category;
+-------------+------------------+------+-----+---------+----------------+
| Field       | Type             | Null | Key | Default | Extra          |
+-------------+------------------+------+-----+---------+----------------+
| category_id | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name        | varchar(128)     | YES  |     | NULL    |                |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

Query in model
$query = $this->db->query('SELECT * FROM items join category WHERE items_id = category_id');
Controller
$data['uniq'] = $this->category_model->get_uniq_news();
Sample
<?php
    var_dump($uniq);
    foreach ($uniq as $uniq_item)
    {
        $uniq_item['name'];
    }
    ?>
    <?php foreach ($category as $category_item): ?>
        <a style="display: block" href="/catalog/<?= $uniq_item['name'] ?>"><?= $category_item['name'] ?></a>
    <?php endforeach ?>

I brought the catalog, everything suits, but there were difficulties with creating the link ...
I would be very grateful for the help and any advice, thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Entelis, 2014-10-20
@DmitriyEntelis

1. Database fields, and functions too - should be named in such a way that it would be clear what is at stake.
That items_id is a category_id is not obvious at all
2. Don't use *. This is potentially a source of wild hemorrhoids. Do not be lazy to list the fields with the names of the tables.
3. ALWAYS use sorting in sql.
4. I didn't really understand from the code and description what exactly you want to do.
List categories? Display tree category-products? What?
What does it have to do with some news? What's with all the links?

M
Maxim, 2014-10-20
@iru

base_url('catalog/'.$id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question