Jump to content

WHMCS 6.0 problems with PHP code not populating the entire page.


senseless

Recommended Posts

Hello,

 

I have the following PHP code in my header.tpl

 

{php}
$userid = $_SESSION['uid'];
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1");  
$fiber = "";
while ($data = mysql_fetch_array($result)) {
$fiber = $data;
}
$fiberid = $fiber['id'];
echo $fiberid;
{/php}

 

This code correctly lists the package id of the user

 

On the same page I have the following code

 

<li><a href="/cp/clientarea.php?action=productdetails&id={php} echo $fiberid; {/php}">My Fiber</a></li>

 

The $fiberid displays just fine in the first group of coding but is blank on the second iteration. It's as is if the php variables don't span the full page. The php variables only seem to span a single {php}{/php} tag. Once you leave this tag none of the php variables are available.

 

Any ideas? Bug?

 

- - - Updated - - -

 

To test I did this

 

{php}
$userid = $_SESSION['uid'];
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1");  
$fiber = "";
while ($data = mysql_fetch_array($result)) {
$fiber = $data;
}
$fiberid = $fiber['id'];
{/php}
{php} echo $fiberid;{/php}

 

The $fiberid does not display correctly. But if I put it in the same {php} tag it works fine. So the issue is definitely that PHP code doesn't span the full page, only the single block.

Link to comment
Share on other sites

I know you've reported this as a bug, so we can leave WHMCS Nate to decide on that one.... :idea:

 

... but one possible workaround might be in your first {php} block to assign $fiberid to a new Smarty variable... that way, where you now have your second {php} block, you can just replace it by calling the new Smarty variable directly.

 

that would totally avoid the need for the second {php} block and you wouldn't run into this issue of being unable to share variables.

Link to comment
Share on other sites

I know you've reported this as a bug, so we can leave WHMCS Nate to decide on that one.... :idea:

 

... but one possible workaround might be in your first {php} block to assign $fiberid to a new Smarty variable... that way, where you now have your second {php} block, you can just replace it by calling the new Smarty variable directly.

 

that would totally avoid the need for the second {php} block and you wouldn't run into this issue of being unable to share variables.

 

Thank you -- I had not even considered it. Back to work :cry:

Link to comment
Share on other sites

What is the appropriate way to assign and use a smarty variable in v6? I'm having the same problems with this method as I was with the php tags. It simply won't place the variable on the page. I can print_r($smarty) and see that the variable is assigned. It's just not possible to use it.

 

{php}
global $smarty;
$userid = $_SESSION['uid'];
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1");  
$fiber = "";
while ($data = mysql_fetch_array($result)) {
$fiber = $data;
}
$smarty->assign('fiberid', $fiber['id']);
{/php}

 

Edit: I gave up and just started sticking blocks of php code everywhere i wanted something custom.

Edited by senseless
Link to comment
Share on other sites

Edit: I gave up and just started sticking blocks of php code everywhere i wanted something custom.

oh we've all been there with WHMCS - staring at code that should work, but simply refuses to! :twisted:

 

the following code should work - it's not a million miles away from yours...

 

{php}
global $smarty; 
$smartyvars = $smarty->getTemplateVars();
$userid = $smartyvars['clientsdetails']['id']; 
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1"); 
while ($data = mysql_fetch_array($result)) {
$fiber = $data;
}
$fiberid = $fiber['id'];
$smarty->assign('fiberid', $fiberid);
{/php} 

 

{$fiberid} should now exist as a Smarty variable and can be called in your template - so when you want to create a link, use...

 

<li><a href="/cp/clientarea.php?action=productdetails&id={$fiberid}">My Fiber</a></li>

Link to comment
Share on other sites

Ya, thats what I had tried (aside from the userid line changes -- ty for that. I was really concerned about pulling uid in from the session variables).

 

The {$fiberid} is still not readable. If I print_r($smarty) after assigning the variable -- I can see it is assigned. I can see the variable in multiple places within the array. But smarty doesn't process the {$fiberid} inside the code.

 

I'm thinking all of this needs to be hooked which I was really hoping to avoid.

 

Current Code:

{php}
global $smarty;
$smartyvars = $smarty->getTemplateVars();
$userid = $smartyvars['clientsdetails']['id']; 
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1");  
$fiber = "";
while ($data = mysql_fetch_array($result)) {
$fiber = $data;
}
$fiberid = $fiber['id'];
$smarty->assign('fiberid', $fiberid);
{/php}

 

<li><a href="/cp/clientarea.php?action=productdetails&id={$fiberid}">My Fiber</a></li>

 

Output on page:

<li><a href="/cp/clientarea.php?action=productdetails&id=">My Fiber</a></li>

 

Seems there is something broken or hooking is really the only way to do it.

 

...

 

Or, could it be my environment? Running cent7 and selinux is disabled. Can you test on your end to see if it's also broken? Something like this..

 

{php} 
$derp = 1; 
global $smarty;
$smarty->assign('derp', $derp);
{/php}
{php} echo $derp; {/php}
{$derp}

 

The above code outputs nothing to my browser. No '11' not even a '1'.

Edited by senseless
Link to comment
Share on other sites

Hooks still work for setting clientarea variables.

 

custom-hook.php

function hook_fiberinfo($vars) 
{
global $smarty;
$smartyvars = $smarty->getTemplateVars();
$userid = $smartyvars['clientsdetails']['id']; 
$result = mysql_query("SELECT *,tblhosting.id as serviceid FROM tblhosting,tblproducts WHERE userid='$userid' AND tblhosting.packageid=tblproducts.id AND tblhosting.domainstatus='Active' ORDER BY tblhosting.id ASC LIMIT 1");  
$fiber = "";
while ($data = mysql_fetch_array($result)) {
	$fiber = $data;
}
return array("fiberid" => $fiber['id']);
}

add_hook('ClientAreaPage', 1, 'hook_fiberinfo');

Link to comment
Share on other sites

I did try the code I posted on my v6 dev site before posting - so it definitely did work! :)

 

as long as the client was logged in, and had services, $fiberid would have a value and was available in the other templates... perhaps it is an environment issue.

 

if I post your test code in header.tpl..

 

{php} 
$derp = 1; 
global $smarty;
$smarty->assign('derp', $derp);
{/php}

then I can call {$derp} in another template, e.g homepage.tpl, and output its value.

 

as you've realised, using hooks would be the better method - but for reference, it's probably worth noting that mysql is getting deprecated from PHP, so you should start to look at the new preferred methods.

 

http://docs.whmcs.com/Interacting_With_The_Database#Deprecated_functionality

 

in the short-term this will continue to work, so the hook isn't going to break tomorrow - but just be aware that it probably will at some point.

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