Post by nil2567 » Thu Apr 26, 2012 7:08 am

Can anybody tell me how to do a simple module that will echo "hello world" when it is stalled?
Just need something to understand MVC of opencart.
many thanks

New member

Posts

Joined
Wed Oct 13, 2010 11:47 pm

Post by Avvici » Thu Apr 26, 2012 12:44 pm

First , could you add to your title (HELLO WORLD) since that is really what you are asking?
The irony of it is that Open Cart is just leaking of Hello Worlds. Most pages you go to something is echoing via HTML that is in direct contact with the controller and model >which contacts the database<. There is no need to make a module for Hello World because Open Cart is already the perfect learning ground.

Here is an example of Hello World. We can just pick and choose there are so many. O0 In this example we will work with the menu in the header.
v1.5.2.1
The files you will be working with:
M(odel):catalog/model/catalog/category.php
V(iew):catalog/view/theme/default/template/common
C(ontrol):catalog/controller/common/header.php
L(anguage) :catalog/language/english/common/header.php

Starting with the control file:
Find this chunk of code which is responsible for populating the menu:

Code: Select all

// Menu
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->data['categories'] = array();
					
		$categories = $this->model_catalog_category->getCategories(0);
		
		foreach ($categories as $category) {
			if ($category['top']) {
				$children_data = array();
				
				$children = $this->model_catalog_category->getCategories($category['category_id']);
				
				foreach ($children as $child) {
					$data = array(
						'filter_category_id'  => $child['category_id'],
						'filter_sub_category' => true	
					);		
						
					$product_total = $this->model_catalog_product->getTotalProducts($data);
									
					$children_data[] = array(
						'name'  => $child['name'] . ' (' . $product_total . ')',
						'href'  => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])	
					);					
				}
				
				// Level 1
				$this->data['categories'][] = array(
					'name'     => $category['name'],
					'children' => $children_data,
					'column'   => $category['column'] ? $category['column'] : 1,
					'href'     => $this->url->link('product/category', 'path=' . $category['category_id'])
				);
			}
		}
We notice that at the very top you have two calls to a model file. These calls are critical in communicating with the model file and basically build the bridge for which data can now travel back and forth:

Code: Select all

$this->load->model('catalog/category');
		$this->load->model('catalog/product');
In all of the code above, look for these lines of code:

Code: Select all

$categories = $this->model_catalog_category->getCategories(0);
				    $children = $this->model_catalog_category->getCategories($category['category_id']);	
					$product_total = $this->model_catalog_product->getTotalProducts($data);
All three of these call particular functions in the Model file (set up by the bridge). In the programming world we call these "stored procedures."

For the sake of this demo were only going to go over one because it's all you need to know to understand the HELLO WORLD.
Open your Model file (listed above) and find the function getCategories which is called here:

Code: Select all

$categories = $this->model_catalog_category->getCategories(0);
The function will look like this:

Code: Select all

public function getCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "'  AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}
All you need to know now is that the function you just communicated with in the Model called upon certain tables in the database, in turn gathered the requested data in the fields and RETURNED AN OBJECT. Now you are left with an object stuffed inside a variable.
Remember $categories = $this->model_catalog_category->getCategories(0); ?
The control then iterates through the OBJECT $categories and that set's the stage for everything else involved in generating the categories, sub categories, names, price etc etc... We won't go over the other two calls.

Now open your VIEW file and find this line of code:

Code: Select all

<?php if ($categories) { ?>
<div id="menu">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
      <?php if ($category['children']) { ?>
      <div>
        <?php for ($i = 0; $i < count($category['children']);) { ?>
        <ul>
          <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
          <?php } ?>
          <?php } ?>
        </ul>
        <?php } ?>
      </div>
      <?php } ?>
    </li>
    <?php } ?>
  </ul>
</div>
<?php } ?>
Notice the if($categories) ??
The next line below that is a foreach loop that will iterate through the multidimensional array $categories like this to start:

Code: Select all

  <?php foreach ($categories as $category) { ?>
Now that we are inside the first LOOP of the multidimensional array we can access the other array like this:

Code: Select all

  <?php for ($i = 0; $i < count($category['children']);) { ?>
And then finally below we have access to the second array by setting up yet another for loop for more data

Code: Select all

 <?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
          <?php for (; $i < $j; $i++) { ?>
          <?php if (isset($category['children'][$i])) { ?>
          <li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
Now open up your LANGUAGE file and find this line:

Code: Select all

$_['text_home']           = 'Home';
And now in your control file find these two lines. One creates a bridge between the control and language. The other creates the variable to be sent to the VIEW:

Code: Select all

$this->language->load('common/header');
$this->data['text_home'] = $this->language->get('text_home');
Again, that line of code is calling the variable(string) from the language file and sticking it inside the variable called "text_home" which is echoed in the VIEW.

In your view you can see it withing the LINKS DIV here:

Code: Select all

<div class="links"><a href="<?php echo $home; ?>"><?php echo $text_home; ?></a><a href="<?php echo $wishlist; ?>" id="wishlist-total"><?php echo $text_wishlist; ?></a><a href="<?php echo $account; ?>"><?php echo $text_account; ?></a><a href="<?php echo $shopping_cart; ?>"><?php echo $text_shopping_cart; ?></a><a href="<?php echo $checkout; ?>"><?php echo $text_checkout; ?></a></div>
I know, you want to see the words HELLO WORLD pop up but we would have gone through the same process to do so. If you go into the ADMIN and change your categories around, add, or remove then you are altering the "database". When these stored procedures are called again then it will generate a new result. A new Hello World.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC

Post by New Guy » Thu Apr 26, 2012 12:58 pm

Thanks for the info ;D Helps a lot. Why does open cart use .tpl and not .html? Wouldn't they produce the same result?

New member

Posts

Joined
Mon Apr 18, 2011 2:16 pm

Post by Avvici » Thu Apr 26, 2012 6:50 pm

A .TPL is essentially a php file. There really is very little difference. So no, not html. The .tpl is "included" just as a regular php file would be included.

User avatar
Expert Member

Posts

Joined
Tue Apr 05, 2011 12:09 pm
Location - Asheville, NC
Who is online

Users browsing this forum: No registered users and 51 guests