0

I have the required ID variable from this, (there are 1-6 possible values):

$new_product['ID'] =$row[2];

What I need is to echo a separate 'php-include' depending on this variable, so something like:

<?php include 'includes/size/prod/echo $row[2].php'; ?>

which would display, includes/size/prod/1.php, includes/size/prod/2.php etc

I don't understand how to phrase the 'echo' within the php.

4
  • <?php include 'includes/size/prod/' . $row[2] . '.php'; ?> Commented Dec 1, 2015 at 10:37
  • try include 'includes/size/prod/'.$row[2].php Commented Dec 1, 2015 at 10:37
  • Possible duplicate of PHP include with a variable in the path Commented Dec 1, 2015 at 10:40
  • Thanks Naruto worked perfectly. Commented Dec 1, 2015 at 10:48

5 Answers 5

0

There are a few ways:

//Concatenate array value in double quotes:
<?php include "includes/size/prod/{$row[2]}.php"; ?>

//Concatenate array value outside of quotes:
<?php include "includes/size/prod/".$row[2].".php"; ?>
//or, using single quotes:
<?php include 'includes/size/prod/'.$row[2].'.php'; ?>

//Concatenate variable (not array value) in double quotes:
<?php $page = $row[2]; include "includes/size/prod/$page.php"; ?>

See:

Sign up to request clarification or add additional context in comments.

Comments

0

It's very dangerous to include your PHP files with this technic !!
You must prevent this by doing at least a control of included files are PHP

Now to respond to your question:

<?php 
// ? $row2 contains more than 1 php file to include ?
//   they are seperated by comma ?

$phpToInclude = NULL;
define(TEMPLATE_INCLUDE, 'includes/size/prod/');

if (isset($row[2])) {
    $phpToInclude = explode($row[2], ',');
}

if (!is_null($phpToInclude)) {
    foreach($phpToInclude as $f) {
        $include = sprintf(TEMPLATE_INCLUDE . '%s', $f);
        if (is_file($include)) {
            // add validator here !!
            include ($include);
        }
        else {
            // file not exist, log your error
        }
    }
}

?>

Comments

0

dear use following working code

$row[2]         =   '';
$file_name      =   !empty($row[2])?$row[2]:'default';
$include_file   =  "includes/size/prod/".$file_name.".php";
include($include_file);

Comments

0

Things get evaluated in double quotes but not in single:

$var = "rajveer gangwar";
echo '$var is musician'; // $s is musician.
echo "$var is musician."; //rajveer gangwar is musician.

So better to use double quotes

Example:

// Get your dynamic file name in a single variable.
$file_name      =   !empty($row[0]) ? $row[0] : "default_file";
$include_file   =  "includes/size/prod/$file_name.php";
include($include_file);

Comments

-1

You can use the dots for separating a string: So for instance:

$path = 'includes/size/prod/'.$row[2].'.php'; 

include '$path';

Or you can put it in a variable:

$path = $row[2];
include 'includes/size/prod/$path.php'; 

Php is able to evaluate a variable within a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.