bbmak Posted August 30, 2012 Share Posted August 30, 2012 Hi, sorry, if I got a crappy title because I do not know what this called. I want to have a dynamic meta tag with php. I have a shopping site. I try to put each product description on meta tag, however, the php mysql variable is below the </head> tag. Are there anyway to get the bottom variable at the top of the script? I know I can do the query on top of meta name, but I want to know are there any convenience way other people are doing this. <?php <head> <meta name="product_description" content="<?php echo $product_description ?>"></meta> <--------call the variable right here. </head> ... $product_result = $mysqli->query("SELECT..."); while($product_row = $product_result->fetch_array()) { $product_description = $product_row['description']; <-----------declaring right here } Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 30, 2012 Share Posted August 30, 2012 See the recommend method of building a page at this link - http://forums.phpfreaks.com/index.php?topic=364499.msg1726616#msg1726616 Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374031 Share on other sites More sharing options...
Christian F. Posted August 30, 2012 Share Posted August 30, 2012 The best way to do this, is called "seperation of logic/concerns". What it means is that you should move all of the PHP processing above any of the output, so that when you start to output content to the browser you don't need to do any testing, declaration of variables, generation of content or anything like that. Quick example, based upon your code above: <?php // TODO: Add all of the validation and other necessary stuff here. // Fetch the details from the database. // TODO: Add output escaping if query is built dynamically. $product_result = $mysqli->query("SELECT..."); // Define the template you want to use for the finalized output. // Let's say you want to show them in a table, with name, price, amount (in stock), and a description below: $prodTemplate = <<<'Prod_Template' <tr> <td>%s</td> <td>%01.2f EUR</td> <td>%d</td> </tr> <tr> <td colspan="3" class="description">%s</td> </tr> Prod_Template; // Initialize output variable. $prodOutput = ''; while($row = $product_result->fetch_array ()) { $prodOutput .= sprintf ($prodTemplate, htmlspecialchars ($row['name']), $row['price'], $row['stock'], htmlspecialchars ($row['description']); } // Set the contents of the product description meta tag. $prodDescription = 'List of all the products in stock'; // TODO: Whatever else you need to do on this page once you've retrieve all products. ?> <doctype html> <html> <head> <meta name="product_description" content="<?php echo $prodDescription ?>"></meta> </head> <body> <table> <thead> <!-- TODO: Fill in table headers here. --> </thead> <tbody> <?php echo $prodOutput; ?> </tbody> </table> . What it means is that you should move all of the PHP processing above any of the output, so that when you start to output content to the browser you don Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374041 Share on other sites More sharing options...
bbmak Posted August 30, 2012 Author Share Posted August 30, 2012 What happen if your product page is include page? index.php <html> <head> <title></title> </head> <? include $left; include $center; <----product pages right here. include right; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374051 Share on other sites More sharing options...
Mahngiel Posted August 30, 2012 Share Posted August 30, 2012 looks like you need a templating system. Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374055 Share on other sites More sharing options...
Christian F. Posted August 30, 2012 Share Posted August 30, 2012 Then you're mixing concerns, and should refactor your page. Once content has been sent to the client, it is impossible to change it for the server. Kind of like how you can't "unjump" from a plane, if you suddenly remember that you've forgotten your parachute. The most common way of separating concerns is by using templates, and have the PHP code determine what template files/content to send to the browser. PHP can act as this templating system, but you should not use any other PHP code than "echo" inside the template files. Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374060 Share on other sites More sharing options...
PFMaBiSmAd Posted August 30, 2012 Share Posted August 30, 2012 What happen if your product page is include page? If you want a general solution that doesn't have the problem that started this thread, you need to give up on the notion of copy/pasting together a web page using php include statements. Programming does not involve magic or the ability to go back in time. Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374063 Share on other sites More sharing options...
bbmak Posted August 30, 2012 Author Share Posted August 30, 2012 Then you're mixing concerns, and should refactor your page. Once content has been sent to the client, it is impossible to change it for the server. Kind of like how you can't "unjump" from a plane, if you suddenly remember that you've forgotten your parachute. The most common way of separating concerns is by using templates, and have the PHP code determine what template files/content to send to the browser. PHP can act as this templating system, but you should not use any other PHP code than "echo" inside the template files. I am writing php as a hobby, not professional, always curious on programming langs How do you suggest the templates? If I have include $pages in the templates? Quote Link to comment https://forums.phpfreaks.com/topic/267829-call-variable-at-the-top-of-script-before-declaring-them/#findComment-1374068 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.