Solved

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

Posted on 2014-09-28
3
314 Views
Last Modified: 2014-09-29
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

0
Comment
Question by:Eu Telea
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40348641
Obviously I cannot test this because I do not have your data model, but it seems like it might be right.  Back Up Your Tables Before Using This Code or just install it in your test system to prove it before you use it in a deployed system.

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;
}

Open in new window

0
 

Author Closing Comment

by:Eu Telea
ID: 40350172
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>
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40350235
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.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now