experience40 Posted November 7, 2023 Share Posted November 7, 2023 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']; ... } Quote Link to comment https://forums.phpfreaks.com/topic/317423-call-data-variables-seperately-from-same-function-to-add-to-html-layout-template/ Share on other sites More sharing options...
Phi11W Posted November 7, 2023 Share Posted November 7, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/317423-call-data-variables-seperately-from-same-function-to-add-to-html-layout-template/#findComment-1612776 Share on other sites More sharing options...
experience40 Posted November 7, 2023 Author Share Posted November 7, 2023 (edited) 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 November 7, 2023 by experience40 Quote Link to comment https://forums.phpfreaks.com/topic/317423-call-data-variables-seperately-from-same-function-to-add-to-html-layout-template/#findComment-1612778 Share on other sites More sharing options...
Phi11W Posted November 7, 2023 Share Posted November 7, 2023 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317423-call-data-variables-seperately-from-same-function-to-add-to-html-layout-template/#findComment-1612779 Share on other sites More sharing options...
experience40 Posted November 8, 2023 Author Share Posted November 8, 2023 Thanks for the advice Phi11W I think your suggestion of using an array with data key-value pairs sounds good and hopefully will fit my requirements Quote Link to comment https://forums.phpfreaks.com/topic/317423-call-data-variables-seperately-from-same-function-to-add-to-html-layout-template/#findComment-1612791 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.