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.

Robert Sheppard
  • 359
  • 1
  • 2
  • 12

5 Answers5

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:

Community
  • 1
  • 1
Ben
  • 8,894
  • 7
  • 44
  • 80
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
        }
    }
}

?>
Halayem Anis
  • 7,654
  • 2
  • 25
  • 45
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);
Rajveer gangwar
  • 715
  • 4
  • 14
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);
Rajveer gangwar
  • 715
  • 4
  • 14
-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.

Yuri Blanc
  • 636
  • 10
  • 24