Jump to content

Call $variable at the top of script, before declaring them?


Recommended Posts

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
}

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

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.

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.

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.