Welcome Guest, Not a member yet? Register   Sign In
Best Method to store a Checkbox
#5

(01-03-2024, 04:20 AM)demyr Wrote: Why don't you also add the category_id to your user_preference table? What happened to that?

Do you mean limiting the user for maximum 3 options?
First, I would go with frontend control & warning with JS. And then I would control it again in Controller and proceed.

yes you are right. also added category_id.

sorry if it took me a while to respond, but I was developing the right method to generate the page that displays the form with the checkboxes.

I think I created a bit of chaos and I think I went around the same query several times to get the data out.

PHP Code:
class SchedaAlimentiController extends BaseController {

    public function SchedaAlimenti() {
        
        $db 
= \Config\Database::connect();
        $builder $db ->table('categorie_alimentari');
        $builder->select('id_categoria_alimentare ,nome');
        $query $builder->get();
        $data['categoria'] = $query->getResult();

        $db = \Config\Database::connect();
        $builder $db ->table('categorie_alimentari');
        $builder->select('categorie_alimentari.id_categoria_alimentare, categorie_alimentari.nome AS categoria, alimenti.nome AS alimento, alimenti.id_alimento');
        $builder->join('alimenti''categorie_alimentari.id_categoria_alimentare=alimenti.id_categoria_alimentare');
        $query $builder->get();
        $data['alimenti'] = $query->getResult();
        
        
return view('utente/schedaAlimenti'$data);

    }


In the view:
PHP Code:
<?php
foreach ($categoria as $riga):
    echo "<strong>" $riga->nome "</strong><br>";
    foreach ($alimenti as $alimento):
        if ($alimento->categoria === $riga->nome){
            echo $alimento->alimento "<br>";
        
    endforeach;
    echo "<br>";
endforeach;
?>

Example result:
Yogurt
Yogurt Magro
Yogurt Intero
Yogurt Greco Intero

Legumi
Fagioli
Lenticchie
Ceci
Piselli
Fave

do you think this is a good way to download and visualize the data, or can you give me some pointers to improve the code?

obviously then in the view there will be the CSS and the entire part relating to the form and for each product there will be the relevant checkbox.

any advice is welcome
Reply


Messages In This Thread
Best Method to store a Checkbox - by elbambolo - 01-02-2024, 09:32 AM
RE: Best Method to store a Checkbox - by demyr - 01-02-2024, 02:10 PM
RE: Best Method to store a Checkbox - by demyr - 01-03-2024, 04:20 AM
RE: Best Method to store a Checkbox - by elbambolo - 01-05-2024, 10:23 AM
RE: Best Method to store a Checkbox - by demyr - 01-07-2024, 09:39 PM



Theme © iAndrew 2016 - Forum software by © MyBB