Jump to content

Local Variables in PHP


bnichols

Recommended Posts

Grateful for some help in using local variables.

 

I am coding a page for my club's monthly photographic competition. There will be a table of winners (1st, 2nd, 3rd, HC) for each month, so I will have 12 identically constructed tables on the web page.

 

Within each table will be a link to each of the winning pictures: e,g:

<a href="#" onclick="MM_openBrWindow('winningphoto.php?picture=January/slides/After%20the%20Battle.jpg','Competition','scrollbars=yes,resizable=yes,width=900,height=900')">Wembley Stadium</a>

Rather than manually edit this code for every picture every month I would like to use a common template for each month and just declare some variables for each month using the same variable names but to get the appropriate picture to display. e.g:

$month="January";
$1stprizepic="After%20the%20Battle.jpg";
$1stprizetitle="Wembley Stadium";
$1stprizeauthor="Fred Smith";
$2ndprizepic=
$2ndprizetitle=
$2ndprizeauthor=

...and so on
 
then enter the same code in each table but to bring up the appropriate results for the particular month that table represents:
e.g.
<a href="#" onclick="MM_openBrWindow('winningphoto.php?picture=<?php echo "$month"?>/slides/<?php echo "$1stprizepic"?>','Competition','scrollbars=yes,resizable=yes,width=900,height=900')"><?php echo "$1stprizetitle"?> "</a>

(The author's name is not shown in this as it will be a simple echo in another cell)

 

I think I may have a problem with quotes as well....

 

Grateful for any help

 

Brian

Edited by bnichols
Link to comment
Share on other sites

you also shouldn't use a series of numbered variables or repeated blocks of code that only differ in the data values they use. you should use an array to hold the data and loop over the array of data to produce your output. the actual line(s) of output will only exist once in your code. the array index is what changes to distinguish which data gets used each time through the loop. in your case the array index should be the month number (you can produce the displayed month name from its month number.)

Link to comment
Share on other sites

you also shouldn't use a series of numbered variables or repeated blocks of code that only differ in the data values they use. you should use an array to hold the data and loop over the array of data to produce your output. the actual line(s) of output will only exist once in your code. the array index is what changes to distinguish which data gets used each time through the loop. in your case the array index should be the month number (you can produce the displayed month name from its month number.)

Thanks mac.

I thought there may be a role for arrays, but I haven't quite got my mind round arrays yet. I'll try to do some studying :-)

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.