sandbudd Posted May 25, 2011 Share Posted May 25, 2011 What I am trying to do is pull the content of another page which works fine and then do echo statements. It does display the content of the page but not the echo statements. outside page lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum <?php echo $stateinsert ?> lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum <?php echo $cityinsert ?> <?php echo $stateinsert ?> lorem ipsum lorem ipsum lorem ipsum lorem ipsum <?php echo $countyinsert ?> Lorum Ipsom Lorum Ipsom Lorum Ipsom </p> <?php $cityinsert = "Auburn"; $stateinsert = "Alabama"; $countyinsert = "Jefferson"; $table = "auburnAL"; ?> <?php $d = file_get_contents("http://mysite.com/includes/leftcopy.php"); echo ($d); ?> It displays the lorem ipsum but won't display the echo statements. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/ Share on other sites More sharing options...
sandbudd Posted May 25, 2011 Author Share Posted May 25, 2011 should mention that the it is pulling from another website Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220312 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 Those echo statements will be executed on the external page, not in your script. So all the echoed variables are blank when the echo executes. Instead you might want to make them placeholders like "%STATEINSERT%" and do a str_replace() on $d after file_get_contents(). Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220316 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 btherl could you give me an example please. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220318 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 $d = str_replace("%STATEINSERT%", $stateinsert, $d); And then in your external html you would use %STATEINSERT% instead of <?php echo $stateinsert; ?> Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220325 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 btherl that worked perfect if you have time could you explain to me why that worked and what exactly I did so I know in the future? And thank you so much for you help! Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220334 Share on other sites More sharing options...
jcbones Posted May 26, 2011 Share Posted May 26, 2011 It is quite simple, the PHP script you are calling resides on a different server. When you call a file using 'file_get_contents()', and you specify the file path with 'http://', the file gets parsed by the server BEFORE delivery of the contents. He had you to place 'placeholders' in your script where you wanted the data to show. Placeholders are unique string patterns that isn't normally used in a page layout. You can make them anything you want, but they must be unique. He then had you to do a simple string_replace function to find you placeholders, and replace them with your desired variables. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220349 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 jcbones explained it well. If both scripts are on the same server you could consider using sessions or include() instead of file_get_contents(). Sessions allow you to share data between scripts, and include() will allow leftcopy.php to access the variables you have set in your script. The reason is that include() runs in the same copy of php, but file_get_contents() starts a new copy of php to run leftcopy.php in, and it doesn't know about the variables you set. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220355 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 Hey everyone thanks for the help but I need to know how to continue the echo statement... this is what I tried but does not work. <?php $cityinsert = "Auburn"; $stateinsert = "Alabama"; $countyinsert = "Jefferson"; $table = "auburnAL"; ?> <? $d = file_get_contents("http://mysite/includes/leftcopy.php"); echo $d = str_replace("%STATEINSERT%", $stateinsert, $d); echo $d = str_replace("%CITYINSERT%", $cityinsert, $d); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220376 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 Tried this with no luck <?php $d = file_get_contents("http://restaurantsanddiners.com/includes/leftcopy.php"); echo $d = str_replace("%STATEINSERT%", $stateinsert, $d); echo $d .= str_replace("%CITYINSERT%", $cityinsert, $d); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220378 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 any help guys I have tried several combinations and can't it to work. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220391 Share on other sites More sharing options...
btherl Posted May 26, 2011 Share Posted May 26, 2011 It should look like this: $d = str_replace("%STATEINSERT%", $stateinsert, $d); $d = str_replace("%CITYINSERT%", $cityinsert, $d); echo $d; You can add as many of the str_replace() lines as you want. Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220392 Share on other sites More sharing options...
sandbudd Posted May 26, 2011 Author Share Posted May 26, 2011 I tried that but it didn't work, tried again and it did...thanks for the help must of missed typed something Quote Link to comment https://forums.phpfreaks.com/topic/237482-not-even-sure-this-is-possible-with-a-file_get_contents/#findComment-1220398 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.