NYCPHP Meetup

NYPHP.org

[nycphp-talk] $$ question

Michael Southwell michael.southwell at nyphp.org
Wed May 25 23:49:51 EDT 2005


At 10:51 PM 5/25/2005, you wrote:

>I agree.  There is a time and place for variable variables and they
>are few and far between.  One appropriate scenario is when using the
>reflection APIs, for instance.  With what info we had about the
>scenario originally in question, I don't feel that is a case where I
>think they are appropriate.

At the risk of looking dumb again, let me revisit this, to explain why I 
was doing it this way, in the interest of exposing some of the smart 
thinking to be found on this list and learning something (and with a 
possible phundamental in mind as well ;-).

I have an array of 2005 concert dates;  I want to display them in a table 
with a heading and date and venue columns.  I have another array of 2006 
concert dates; I want to do the same in a different table.  I wrote a 
function to pull in information for the appropriate year and display 
it.  Then I call the function for 2005 and again for 2006.  This gives me 
no redundant display coding.  Next year I will simply insert the info into 
the appropriate 2007 array, and call the function with that year.

You can see this at work at http://lauratheodore.com/schedule.php.

Relevant code is here:
=============
$concerts2006 = array(
array("January 28","Cypress Club - West Palm Beach, FL"),
// etc.
);

function showSchedule( $year ) {
   ?>
   <h2>Tour Schedule, <?= $year ?></h2>
   <table summary="a list of dates on Laura&#8217;s tour schedule for <?= 
$year ?>">

   <tr>
   <th scope="col">Date</th>
   <th scope="col">Venue</th>
   </tr>

   <?php
   $list = 'concerts' . $year;
   global $$list;
   $num = count( $$list );
   for ($i = 0; $i < $num; $i++){
     ?>
     <tr>
     <td class="left">
     <?php echo ${$list}[$i][0]; ?></td>
     <td class="right">
     <?php echo ${$list}[$i][1]; ?></td>
     </tr>
     <?php
   }
   ?>

   </table>
   <?php
}

showSchedule( '2006' );
=================


>As an alternative solution, personally I'd would have reworked it to
>use a 3 dimensional array, even if the first dimension was small.
>     $concerts[$year][$i][$field]

The best I can figure out, this does not permit the (easy) creation of two 
distinct tables.

But from the educational point of view, above is the task I had and the 
solution I found.  How can it be done better?


Michael Southwell, Vice President for Education
New York PHP
http://nyphp.org/twoday - In-depth PHP Training Courses




More information about the talk mailing list