Sort by category

Asked

Viewed 218 times

2

Following guys I’ve reviewed this community and got some nice fonts but did not help me, I’m trying to separate my links by categories that are also stored in the database.

If anyone has a tutorial to pass me thanks.

See the prints below

1st print category table inserir a descrição da imagem aqui 2nd print link table inserir a descrição da imagem aqui 3rd print home page inserir a descrição da imagem aqui

As you can see in the third image, it’s not separating by categories it’s a tremendous mess, now let’s go to the codes:

My model:

<?php

class Docs_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function getCategories() {
        return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'cats', array());
    }

    public function getDocsCategories() {

        $data = $this->getCategories();

        $arr = [];

        foreach ($data as $value) {
            $arr = $value;
        }

        //return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'docs WHERE id_cat=:id', array(
        //  ':id' => $arr['id_cat']
        //));

        return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'docs c1, ' . DATABASE_PREFIX . 'docs c2 WHERE c1.id_cat = 1 AND c1.id_cat = c2.id_cat', array(
            //':idcat' => $arr['id_cat']
            ));
    }


}

My view:

<section class="section">
    <div class="container">
        <div class="row">
            <div class="col-md-3">
                <?php foreach($data['cats'] as $key => $value): ?>
                <div class="panel panel-default">
                    <div class="panel-heading"><?php echo $value['title'] ?></div>
                    <div class="list-group">
                    <?php foreach($data['docs'] as $key2 => $value2): ?>
                      <a href="#" class="list-group-item"><?php echo $value2['title'] ?></a>
                    <?php endforeach; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>

If you need more information I have, I expect help.

1 answer

3


inserir a descrição da imagem aquiIt depends on the link you want to point to. So that common ones don’t repeat themselves you can use something around that

return $this->_db->read('SELECT * FROM ' . DATABASE_PREFIX . 'cats GROUP BY id_cat', array()); // Agrupamento pelo codigo da categoria.

Further research on SUB-SELECT

Example

SELECT * FROM (
SELECT * 
FROM tabela
ORDER BY id_cliente DESC
) AS retorno
GROUP BY id_cat
LIMIT 4

Complementing the help

Reference that can give you a north.

Basically, from what I understand, you will bring the names of the categories and with the subconsulta bring the records referring to each of them

mysql> SELECT A.id_cat,a.title,(SELECT GROUP_CONCAT(id_doc, title) FROM hb_docs WHERE id_cat = A.id_cat) AS retorno FROM hb_cats A ORDER BY a.title ASC;


+--------+-----------+----------------------------------------------------------+
| id_cat | title     | retorno                                                  |
+--------+-----------+----------------------------------------------------------+
|      1 | Começando | 1Visão Geral,2Requisitos,3Intalação                   |
|      3 | Helpers   | 8Database,9Password,10Session,11Url,12Input,13Validações|
|      2 | O Básico  | 4Controladores,5Modelos,6Visualizações,7Erros          |
+--------+-----------+----------------------------------------------------------+
3 rows in set

hope it helps.

  • looks almost worked out, see print, note that this listing all the same :/ http://i.imgur.com/8Toz1bR.png

  • is would have to look like this: http://i.imgur.com/sS7dPur.png, look at the prints dad my table, onte id 1 = starting, 2= basic, 3 = helpers, and what’s happening is this: http://i.imgur.com/v3cylJI.png ta tense :/

  • That’s right, the first query will bring you code and category name, so the GROUP BY ( Getting(1), Basic(2) and Helpers(3)). And with the subconsultation you take the code of each record and bring your data with the WHERE clause (in the case id_cat (1,2 or 3) see A.Category, A.id_cat) with the code that came in the query using GROUP_CONCAT(). In subconsultation you say: (bring all records where id_cat = A.id_cat ).

  • I’ll try to tidy up, with the tips you gave me I think should be possible, if I can get back here and mark the answer as solved att.

  • It’s hard to go to sleep, tomorrow I continue, thanks for the help.

  • 1

    I performed a scenario and managed to reach the expected result. I edited the answer. See if you can finish, just remembering that the return needs to be treated.

  • The direct query in phpmyadmin returns everything to me correctly, however, in logic in my model still keeps returning as in print. :/

  • I think the problem is in the display there look my code in my view, first time in 3 years fiddling with PHP that I get into a gambiarra of these kkkkkk

  • Put the query according to its logic here getCategories(); and print_r here getDocsCategories() to see the return. I would send $data to the view and give it a var_dump or print_r() to see what it brings.

  • You’re really gonna have to go through your view. in your first foreach gets 03 items like id_cat, title and a long return string, for each row of the first the second foreach you do using a explode to treat the return.

  • I marked as solved, what I wanted you explained to me and it worked out now is only adjustments, I am now having difficulty in receiving in the view

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.