View Full Version : Changing Images and Text with PHP

02-08-2006, 09:53 PM
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:

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!

02-09-2006, 02:37 AM
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 ...

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...)

$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 ?

02-09-2006, 05:17 PM
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:

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 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 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?

02-09-2006, 05:40 PM
I know they all blur together sometimes :D

<?php echo '<img src="../images/'.$_POST['$header_image'].'.jpg" />';?>
<?php echo "<img src=\"../images/{$_POST['$header_image']}.jpg\" />";?>

02-09-2006, 06:29 PM
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.


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?

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)?

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.

02-10-2006, 01:24 AM
ak sorry, I was not looking properly last night , I dont know if this is the problem but ...

should be

02-10-2006, 05:19 PM
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?