Post by harryusa » Thu Mar 03, 2011 8:51 pm

I've been reading tutorials and looking through the controller files and I just can't seen to get my head around handling $_GET['data'] in the controller and outputting it to my custom template. The template displays correctly until I throw in my script starting with: IF ($this->request->server['REQUEST_METHOD'] == 'GET'
I have a url that looks like this:

Code: Select all

http:.../index.php?route=services/order&height=8&width=10
I want to process height*width in my controller script named order.php and output it to my template, order.tpl
So far I have:

Code: Select all

<?php
class ControllerServicesOrder extends Controller {
public function index() {
$this->language->load('services/order');
$this->document->title = $this->language->get('heading_title');
IF ($this->request->server['REQUEST_METHOD'] == 'GET') {
$this->request->get['height'];
$this->request->get['width'];
if(($height) or ($width) == NULL))
{$area = 0;}
ELSE {$this->$area = $height*$width;}
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/services/order.tpl')) {
$this->template = $this->config->get('config_template') . '/template/services/order.tpl';
} else {
$this->template = 'default/template/services/order';
}
}
?>
In order.tpl I use the variable $area where I need it But I keep getting
Looking at all the online tutorials I see examples of declaring variables and giving them values in the CLASS, What good is that? I've been looking to find out how to pass $_GET data to the CLASS, do arithmetic or logical operations on it and output it back to the template.
Can anyone tell me what is wrong with my Class or URL ?
-Harry
Last edited by harryusa on Fri Mar 04, 2011 2:30 am, edited 2 times in total.

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by Xsecrets » Thu Mar 03, 2011 10:05 pm

In order to use the $area variable in the .tpl you'll need to put it into the data array.

Code: Select all

$this->data['array'] = $array;

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by harryusa » Fri Mar 04, 2011 12:20 am

Thanks xsecrets,
So I put $this->data['area'] = $area; into the order.php controller script and now my template outputs with an error
on the line I first use the index variable $area:

Code: Select all

Notice: Undefined index: in .../catalog/view/theme/default/template/services/order.tpl on line 133
Is the syntax correct? Do I declare the $area variable for global use?
Thanks again!

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by Xsecrets » Fri Mar 04, 2011 1:11 am

no you don't have to declare it global. my guess is that the server REQUEST_METHOD stuff is not right. so it never gets set. I haven't looked at the request method in a while, so I'm not even sure if it has the server stuff in it. At any rate I know I've never seen anyone else utilize it. It would probably be better if you simply checked the get variables directly.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by SXGuy » Fri Mar 04, 2011 1:48 am

just wanna point out, shouldnt this

Code: Select all

class ControllerServicesOrder extends Controller {
be this?

Code: Select all

class ControllerServiceOrder extends Controller {
plus your url

Code: Select all

http:.../index.php?route=service/order&height=8&width=10
points to service, yet your language files and im guessing other files all point to services

So before we can work out why its not working, i think you need to decide whether its service or services that the data is being located at.

Active Member

Posts

Joined
Sun Nov 08, 2009 2:07 am

Post by harryusa » Fri Mar 04, 2011 2:29 am

Thanks SXGuy,
You are correct but the it was careless on my part in writing the example. I just double checked and it is "services" in the controller, template and url. I have a feeling XSecrets is on to something, other than I don't know what I am doing in the OOP controller script, with regard to the server REQUEST_METHOD syntax. This leads me to wonder if I need to place/declare any new variables somewhere other than the controller or if my syntax is just wrong.

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by Xsecrets » Fri Mar 04, 2011 2:35 am

try this

Code: Select all

   <?php
    class ControllerServicesOrder extends Controller {
		public function index() {
			$this->language->load('services/order');
			$this->document->title = $this->language->get('heading_title');
			IF ((isset($this->request->get['height']) && !empty($this->request->get['height'])) || (isset($this->request->get['width']) && !empty($this->request->get['width']))) {
				$height = $this->request->get['height'];
				$width = $this->request->get['width'];
				$this->data['area'] = $height*$width;
			} else {
				$area = 0;
			}
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/services/order.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/services/order.tpl';
			} else {
				$this->template = 'default/template/services/order';
			}
		}
	}
    ?>

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by harryusa » Fri Mar 04, 2011 3:00 am

Thanks Xsecrets,
Everything looks correct but now the page doesn't load at all; white browser screen like the template isn't being called. You have all the directories spelled correctly, maybe it's getting hung-up in the IF statement NULL checking.
I'll try bypassing it temporarily.

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by harryusa » Fri Mar 04, 2011 3:05 am

I get an undefined index error on both variables:

Notice: Undefined index: height in /public_html/shop/catalog/controller/services/order.php on line 12Notice: Undefined index: width in /public_html/shop/catalog/controller/services/order.php on line 13

Code: Select all

12.    $height = $this->request->get['height'];
13. $width = $this->request->get['width'];

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by openmycart.com » Fri Mar 04, 2011 3:21 am

untested but it seem need like this:

Code: Select all

               if (isset($this->request->get['height'])) {
			$height = $this->request->get['height'];
		} else {
			$height = '';
		}

               if (isset($this->request->get['weight'])) {
			$weight = $this->request->get['weight'];
		} else {
			$weight = '';
		}

Find and get many various of opencart modules, themes, mods, etc for your opencart store at http://www.openmycart.com/oc/, OPENCART SITE customization and Maintenance supports at here


User avatar
Active Member

Posts

Joined
Tue Oct 12, 2010 4:47 am


Post by SXGuy » Fri Mar 04, 2011 3:25 am

openmycart.com wrote:untested but it seem need like this:

Code: Select all

               if (isset($this->request->get['height'])) {
			$height = $this->request->get['height'];
		} else {
			$height = '';
		}

               if (isset($this->request->get['width'])) {
			$weight = $this->request->get['width'];
		} else {
			$width = '';
		}

or try

Code: Select all

if (isset($this->request->get['height'])) {
      		$this->data['height'] = $this->request->get['height'];
		} else {
      		$this->data['height'] = ' ';
    	}
if (isset($this->request->get['width'])) {
            $this->data['width'] = $this->request->get['width'];
      } else {
            $this->data['width'] = ' ';
       }

Active Member

Posts

Joined
Sun Nov 08, 2009 2:07 am

Post by harryusa » Fri Mar 04, 2011 3:39 am

I think all you guys have good error checking but look at the line generating the error now.
Undefined variable error(width & height) on this line: $this->data['area'] = $height*$width;

Am I calling these variable correctly to multiply them?

Could it be my URL:
/index.php?route=services/order&height=8&width=3

BTW I did a var_dump and the data is being passed by the URL:
["request"]=> object(Request)#8 (5) { ["get"]=> array(3) { ["route"]=> string(14) "services/order" ["height"]=> string(1) "8" ["width"]=> string(1) "3" }

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by Xsecrets » Fri Mar 04, 2011 4:27 am

using my previous code try setting the variables like

Code: Select all

$height = (int)$this->request->get['height'];
just in case it's not multiplying because it's a string. Though php generally handles this fine without having to specify a data type.

OpenCart commercial mods and development http://spotonsolutions.net
Layered Navigation
Shipment Tracking
Vehicle Year/Make/Model Filter


Guru Member

Posts

Joined
Sun Oct 25, 2009 3:51 am
Location - FL US

Post by harryusa » Fri Mar 04, 2011 4:49 am

Finally! That was the line I was looking for. I also added width and echoed the product of the multiplication. Thank you:

Code: Select all

$height = (int)$this->request->get['height'];
$width = (int)$this->request->get['width'];
$area = $height * $width;
echo  $area;
Now if I do this: $this->data['area'] = $area; Can I use $area to access the $area variable in the template?

Now the template is still not loading.
This looks correct to me, anyone else?

Code: Select all

  if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/services/order.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/services/order.tpl';
             } else {
             $this->template = 'default/template/services/order';
             }

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by harryusa » Fri Mar 04, 2011 5:02 am

Something funny going on with the "mini" order.php controller module. I put the same code in one of the other controller files, changed the name and order.tpl loads.
The only issue is I can only echo $area from the controller script NOT the template. The error "Undefined Variable" is returned along the line number where $area appears in the template.

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm

Post by SXGuy » Fri Mar 04, 2011 5:04 am

harryusa wrote:This looks correct to me, anyone else?

Code: Select all

  if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/services/order.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/services/order.tpl';
             } else {
             $this->template = 'default/template/services/order';
             }

Code: Select all

  if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/services/order.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/services/order.tpl';
             } else {
             $this->template = 'default/template/services/order.tpl';
             }
$height = (int)$this->request->get['height'];
$width = (int)$this->request->get['width'];
$this->data['area'] = $height * $width;

Try that

Active Member

Posts

Joined
Sun Nov 08, 2009 2:07 am

Post by harryusa » Fri Mar 04, 2011 5:30 am

THANK YOU! I got to work just prior to your update and did the exact same thing and I am able to access it on the .tpl page.
I just can't get the order.php controller to load the template except by bastardizing another controller file. I'll figure it out by just stripping the file down little by little. Thanks again. I wish I could have found more info on using classes in the MVC structure. Most examples out there write the CLASS like it is the means of input and output. I saw stuff like $name = "Tom"; Now who would hard code a variable, let alone inside a CLASS? And most of the tutorials seem to do the same thing. Anyone have a good tutorial for a novice to OOP please post the link.
Thanks again,
Harry

New member

Posts

Joined
Mon Feb 21, 2011 12:23 pm
Who is online

Users browsing this forum: Majestic-12 [Bot] and 65 guests