Link to home
Start Free TrialLog in
Avatar of Eu Telea
Eu Telea

asked on

Litle Sql Query, php - change status to 0 if row['total'] iz zero

My ask now it is:

return $query->row['total']; - returns as results counted products from each categorie.

I want  dezactivate the category who dont have products in, by setup in oc_category table status=0

i wish  a nice answer in same function, i want it modified somehow to have 2 active queries, the second one to setup status o if row['total'] is 0

public function getTotalProductsByCategoryId($category_id) {
            		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");
            
            		return $query->row['total'];
            	}

Open in new window


Thank u!

I got this structure for category.

CREATE TABLE oc_category (
  category_id int(11) NOT NULL AUTO_INCREMENT,
  image varchar(255) DEFAULT NULL,
  parent_id int(11) NOT NULL DEFAULT '0',
  top tinyint(1) NOT NULL,
  `new` tinyint(1) NOT NULL,
  `column` int(3) NOT NULL,
  sort_order int(3) NOT NULL DEFAULT '0',
  discounts varchar(255) NOT NULL DEFAULT '',
  `status` tinyint(1) NOT NULL,
  date_added datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  date_modified datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  selected_product_id int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (category_id),
  KEY parent_id (parent_id),
  KEY top (top),
  KEY sort_order (sort_order),
  KEY `status` (`status`),
  KEY parent_id_2 (parent_id),
  KEY top_2 (top),
  KEY sort_order_2 (sort_order),
  KEY status_2 (`status`),
  KEY parent_id_3 (parent_id),
  KEY top_3 (top),
  KEY sort_order_3 (sort_order),
  KEY status_3 (`status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

Open in new window


and here the next structure which are in corelation with first one.

CREATE TABLE oc_category_description (
  category_id int(11) NOT NULL,
  language_id int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  description text NOT NULL,
  text_left text NOT NULL,
  text_right text NOT NULL,
  meta_description varchar(255) NOT NULL,
  meta_keyword varchar(255) NOT NULL,
  custom_title varchar(255) DEFAULT '',
  PRIMARY KEY (category_id,language_id),
  KEY `name` (`name`),
  KEY language_id (language_id),
  KEY language_id_2 (language_id),
  KEY language_id_3 (language_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Open in new window


and

CREATE TABLE `oc_product_to_category` (
  `product_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`product_id`,`category_id`),
  KEY `category_id` (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Open in new window


I got this query who is used in a function model.

$sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name, c.parent_id, c.sort_order, c.status FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category c ON (cp.path_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (c.category_id = cd1.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";

Open in new window


and here is function

public function getTotalProductsByCategoryId($category_id) {
            		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_category WHERE category_id = '" . (int)$category_id . "'");
            
            		return $query->row['total'];
            	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eu Telea
Eu Telea

ASKER

Your solution was good for me.
I just modifie the double equal ==
if ($total == 0) in if ($total = 0)
$query = = in $query =

And my final file it is this and working.

    <file name="admin/model/catalog/category.php">
        <operation info="Change getCategories function query in category model">
            <search position="replace"><![CDATA[
                $sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' &gt; ') AS name, c.parent_id, c.sort_order FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category c ON (cp.path_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (c.category_id = cd1.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";
            ]]></search>

            <add><![CDATA[
                $sql = "SELECT cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' &gt; ') AS name, c.parent_id, c.sort_order, c.status FROM " . DB_PREFIX . "category_path cp LEFT JOIN " . DB_PREFIX . "category c ON (cp.path_id = c.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd1 ON (c.category_id = cd1.category_id) LEFT JOIN " . DB_PREFIX . "category_description cd2 ON (cp.category_id = cd2.category_id) WHERE cd1.language_id = '" . (int)$this->config->get('config_language_id') . "' AND cd2.language_id = '" . (int)$this->config->get('config_language_id') . "'";
            ]]></add>
        </operation>
       
        <operation info="Add new function for number of products in sepcific category to category model">
            <search position="after"><![CDATA[
                class ModelCatalogCategory extends Model {
            ]]></search>

            <add><![CDATA[
            public function getTotalProductsByCategoryId($category_id)
{
    $categ = (int)$category_id;
    $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "product_to_category WHERE category_id = $categ");
    $total = $query->row['total'];
    if ($total = 0)
    {
        $query = $this->db->query("UPDATE oc_category SET `status` = 0 WHERE category_id = $categ LIMIT 1");
    }
    return $total;
}
            ]]></add>
        </operation>
    </file>
Thanks for the points.  In PHP the single equal sign is the assignment operator.  The double equal is the comparison operator for loose comparisons.  The triple equal is the comparison operator for identical comparisons.