SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Input info from PHP file

    I'll try my best to explain what I'm trying to do since I don't know the proper terms!

    I'm creating a template for some pages, and the only thing I want to change are a couple words and an image. but what I would like to do is have a php file where it will have all the information for the site to display what it is suppose to.

    For example:

    Say I am creating a website about a blue, red, and green, car...Instead of saying blue, red, green i would just put $color and then on the page I would point to a PHP file with something like $color="green" in it.. that way I can just change the php info and it will replace all the $color on the page with green. Same thing with image, but instead I would put $image on the page and $image="/images/greencar.jpg" or something similar in the PHP file.

    Does anyone know how I can do this?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    template.php:
    PHP Code:
    This is my great website template.<br /><br />

    It is about <?php echo $color?> cars.<br /><br />

    This is an example of a <?php echo $color?> car:<br /><br />

    <img src="<?php echo $image?>" />
    green-car.php:
    PHP Code:
    $color "green";
    $image "/images/greencar.jpg";
    include(
    "template.php"); 
    blue-car.php:
    PHP Code:
    $color "blue";
    $image "/images/bluecar.jpg";
    include(
    "template.php"); 

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    template.php:
    PHP Code:
    This is my great website template.<br /><br />

    It is about <?php echo $color?> cars.<br /><br />

    This is an example of a <?php echo $color?> car:<br /><br />

    <img src="<?php echo $image?>" />
    green-car.php:
    PHP Code:
    $color "green";
    $image "/images/greencar.jpg";
    include(
    "template.php"); 
    blue-car.php:
    PHP Code:
    $color "blue";
    $image "/images/bluecar.jpg";
    include(
    "template.php"); 
    thanks alot! 1 thing I'm a little confused about is the php include.. wouldnt I put that in the template.php or no?

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Calling include() is equivalent to pasting the contents of the included file at that point in the code.

    Imagine the contents of template.php appearing right there.

    $color and $image are properly defined when they get echo'd out later.

    You can do it the other way around, your "template" just becomes a controller instead of a template, since I assume you'd be adding some kind of logic to decide whether to include the green file or the blue file, etc.

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I see and if for some reason I want to just use everything in 1 file and change the values on the page could I just put
    PHP Code:
    $color "blue";
    $image "/images/bluecar.jpg"
    at the top of the page and just change the values on the actual page?

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, of course (remember the equivalence to copying and pasting we talked about 5 minutes ago?) but then you have to edit every page to change the design, instead of just one file.

  7. #7
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello again! I'm working with my file, and I'm trying to have my pages get the values from main web page.. that way I don't have to have the values in a seperate file.

    right now my www.site.com (index.php in public_html) has the following PHP at the top above the doc type
    PHP Code:
    <?php
    $topic 
    "topic";
    $title "site title";
    ?>
    I'm trying to display that info on www.site.com/page which also has an index.php in it. so in my www.site.com/page I have
    PHP Code:
    <?php
    include("../index.php"); 
    ?>
    Which kinda works.. but it copies the ENTIRE page.. is there a way I can just take the
    PHP Code:
    <?php
    $topic 
    "topic";
    $title "site title";
    ?>
    from the page?

  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, put that code in a separate file which you include in both places

  9. #9
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks!

  10. #10
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is line 8 of my php
    PHP Code:
    $img "<img src="/test/dog.jpg" alt="dog" width="450" height="323"/>"
    everything worked before I added the img src.. and this is the error I'm getting

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home2/site/public_html/test/info.php on line 8
    what am I doing wrong?

  11. #11
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that the string you're trying to make there isn't really behaving as such due to the placement of your quotes (or how you're using them).

    Strings require single quotes where as strings containing variables and such can all be encapsulated in double quotes. The link below will better explain what it is I'm trying to convey here:

    http://us2.php.net/language.types.string

  12. #12
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Wolf_22 View Post
    The problem is that the string you're trying to make there isn't really behaving as such due to the placement of your quotes (or how you're using them).

    Strings require single quotes where as strings containing variables and such can all be encapsulated in double quotes. The link below will better explain what it is I'm trying to convey here:

    http://us2.php.net/language.types.string
    I don't understand.. what do I have to change?

  13. #13
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,711
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The variable you made ($img) is basically a string. Because of this, it means that it needs to be placed within single quotes instead of double quotes--which in turn allows you to use those double quotes for the markup attributes within the image element / tag. For more information on all this, though, I would encourage you to follow that link I provided you with so that you can have a more in-depth understanding of how this all works. It can get pretty hairy sometimes when it comes to escaping things in PHP.

    This is what you had:
    Code:
    $img = "<img src="/test/dog.jpg" alt="dog" width="450" height="323"/>";
    This is what you need to have:
    Code:
    $img = '<img src="/test/dog.jpg" alt="dog" width="450" height="323"/>';
    Though, I would advise you to append the unit type identifier after your width and height attribute values to provide better support for standards, accessibility, and all the other wonderful in-crowd things we all see people blog about...

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't explain it as "it needs to be in single quotes because it's a string". Text contained in double quotes is a string as well.

    The problem with your code, enwise, is that when you have double quotes inside a string enclosed by double quotes, how does PHP know which ones are the end of the value you're assigning to the variable, and which are just part of that value?

    Code:
    $img = "<img src="/test/dog.jpg" alt="dog" width="450" height="323"/>";
           ^         ^ 
                     matches the double quotes starting the string, so this is the end, right?
    Reading the PHP manual page on strings is a good idea. You have a few ways to deal with this. Enclosing in single quotes so that there's no ambiguity that the double quotes inside are part of the string or not is one way. Escaping the double quotes inside is another.

    PHP Code:
    $img "<img src=\"/test/dog.jpg\" alt=\"dog\" width=\"450\" height=\"323\"/>"

  15. #15
    SitePoint Zealot
    Join Date
    Sep 2008
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dan Grossman View Post
    I wouldn't explain it as "it needs to be in single quotes because it's a string". Text contained in double quotes is a string as well.

    The problem with your code, enwise, is that when you have double quotes inside a string enclosed by double quotes, how does PHP know which ones are the end of the value you're assigning to the variable, and which are just part of that value?

    Code:
    $img = "<img src="/test/dog.jpg" alt="dog" width="450" height="323"/>";
           ^         ^ 
                     matches the double quotes starting the string, so this is the end, right?
    Reading the PHP manual page on strings is a good idea. You have a few ways to deal with this. Enclosing in single quotes so that there's no ambiguity that the double quotes inside are part of the string or not is one way. Escaping the double quotes inside is another.

    PHP Code:
    $img "<img src=\"/test/dog.jpg\" alt=\"dog\" width=\"450\" height=\"323\"/>"

    Thanks for all the help! Is there a reason to choose 1 of these 2 methods over the other? Will the other way cause any problems? will this way?

  16. #16
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The primary difference between putting single quotes and double quotes around a string value is how the PHP interpreter deals with what's inside.

    If it's enclosed in single quotes, it's treated as a literal string and no more.

    If it's enclosed in double quotes, then any variables inside the string get parsed and replaced.

    Examples:
    PHP Code:
    $color 'blue';
    $img '<img src="images/$color.jpg" />';
    echo 
    $img
    Output:
    HTML Code:
    <img src="images/$color.jpg" />
    PHP Code:
    $color 'blue';
    $img "<img src=\"images/$color.jpg\" />";
    echo 
    $img
    Output:
    HTML Code:
    <img src="images/blue.jpg" />
    But I think concatenating the literal strings with variables is easier to read and less prone to human error:
    PHP Code:
    $color 'blue';
    $img '<img src="images/' $color '.jpg" />';
    echo 
    $color
    Output:
    HTML Code:
    <img src="images/blue.jpg" />


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •