Jump to content

assign array to variable


rtofvnt

Recommended Posts

Hi,

I think I've lost quite a few brain cells ... or maybe not.

I can't figure it out or find in in manual.

 

In my template I have nice custom mysql queries that producing nice array of product groups / products.

Before 6 I could just do:

$this->assign('products',$products);

but now it throws an error:

all to a member function assign() on a non-object in

array $product is ok - I var_dump it ant it's ok.

I know something has been changed, but I can't find it.

Please - any help ?

 

Thanks - Matt.

Link to comment
Share on other sites

Thanks sentq.

 

I think I'm doing something fundamentally wrong anyway.

 

My aim was to create in template on homepage table of product groups and products, so what I did was:

duplicated default template and renamed,

in header.tpl at the very top added:

<code>{include_php file='templates/ultimatehosting/config.php'}</code>

where in config.php I initialize new mysqli object and use that to create array of product groups and products which I'm assigning as smarty variable.

Then in homepage.tpl I'm looping through and that's it.

<code>$smarty->assign(... </code> - not working.

I'm not sure if hooks will let me achieve what I'm looking for.

On the other note I'm considering actually to not even using this whmcs system at all - I'm paying lease licence every month but updates that don't care about backward compatibility - that's really bad practice.

 

Thanks for looking at it.

 

Cheers

Link to comment
Share on other sites

Thanks for putting little bit of light on the subject, but it created some shadows that bringing more questions:

1. how should I name file with my custom function in /includes/hooks ?

2. how do I see if it actually works ? In /includes/hool/example.php file I created some basic function:

 

function get_all_products() {
$product_groups_query = "SELECT g.* FROM tblproductgroups g WHERE g.hidden <>1 ORDER BY g.order ASC";
$groups = full_query($product_groups_query);
foreach($groups as $group) {
	echo '<pre>';
	var_dump($group);
	echo '</pre>';
}
}

 

and then within the same file at the bottom:

 

add_hook("ClientAdd",1,"get_all_products");

 

Cheers - Matt.

Link to comment
Share on other sites

Matt,

 

with regards to this->, the link below might help...

 

http://docs.whmcs.com/Version_6_Template_Migration_Guide

 

another option would be to write a data feed and use it on the homepage.

 

http://docs.whmcs.com/Data_Feeds

 

it sounds like you already have the php and sql query, so it should just be a case of formatting the output - and would remove the need to pass variables to Smarty.

Link to comment
Share on other sites

Matt,

 

simply create new file inside /includes/hooks/, with any desired name (productGroups.php) or something and try to put the following code inside:

<?php

function hook_get_all_products($vars) {
   $product_groups_query = "SELECT * FROM `tblproductgroups` WHERE `hidden` <>1 ORDER BY `order` ASC";
   $groups = full_query($product_groups_query);
   while (group = mysql_fetch_assoc($groups)){
       $grouplist[] = $group;
   }

   return array("grouplist" => $grouplist); 
}
add_hook("ClientAreaPage", 1, "hook_get_all_products");

 

then in template you can loop this information like follow:

{foreach item=group from=$grouplist}
   <b>{$group.name}</b>
{/foreach}

Link to comment
Share on other sites

Matt,

 

Yes, WHMCS will call all .php files inside /includes/hooks/ directory, and handle the different hook functions when needed.

all you have to do is to write a valid hook function/file, and leave the rest for WHMCS.

 

And yes you can access the information from any .TPL files.

Link to comment
Share on other sites

it's probably also worth mentioning that I think the hooks run in alphabetical order - e.g a.php will run before b.php - but most times that won't matter... unless you know b needs to run before a!

it should run based on the priority number we specify, right?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated