Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Changing Images and Text with PHP

    Ok. Let's say that I was working on simple two page site. The first page will simply have a drop down list that offers the following options:

    The New York Yankees
    The New York Mets
    The New York Knicks
    The Adelaide Crows
    The Collingwood Magpies

    Once the user selects a team and clicks submit, the second page loads and I would like to have the header image of that page reflect which team the user selected and also have an intro paragraph that is specific to the team.

    I already know how to pass a variable from one page to another but I am looking to avoid making five or six separate pages for each of the options. If I wanted to add another page, for team news, record or rosters for instance, I would rather prepare a single page and have the selected team from the first page dictate what information is shown.

    I have been reading up on sessions in PHP and looking at perhaps using XML for my datasource or setting up a table in mySQL to store the data. My question is what would be the best way to approach this problem? I started trying to piece together some pseudocode to help me through it and this is what I came up with:


    PHP Code:
    if ($team == 'yankees') {
        
    // This is the header image I would swap out.
        
    $header_image 'images/yankee_header.jpg';
        
    // This would be the text I would need to swap out. 
        
    $team_text 'The New York Yankees play in the Bronx at Yankee Stadium';
        } else if (
    $team == 'mets'){    
        
    $header_image 'images/mets_header.jpg';
        
    $team_text 'The New York Mets play in Queens at Shea Stadium';
        } else if (
    $team == 'kicks'){
        
    $header_image 'images/knicks_header.jpg';
        
    $team_text 'The New York Knicks play in Manhattan at Madison Square Garden.';
        }
        } else if (
    $team == 'crows'){
        
    $header_image 'images/crows_header.jpg';
        
    $team_text 'The Adelaide Crows play in Australia and thats pretty freakin sweet!';
        }
        } else if (
    $team == 'magpies'){
        
    $header_image 'images/magpies_header.jpg';
        
    $team_text 'The Collingwood Magpies play in Australia and they are my favorite team.';
        } 

    I was even looking at using Dreamweaver's templates to help me in streamlining the pages as well. I was thinking I could place the sorting script in the template and the editable regions would be where I could call the variables for the specified team.
    If using mySQL is the best way to approach storing the images, how do I load them into the tables. The only information that I found on the web thus far had to do with dynamically creating graphs using PHP (cool in and of itself but not what I was looking for...)
    If XML is the way to go, what are the steps involved in pulling data in using PHP? Most of my PHP work thusfar has involved mail forms and simple posting to tables in mySQL.
    You guys are the best of the best so any suggestions would be greatly appreciated.

    Go Magpies!
    Last edited by thepocketgeek; 02-08-2006 at 08:57 PM.

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    MySQL is not the best approach for storing images , though it is the best way to store your data, however that still helps you with your images... e.g, with a database table teams ...

    Code:
    id |   team_name |        team_info
    1      'yankees'       'the yankees ....etc'  
    2      'mets'           'mets are .. etc'
    then given the team id (which you can pass from your select...)
    PHP Code:
    <?php
    $q 
    =mysql_query("SELECT * FROM teams WHERE id={$_POST['id']}");
    $row mysql_fetch_assoc($q);
    echo 
    $row['team_name'].'<br />';
    echo 
    '<img src="images/".$_POST['id'].".jpg" />';
    ?>
    so the image name is based on the id (or you could use team_name etc), see where its going ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thanks for the help. So far the code seems to be working with one problem. What I ended up doing was writing a script that the form on the very first page calls that basically goes like this:

    PHP Code:
    if ($_POST['team'] == 'yankees') {
        
    header('Location: ../teamhome.php');
        
    $header_image 'yankees_header';

    No problems there. The redirects all work. One caveat that I have is that I am using a dreamweaver template to help define the navigation links and footer and that sort of thing. Since the team image is going directly under the header and main navigation, I wanted to call the $header_image variable in the template. I followed your suggestion for the code:
    PHP Code:
    <?php echo '<img src="../images/".$_POST["$header_image"].".jpg" />'?>
    The problem I am now running into is with my quotation marks. When the code is parsed, its cutting the path to the image off at "../images", leaving a nice blank space where the image is supposed to go.

    Here is how I tried to fix it:
    PHP Code:
    <?php echo "<img src=\"../images/'.$_POST['$header_image'].'.jpg\" />"?>
    And this is the error that it generated:

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

    To be perfectly honest, the whole single quote / double quote thing has given me fits. I know why its not working but I am not really sure how to fix it.
    Any suggestions?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    I know they all blur together sometimes
    PHP Code:
    <?php echo '<img src="../images/'.$_POST['$header_image'].'.jpg" />';?> 
    or
    <?php echo "<img src=\"../images/{$_POST['$header_image']}.jpg\" />";?>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I am still getting a missing image icon. Is it possible that the variable $header_image is not being passed from my proccessing script? The header redirect portion works fine, you end up on the page, but the image is still missing. When I check the properties the name of the image file that is supposed to be called is missing.

    /images/.jpg

    I have tried to simply echo the $header_image variable to see if at least the word 'yankees' would appear but nothing shows up. That got me thinking that perhaps the variable value is not avaliable to the target page.

    Any ideas?

    *EDIT*
    I was just thinking... I am using a separate file to hold the script that I am using to determine the proper image. Would it make a difference if I had that script located in the page where I am looking to have the images changed out as opposed to a separate file that is called between the first page (where the selection is made) and the second page (where the images are supposed to show up)?

    *EDIT*
    I tried it and it didn't work. I am going to try and see if it will work on a regular file as opposed to one that is linked to a dreamweaver template.
    Last edited by thepocketgeek; 02-09-2006 at 08:21 PM.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    ak sorry, I was not looking properly last night , I dont know if this is the problem but ...

    $_POST['$header_image']
    should be
    $_POST['header_image']
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    New Coder
    Join Date
    Jun 2005
    Location
    Florida
    Posts
    73
    Thanks
    11
    Thanked 0 Times in 0 Posts
    I tried that earlier in the day with no luck. I think the problem is that the $header_image variable is not being sent over from the processing scipt that I set up. I've tried to just echo the variable to see if at least the words will pop up and got nothing.
    I even noticed that in my script that header('Location: ../team_index.php'); was being called before the variable was being set, so switched them around. Still got nothing.
    What other methods can I use to make sure that a variable is being passed from one page to the next? Do you think that using an intermediary script as opposed to processing the data from the original page is making a difference?


  •  

    Posting Permissions

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