Jump to content

Call data variables seperately from same function to add to HTML layout template


experience40

Recommended Posts

Wondering how i can call specific variables from the same function which i can then use for a HTML layout template

If i have a 'data' function with several variables how can i call the variables seperately?

Quote

SELECT title, category, description, image_link FROM TABLE1 WHERE product_id=1;

function data () {

//Abbrieviated code....
SELECT title, category, description, image_link FROM TABLE1 WHERE product_id=1;

 

$title = $row['title'];
$category = $row['category'];
$description = $row['description'];
$image_link = $row['image_link'];

...
}

template.jpg

Link to comment
Share on other sites

I think this is something like what you're after:

 

function data () {
	//Abbrieviated code....
	SELECT title, category, description, image_link FROM TABLE1 WHERE product_id=1;
	
	return $row ; 
}

function template( string $title, string $category, string $description, string $image_link ) : string 
{
	$tmp = '<div class="t">TITLE %s</div>' 
		. '<div class="l">CATEGORY %s</div>' 
		. '<div class="m">DESCRIPTION %s</div>' 
		. '<div class="r">IMAGE LINK <img src="%s"/></div>' 
		; 
	return sprintf( $tmp, $title, $category, $description, $image_link ); 
}

function go() {

	$row = data(); 

	echo template( 
		  $row['title']
		, $row['category']
		, $row['description']
		, $row['image_link']
		); 
}

Regards, 
   Phill W. 

 

Link to comment
Share on other sites

thanks Phill

I think the way you've suggested is to run the 'html' code directly from the backend php file via echo which wont work if there are several template designs, however i can see you have seperated the functions which was one of the things i was wondering about

Basically there will be 2 files:
1x html / php (frontend) - there may be several 'template' layout designs depending on the users choice
1x php (backend)

The frontend will need to display the data from the backend 

Frontend example

<?php 
   require "/functions/data.php";   
?>

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" name="viewport" content="width=device-width"/>
		<link rel="stylesheet" type="text/css" href="/resources/css/bootstrap.css"/>
	</head>
<body>
<table border="1" cellpadding="1" cellspacing="1">
	<tr>
		<td>
		<?php		
		data($title);
		?> 
		</td>
		<td><?php		
		data($description);
		?></td>
		<td><?php		
		data($category);
		?></td>
		<td><?php		
		data(image_link);
		?></td>
	</tr>
</table>	
</body>
</html>

Based on your example i'm unsure how i would retrieve the data bearing in mind i may need just one of the variables not all four and so forth so would need to be able to display/pull them individually 

So would need to be able to call description / category / image_link / title seperately if needed

Edited by experience40
Link to comment
Share on other sites

Quote

I think the way you've suggested is to run the 'html' code directly from the backend php file via echo ...

You'll also note that my function returns a string which is then displayed by echo().  It's a subtle distinction but means that you can send that string result anywhere you want. 

 

Quote

Based on your example i'm unsure how i would retrieve the data bearing in mind i may need just one of the variables not all four and so forth so would need to be able to display/pull them individually

You should retrieve the data up front and pass it to the templating "system", not the other way around. 
Having the templating "system" reaching out to get its own data whenever it needs it will cripple the application.  You could wind up running dozens (or hundreds!) of queries where one would do just as well. 

 

The principle I'm trying to demonstrate here is that data retrieval (from the database) and creation of content (based on a "template") need to be separate functions and you use PHP code to get the data you want from one into the other. 

The "front-end" must be parameterised to take the data you pass it and apply those values to the template HTML. 

The "back-end" must retrieve the required data and put it in a form that you can pass to the "front-end".
In my example, I used individual parameters, mainly for clarity.  It sounds like you'd be better off passing an array, with key-value pairs containing the data.  This allows the templating "system" to take whichever values it wants and use them and "ignore" any that it doesn't need.  (This is the classic "XML" principle; a great idea, as long as you don't have to worry about security!).

Regards, 
   Phill W. 

  • Like 1
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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.