Jump to content

How to pass the whole table values from php to tpl file in the provisioning module?


Recommended Posts

 try {
        // Call the service's function based on the request action, using the
        // values provided by WHMCS in `$params`.

	$conn=mysqli_connect("localhost","admin1","admin","hello");
	$query="SELECT * FROM t_hello";
	$run=mysqli_query($conn,$query);
	while($row=mysqli_fetch_assoc($run))
	{		
		$set1=$row['id'];
		$set2=$row['name'];
		$set3=$row['email'];	
	
	//$response = array();
        
	
	return array(
            'tabOverviewReplacementTemplate' => $templateFile,
            'templateVariables' => array(
                'extraVariable1' => $set1,
                'extraVariable2' => $set2,
		'extraVariable3' => $set3,
            ),
        );
	
	} 
	
	
    } catch (Exception $e) {
        // Record the error in WHMCS's module log.
        logModuleCall(
            'provisioningmodule',
            __FUNCTION__,
            $params,
            $e->getMessage(),
            $e->getTraceAsString()
        );

        // In an error condition, display an error page.
        return array(
            'tabOverviewReplacementTemplate' => 'error.tpl',
            'templateVariables' => array(
                'usefulErrorHelper' => $e->getMessage(),
            ),
        );
    }	

I have used the above given code to fetch data from my database. But, I do not know how to pass it to .tpl file. 

The code of .tpl file is as follows.

{$extraVariable1}
{$extraVariable2}
{$extraVariable3}


The output shows me only the first record of my table. Where is the problem?

Link to comment
Share on other sites

Try this for your module code:
 

while($row=mysqli_fetch_assoc($run))
    {       
        $tmpArr['dbData'][] = [
            'id' => $row['id'],
            'name' => $row['name'],
            'email' => $row['email']
        ]

/*        
        $set1=$row['id'];
        $set2=$row['name'];
        $set3=$row['email'];    
*/   
    //$response = array();
        
    
    return array(
        'tabOverviewReplacementTemplate' => $templateFile,
        'templateVariables' => $tmpArr;
/*
        'templateVariables' => array(
            'extraVariable1' => $set1,
            'extraVariable2' => $set2,
            'extraVariable3' => $set3,
        ),
*/
    );
    
} 

 

Then loop through your {$templateVariables} array in your template to extract the data. 

Link to comment
Share on other sites

14 hours ago, vortex2000 said:

Try this for your module code:
 


while($row=mysqli_fetch_assoc($run))
    {       
        $tmpArr['dbData'][] = [
            'id' => $row['id'],
            'name' => $row['name'],
            'email' => $row['email']
        ]

/*        
        $set1=$row['id'];
        $set2=$row['name'];
        $set3=$row['email'];    
*/   
    //$response = array();
        
    
    return array(
        'tabOverviewReplacementTemplate' => $templateFile,
        'templateVariables' => $tmpArr;
/*
        'templateVariables' => array(
            'extraVariable1' => $set1,
            'extraVariable2' => $set2,
            'extraVariable3' => $set3,
        ),
*/
    );
    
} 

 

Then loop through your {$templateVariables} array in your template to extract the data. 

I understood the concept, but still I am having the same problem. Can you show me how should I show these variables in my .tpl file?

Link to comment
Share on other sites

I have found a solution for the issue that I would like to share:

I modified my code to one given here:

	$finalarray=array();
	$response=array();
	$i=1;
	while($row=mysqli_fetch_assoc($run))
	{		

		foreach($row as $rows)
		{
			$response = array(
			'extraVariable1'.$i => $row['id'],
			'extraVariable2'.$i => $row['name'],
			'extraVariable3'.$i => $row['email'],
			);
		}
	
		$finalarray=$finalarray+$response;
		$i++;
	} 
	
        return array(
            'tabOverviewReplacementTemplate' => $templateFile,
	    'templateVariables' => $finalarray,
	); 

and then in .tpl file I wrote this:

{$extraVariable11}
{$extraVariable21}
{$extraVariable31}
{$extraVariable12}
{$extraVariable22}
{$extraVariable32}
{$extraVariable13}
{$extraVariable23}
{$extraVariable33}

The next problem I am facing is : how to make my variable name dynamic in .tpl file so that I can do the above task using loop?

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • 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