Log in

View Full Version : Include file as a variable



kuau
03-21-2008, 01:53 AM
I have a template file that uses two different versions of the right colum so I use this code for the right column:


<div id="right-hm">
<?=$rightcol;?>
</div>

I have created 2 include files called right-hm.html and right-res.html which are on the include path in php.ini. What I would like to do is set the value of $rightcol in the file that runs the template like this: $rightcol = 'right-hm.html'; in which case the above would be:


<?include($rightcol);?>

But it does not work! The only way I can get the right column to appear is to load the value of $rightcol from a mySQL database. How do you write a command to get to to load from the file instead of the database? I even tried setting $rightcol = 'include("right-hm.html")'; It didn't work.

What am I doing wrong? Thanks, erin

Nile
03-21-2008, 02:42 AM
Instead of using short starts, use the standard.

kuau
03-21-2008, 02:51 AM
What are short starts and what are standards? I have no idea what you are saying. Sorry.

Nile
03-21-2008, 02:54 AM
Short:


<?

?>

Standard:


<?php

?>

kuau
03-21-2008, 10:39 PM
Dear Nile: Thanks for helping me with this. It is not the php short tags because I have tons of them throughout the site. But I know you know how to solve this problem because you know php so well and helped me previously with a similar issue. If you would be so kind, I need you to show me how to say this:

<?php include('right-hm.html'); ?>

but where the filename is a variable. I've tried the following:

<?php include('$rightcol'); ?> and <?php include($rightcol); ?>

Setting $rightcol = 'include("right-hm.html");';

then in template <?php echo $rightcol; ?> ... this one prints the literal 'include("right-hm.html") on the screen.

So I tried leaving out the outer quotes and it did print the column, but at the very top of the screen above the page. Not sure what else to try. Any ides? Thanks, erin.

Nile
03-21-2008, 10:44 PM
So this doesn't work?:


<?php
$rightcol = "include('right-hm.html')";
?>
Then


<?php echo $rightcol; ?>

If the above works it was because of your ; after the include.

thetestingsite
03-21-2008, 10:51 PM
instead of using include, simply use file_get_contents (http://php.net/file_get_contents) like so:



<?php
$content = file_get_contents('filename_here.txt');
?>


and then:



<?php echo $content; ?>


Added Later: You cannot use include in a variable as that is not what the include function was made for and will cause strange things to happen to your php code. Instead, you need to read the file using file accessing functions (such as: file, file_get_contents, fgets, and others).
Hope this helps.

kuau
03-21-2008, 10:54 PM
That doesn't work either. It printed this on the screen where the column would be...

include('right-hm.html')

Nile
03-21-2008, 10:54 PM
Oh, sorry. Try this:
<?php $rightcol; ?>

kuau
03-21-2008, 11:14 PM
I tried the file_get_contents and removing the echo and both had the same result. The column did not appear and in the code there was nothing at all.

Maybe this is just not possible. I really appreciate your willingness to help me.

Mahalo, erin :)

thetestingsite
03-21-2008, 11:15 PM
What are the contents of the file that you are trying to include? Also, what version of PHP do you have as functions vary for each branch.

To see a working example of my code, go to http://thetestingsite.net/test.php. You can check out the contents of test.html and see what is in that file and that it is being included. To see the php, go to test.phps.

Hope this helps.

kuau
03-23-2008, 02:16 AM
Here are the contents of the right-hm.html file. It is just a list of items for the righthand column of the page, but it appears on 5 pages so I use an include:


&nbsp;&nbsp;<a class="agency" href="/html/rates.html#Alamo"><img src="/img/gr/logo_alamo.gif" alt="Click for Alamo Rates" width="31" height="19" /></a>
<a class="agency" href="/html/rates.html#Avis"><img src="/img/gr/logo_avis.gif" alt="Click for Avis rates" width="38" height="19" /></a>
<a class="agency" href="/html/rates.html#Budget"><img src="/img/gr/logo_budget.gif" alt="Click for Budget rates" width="34" height="19" /></a>
<a class="agency" href="/html/rates.html#Dollar"><img src="/img/gr/logo_dollar.gif" alt="Click for Dollar rates" width="42" height="19" /></a>
<a class="agency" href="/html/rates.html#Thrifty"><img src="/img/gr/logo_thrifty.gif" alt="Click for Thrifty rates" width="39" height="19" /></a>
<ul>
<li><a href="/html/faq.html#bargain"><b>Bargain rates</b></a> for late model cars through major rental car companies.</li>
<li><a href="/html/faq.html#guaranteed"><b>Guaranteed</b></a> reservation, no credit card&nbsp;required.</li>
<li><a href="/html/faq.html#reserve"><b>Reservations</b></a> are subject to availability of the chosen car type at the pick-up location.</li>
<li><a href="/html/faq.html#airport"><b>Airport</b></a> pick up for all cars.</li>
<li><a href="/html/faq.html#Driver"><b>Additional</b></a> driver is free!!</li>
<li>For 12-Pax Van &amp; <a href="/html/faq.html#suv"><b>Fullsize SUV</b></a> rates please inquire by
<a href="mailto:info@carrentalhawaii.com?subject=12PaxVan/FullsizeSUV%20Rental%20Inquiry">email</a>.</li>
<li>For Molokai rates please inquire by <a href="mailto:info@carrentalhawaii.com?subject=Molokai Rental Inquiry"><b>email</b></a>.</li>
<li>Special deal for <a href="/html/canadian.html"><b>Canadian customers</b></a>. <img src="/img/gr/cdn-flag-sm.jpg" alt="Canadian Flag" width="30" height="18"></li>
<li><b>Cruise Ship Passengers:</b> <a href="/html/cruise-ship.html"><b>click here</b></a> for special instructions.</li>
</ul>

