Jump to content

Using an include in defining a variable


Jaymoon

Recommended Posts

I was wondering if it was possible to use an include when defining a variable.

 

This is the code in the form of how I want it to behave...

 

//Contents of color.php

Green

 

// Contents of index.php

$color = include('color.php');

<p>My favorite color is:</p>
<p><? $color ?></p>

 

So how would I define the contents of color.php as the $color variable in index.php?  :shrug:

Link to comment
Share on other sites

Probably want to do something like this

 

Inside color.php

 

$color = "blue";

 

inside your index.php

 

include "color.php";

echo "$color";

 

This is really dirty but is the jist

 

Well I thought of that, but I'm not sure if that will work.  Without getting too complicated in explaining...  color.php needs to only be a string of text because it will be inserted by a form drop down box.

 

So on edit.php, there will be a drop down box with a list of colors.  The user chooses Green, and when they hit save, this live-edit PHP code I have writes 'Green' to color.php.

 

Then of course index.php will need to display the color.php contents.

Link to comment
Share on other sites

If I understand you correctly your trying something a little more complicated than you first described.  This would require cookies, sessions, or a database of some sort.  You could do this pretty easy with cookies if you don't want to complicated a system.  If you want some real help for your problem I suggest you get as complicated as you can in your explanation of your situation.

Link to comment
Share on other sites

I get your problem.  8)

 

You'll have to use something such as this. include() would basically be the same as shoving that color into the form and not have it parse as a variable.

 

$color = get_file_contents('./color.php');

And you're set!

Link to comment
Share on other sites

$color = get_file_contents('./color.php');

 

That looks like exactly what I need, however it doesn't seem to be parsing.

 

Fatal error: Call to undefined function get_file_contents() in D:\Web\college-picks\vote\week-01.php on line 9

 

Here is the actual code I am using:

 

// contents of index.php
8   <?php
9   $awayfull01 = get_file_contents('./vote/week-01/01-away-full.php');
10   ?>
11
12   <p><img src="images/team/<? $awayfull01 ?>.jpg" /></p>

 

Right now the contents of '01-away-full.php' is just 'Stanford'.

 

So I would like the page to render:

<p><img src="images/team/Stanford.jpg" /></p>

 

Hopefully that explains it a little better?  I am using PHP 5.2.9 on Windows if that matters any.

Link to comment
Share on other sites

Whoops, misworded the function. It's file_get_contents().

 

// contents of index.php
<?php
$awayfull01 = file_get_contents('./vote/week-01/01-away-full.php');
?>

<p><img src="images/team/<?php echo $awayfull01; ?>.jpg" /></p>

Link to comment
Share on other sites

Whoops, misworded the function. It's file_get_contents().

 

// contents of index.php
<?php
$awayfull01 = file_get_contents('./vote/week-01/01-away-full.php');
?>

<p><img src="images/team/<?php echo $awayfull01; ?>.jpg" /></p>

 

Ah hah!  Thank you very much oni-kun, that works perfect!

 

:D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.