bnichols Posted August 5, 2013 Share Posted August 5, 2013 (edited) 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 August 5, 2013 by bnichols Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/ Share on other sites More sharing options...
trq Posted August 5, 2013 Share Posted August 5, 2013 You haven't told us what the problem is. Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443522 Share on other sites More sharing options...
davidannis Posted August 5, 2013 Share Posted August 5, 2013 One problem is that A valid variable name starts with a letter or underscore and $1stprizepic starts with a number. http://www.php.net/manual/en/language.variables.basics.php Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443530 Share on other sites More sharing options...
mac_gyver Posted August 5, 2013 Share Posted August 5, 2013 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.) Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443556 Share on other sites More sharing options...
bnichols Posted August 5, 2013 Author Share Posted August 5, 2013 You haven't told us what the problem is. Sorry trq I thought I had: It is to use the same variables several times on a web page with different contents. Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443562 Share on other sites More sharing options...
bnichols Posted August 5, 2013 Author Share Posted August 5, 2013 One problem is that A valid variable name starts with a letter or underscore and $1stprizepic starts with a number. http://www.php.net/manual/en/language.variables.basics.php Thanks for pointing that out. I can easily change it to $prize1winner etc. Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443563 Share on other sites More sharing options...
bnichols Posted August 5, 2013 Author Share Posted August 5, 2013 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 :-) Quote Link to comment https://forums.phpfreaks.com/topic/280843-local-variables-in-php/#findComment-1443565 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.