0

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

7
  • 1
    You should define $someVariable before using it in a string. An output buffer just collects output text, it does't execute any code.
    – akrys
    Commented May 16, 2021 at 18:12
  • @akrys can you give a little example, because maybe I am wrong with the example i gave there,, in real case it's different.. but the first page i access is index.php and test.php only run an code when i need it..
    – Denis
    Commented May 16, 2021 at 18:21
  • and what i should use better than buffer to put content, thanks for you tell me now i knew buffer only run output not code
    – Denis
    Commented May 16, 2021 at 18:25
  • If your code is different, then show something that is equivalent to it.
    – akrys
    Commented May 16, 2021 at 18:26
  • @akrys i've checked my page process and it the same as what i mean on my question.. can you tell me what trick to replace ob buffer that can put the code like variables
    – Denis
    Commented May 16, 2021 at 18:31

2 Answers 2

0

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
  • sorry guys.. can i update this question ? i found that buffer content really not help me for this because buffer only work on string variable, i mean at my real code is working with sql while loop i want put $row query back on while loop.. so wait i try to update this question sorry...
    – Denis
    Commented May 16, 2021 at 20:40
0

you can use:

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

for more help, use the link below:

enter link description here

1
  • sorry guys.. can i update this question ? i found that buffer content really not help me for this because buffer only work on string variable, i mean at my real code is working with sql while loop i want put $row query back on while loop.. so wait i try to update this question sorry...
    – Denis
    Commented May 16, 2021 at 20:40

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.