php call variable and put back on while loop

88 Views Asked by At

sorry for my last question where i try put some live code with ob_start buffer content is not helping me to solve my problem because buffer content just collects output text, it doesn't execute any code. thanks @akrys for your advices

what i want is to put code into while looping like this

$sql = $conn->query("SELECT * FROM `users`"); 
$var = $row['full_name'];
include('test.php');

after i call test.php contain while code like:

while($row = $sql->fetch_array()) {
    echo $var;
}

everything is work if i replace $var with $row['full_name'];

but i get the name of row field from some script on index.php so i should access that file first then i call portable file contain query to fetch_array on test.php

how to make it work when i put it back with $var contain variable field name

thank you very much for your attention guys

2

There are 2 best solutions below

1
MEHRDAD DASHTI On

you should to include before your code page

test.php

<?php 
$someVariable = 'hello'; // the variable only can access in here
?>

<?php 
include('test.php');
ob_start();
echo "some text with call variable $someVariable";
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; // 
?>

of course you can use define too

page test.php

<?php 
define( "SOMEVARIABLE", hello ); 
?>

<?php 
include('test.php');
ob_start();
echo "some text with call variable ".SOMEVARIABLE;
echo "other stuff";
$tdcol1_val = ob_get_contents(); ob_clean();
echo $tdcol1_val; // 
?>
1
Amir Amini On

you can use:

define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."

for more help, use the link below:

enter link description here