heisje Posted June 22, 2006 Share Posted June 22, 2006 .have been trying all sorts of ideas to solve this, but nothing has worked - so here I come:for this simple script:[code]<?$var1 = "one";$var2 = "two";$var3 = "three";$text = "$var1, $var3";echo $text?>[/code]output is: one, threewhat is wanted:to get the part "$var1, $var3" from an external text file, and have the script output determined according to what is specified in the text file, in this case the text file contents would only be:$var1, $var3but could also be $var3, $var2and then the output would be: three, twothanks in advance for any ideas.heisje. Quote Link to comment Share on other sites More sharing options...
jvrothjr Posted June 22, 2006 Share Posted June 22, 2006 <?$var1 = "one";$var2 = "two";$var3 = "three";$text = $var1.",".$var3;echo $text?> Quote Link to comment Share on other sites More sharing options...
heisje Posted June 22, 2006 Author Share Posted June 22, 2006 .many thanks, but I do not get the point:where is the external text file with the variables?and how are they inserted in the script?maybe my question was not very clear:there exists an external text file specifying which variables shall determine $text:for example:text.txt with contents $var1, $var3tell the script which variables to use for $text.need to read the text file and insert these variables, and get the script to output the text: one, three this is the question.thanks,heisje.. Quote Link to comment Share on other sites More sharing options...
heisje Posted June 22, 2006 Author Share Posted June 22, 2006 .I shall try to describe the request in greater detail:Suppose that I have 10,000 php pages that already contain the script:[code]<?$var1 = "one";$var2 = "two";$var3 = "three";$text = "$var1, $var3";echo $text?>[/code]output is: one, threefor all 10,000 pagesNow, suppose that I want to have the ability to change the output of all those 10,000 pages all at the same time by setting the value of $text.One time I may want $text = "$var1, $var3";Another time I may want $text = "$var3, $var2";etc.So, if I have an external text.txt file which I can change to $var1, $var3 - or var3, $var2 - or anything I want, then all the 10,000 pages will print the same thing accordingly.It would be fine if the following worked, but of course it does not:[code]<?$var1 = "one";$var2 = "two";$var3 = "three";$text = file_get_contents('text.txt');echo $text?>[/code]to insert ex. $var1, $var3 and specify$text = "$var1, $var3";but this just prints: $var1, $var3and not :one, threeas intended.I hope this is a bit more clear.thanks,heisje. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 22, 2006 Share Posted June 22, 2006 You want use the [a href=\"http://www.php.net/include/\" target=\"_blank\"]include()[/a] statement.The include() statement will take the contents of the file and process the lines as if they were part of the source. Ken Quote Link to comment Share on other sites More sharing options...
heisje Posted June 22, 2006 Author Share Posted June 22, 2006 .many thanks, kenI tried<?$var1 = "one";$var2 = "two";$var3 = "three";$text = include('text.txt');echo $text?>with contents in text.txt$var1, $var3but the output is:$var1, $var31and not:one, threeas expected.certainly I am doing something wrong - because apparently you did not mean what I have done by suggesting the use of include()clarification?thanks,heisje. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 22, 2006 Share Posted June 22, 2006 You want the file text.txt should contain:[code]<?php$var1 = "one";$var2 = "two";$var3 = "three";?>[/code]Your script would be:[code]<?phpinclude('text.txt');echo "$var1, $var3";?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
heisje Posted June 23, 2006 Author Share Posted June 23, 2006 .many thanks ken, I see what you meanunfortunately this solution will not work because the $var in my example can be defined only within the script (and are actually different from page to page) - while the text.txt file is a "control" file only, for the 10,000 pages, intended to control how many times and which $var are included in $text.for example:$text = '$var1 $var2 $var1 $var3';or$text = '$var1 $var1 $var1';or$text = '$var3';any ideas?thanks,heisje. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted June 23, 2006 Share Posted June 23, 2006 So you want the include file to tell you which variables to print in the scripts. Here is one way that works.This is the contents of the include file named test_inc.php:[code]<?php $print_these = array($var1,$var3,$var5);?>[/code]Here is the test script:[code]<?php$var1 = 'This is var 1';$var2 = 'This is var 2';$var3 = 'This is var 3';$var5 = 'This is var 5';include('test_inc.php');$tmp = array();foreach($print_these as $var) $tmp[] = $var; echo implode(', ',$tmp);?>[/code]You put the variable names you want to print in the array and use the foreach statement to get the values to print.When you change the variables in the array, the output will change. You have to include the file after the variables are defined or this method won't work.Ken Quote Link to comment Share on other sites More sharing options...
heisje Posted June 23, 2006 Author Share Posted June 23, 2006 .ken, [b]many thanks indeed[/b]this is exactly what is needed.a simple and nice solution !! - and wonder now why I did not come up with this myself after trying so long?well . . . . .I hope this will also be of help to somebody else who will need to do the same thing in the future.(I searched extensively in past posts and could not find something similar before).thanks again to [b]ken[/b],and thanks also go to [b]jvrothjr[/b] and to [b]barry[/b] (barand)who contributed alternative ideas.all the best to this wonderful community, dedicated to solving problems.heisje. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.