To be honest, I can't afford to spend any more time on this issue as I have other work and I have it working by using the database. So I thank you very much for your efforts and am letting you abandon the quest.

But you can perhaps help me with this please. What is the php code to add a BCC so that it will blind copy the email to another address without displaying the email address?


$subject = "Generic Website Inquiry";
$mailto = "Ken <kenw@gmail.com>";
mail($mailto,$subject,$body,"From:$sender");

Thanks, erin :)

thetestingsite
03-23-2008, 02:00 PM
Here are the contents of the right-hm.html file. It is just a list of items for the righthand column of the page, but it appears on 5 pages so I use an include:


&nbsp;&nbsp;<a class="agency" href="/html/rates.html#Alamo"><img src="/img/gr/logo_alamo.gif" alt="Click for Alamo Rates" width="31" height="19" /></a>
<a class="agency" href="/html/rates.html#Avis"><img src="/img/gr/logo_avis.gif" alt="Click for Avis rates" width="38" height="19" /></a>
<a class="agency" href="/html/rates.html#Budget"><img src="/img/gr/logo_budget.gif" alt="Click for Budget rates" width="34" height="19" /></a>
<a class="agency" href="/html/rates.html#Dollar"><img src="/img/gr/logo_dollar.gif" alt="Click for Dollar rates" width="42" height="19" /></a>
<a class="agency" href="/html/rates.html#Thrifty"><img src="/img/gr/logo_thrifty.gif" alt="Click for Thrifty rates" width="39" height="19" /></a>
<ul>
<li><a href="/html/faq.html#bargain"><b>Bargain rates</b></a> for late model cars through major rental car companies.</li>
<li><a href="/html/faq.html#guaranteed"><b>Guaranteed</b></a> reservation, no credit card&nbsp;required.</li>
<li><a href="/html/faq.html#reserve"><b>Reservations</b></a> are subject to availability of the chosen car type at the pick-up location.</li>
<li><a href="/html/faq.html#airport"><b>Airport</b></a> pick up for all cars.</li>
<li><a href="/html/faq.html#Driver"><b>Additional</b></a> driver is free!!</li>
<li>For 12-Pax Van &amp; <a href="/html/faq.html#suv"><b>Fullsize SUV</b></a> rates please inquire by
<a href="mailto:info@carrentalhawaii.com?subject=12PaxVan/FullsizeSUV%20Rental%20Inquiry">email</a>.</li>
<li>For Molokai rates please inquire by <a href="mailto:info@carrentalhawaii.com?subject=Molokai Rental Inquiry"><b>email</b></a>.</li>
<li>Special deal for <a href="/html/canadian.html"><b>Canadian customers</b></a>. <img src="/img/gr/cdn-flag-sm.jpg" alt="Canadian Flag" width="30" height="18"></li>
<li><b>Cruise Ship Passengers:</b> <a href="/html/cruise-ship.html"><b>click here</b></a> for special instructions.</li>
</ul>

To be honest, I can't afford to spend any more time on this issue as I have other work and I have it working by using the database. So I thank you very much for your efforts and am letting you abandon the quest.


Well, as you can see from the example link I posted above, it should work just fine no matter what your HTML code is.



But you can perhaps help me with this please. What is the php code to add a BCC so that it will blind copy the email to another address without displaying the email address?


$subject = "Generic Website Inquiry";
$mailto = "Ken <kenw@gmail.com>";
mail($mailto,$subject,$body,"From:$sender");

Thanks, erin :)

View this code snippet taken from php.net's documentation that shows how to send an HTML email to multiple recipients (the headers also include a BCC address so this should be helpful for you.



<?php
// multiple recipients
$to = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);
?>


Hope this helps.

kuau
03-23-2008, 08:40 PM
I am using php4, because if I set it to php5, the email script does not retain the value of the variables and sends blanks - no idea why.

I just tried doing exactly what you did with:

$rightcol = file_get_contents('right-hm.html');

with this in the template file:

<div id="right-hm">
<?php echo $rightcol; ?>
</div> and the column disappeared again - it makes no sense.

What php version are you using? Now I'll try the BCC part. This is very discouraging. Thanks for your help and persistence. Aloha, erin :)

kuau
03-23-2008, 08:43 PM
Forgot to mention that I was not using html mail. Do you have to use html to use BCC?

thetestingsite
03-23-2008, 09:49 PM
Forgot to mention that I was not using html mail. Do you have to use html to use BCC?

No you do not. I just pulled that off of the php.net documentation. You can just strip out what you don't need and should be good to go. As for the version of php I am using, it is php 5.2.5 as php 4 is about to be non-supported. Anyways, file_get_contents works with versions greater than or equal to PHP 4.3.0 so that should still work for you unless there are other issues with your code.

Hope this helps.

kuau
03-23-2008, 10:57 PM
The version of php is 4.4.7. so I have no idea why that right colum thing doesn't work using your get_file_contents. The server has php5 available as well, which I would like to use, but my email script stops working. Would you mind please looking at the email script (it is only one screen long) and telling me why php5 won't run it? The only things I know about php are from inheriting some old scripts from some guy who had poor programming practices. I swear it's a miracle anything I write works. Your BCC suggestion did work (thanks!) but I don't see why it needs so many quote marks. You'll see in the script the old way and the new way I just added. I'd like to combine them to kind of "zen" the code. I don't want to impose upon your good will, so it's OK if you say no. Thanks, erin :)

thetestingsite
03-24-2008, 12:07 AM
Show me the code and I'll take a look to see what I can do. Also, what actually happens with your script when you switch from php 4 to 5? It could be just a simple issue within your ini file (such as register_globals on in one and not the other, or missing extensions, or something else altogether). In any case, show the code (or PM/IM/email me the code) and I'll see what I can do.

Hope this helps.