How to Question? MVC Structure SQL

database
scripts

#1

Ok so within the past month i have been learning the MVC model but I am stuck on something which i'm sure is super simple but i'm unable to figure it out for some reason.

Basically,right now im doing a foreach and im echoing all the products which works great but what i want to do in the right hand corner is get the name of the category it belongs to so if in the item table cat_id = 1 then echo 'PHP Scripts' from the category table but i don't know why but im stuck so all help much appreciated.


Here is my view code:

<?php if($data['isProducts']) { foreach($data['products'] as $product) : ?>
  <div class="col-lg-4 col-md-4 col-sm-6 mb-3">
        <div class="card h-100 shadow-1 rounded-0">
           <a href="<?php echo FULL_ROOT;?>/item/<?php echo $product['id']; ?>/">
              <img class="card-img-top rounded-0" src="<?php echo FULL_ROOT;?>/uploads/items/<?php echo $product['id']; ?>/<?php echo $product['preview_img']; ?>" alt="<?php echo $product['name']; ?>">
                                    </a>
                                    <div class="card-body text-center">
                                        <a href="<?php echo FULL_ROOT;?>/item/<?php echo $product['id']; ?>/"><h2 class="card-title font-weight-bold f-18"><?php echo $product['name']; ?></h2></a>
                                        <div class="clearfix">
                                            <a data-toggle="tooltip" data-placement="top" title="Add to Wishlist" href="#" class="btn btn-sm btn-light btn-lightb float-left mr-1"><i class="far fa-heart"></i></a>
                                            <a data-toggle="tooltip" data-placement="top" title="Live Preview" href="<?php echo $product['demo']; ?>" target="_blank" class="btn btn-sm btn-light btn-lightb float-left"><i class="fas fa-desktop"></i></a>
                                        </div>
                                    </div>
                                    <div class="card-footer bg-white">
                                        <div class="clearfix">
                                            <button type="button" class="btn btn-sm btn-light float-left btn-lightb">
                                              <!-- get name from cat table based on id -->  <?php echo $product['cat_id']; ?>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-light float-right btn-lightb">
                                                <?php echo $data['settings']['payment_currency_sym'].$product['price']; ?>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; } else { ?>
                            <div class="col-12">
                                <div class="alert alert-primary shadow-1 rounded-0" role="alert">
                                    <b><i class="fas fa-info-circle"></i> No Items Available!</b>
                                </div>
                            </div>
                        <?php } ?>

Here is my controller code:

    public function profile($user_id = '')
    {
        $this->item = $this->model('Items');

        $sdata = $this->setting->getAll();
        // Check if empty
        if($user_id == '') { redirect(''); }


        $udata = $this->user->getUserByUsername($user_id); // Gets user data
        $pdata = $this->user->getUserProducts($udata['id']); //Gets products based on user id
//        $prodCategory = $this->item->getItemsCatName();  some how get category name for each product
        if(is_array($pdata[0]) ) {
            $isProducts = true;
        } else {
            $isProducts = false;
        }
        $data = array(
           "user" => $udata,
           "products" => $pdata,
           "isProducts" => $isProducts,
           "prodCategory" => $prodCategory,
           "settings" => $sdata
        );
        // Checks if no data found (404)
        if($udata == false) { redirect('error'); }
        // Load view
        $this->view('marketplace/user/profile', $data);     
    }

Here is my model code:

    public function getUserByUsername($user_id)
    {
        $bind = [':username' => $user_id];
        $results = $this->db->select('msi_users','status = 1 AND username = :username', $bind);
        return $results;
    }
    
    public function getUserProducts($user_id)
    {
        $bind = [':username' => $user_id];
        $results = $this->db->select('msi_items','status = 1 AND author_id = :username', $bind);
        if(!is_array($results[0])) {
            $new_results = array();
            array_push($new_results, $results);
            return $new_results;
        } else {
            return $results;
        }
    }

now im unsure what code to do to get the name, here is what i have currently but i'm clearly being dum so someone else php expertise would be helpful thanks

    public function getItemsCatName($id)
    {
        $bind = [':id' => $id];
        $results = $this->db->select('msi_itemscat','status = 1 AND id = :id', $bind);
        return $results;
    }

Thanks again


#3

For the record, since it's an old post. You are bringing the msi_user table but you are missing to join the category table.

https://laravel.com/docs/5.6/queries#joins


#4

Its ok I figured it out myself! This is only a small project so i'm using a custom built MVC! No need for Laravel :slight_smile: