SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Including files in a while loop

    I have a query and while loop that can be modified to display a variety of tables (e.g. Birds, Mammals, Flowers, etc.). However, each table requires special styling, so the page is getting bloated with code, like this...

    Code:
    switch($MyURL)
    {
     case 'State_Birds':
      // INNER SWITCH(es)
      switch($Symbol)
      {
       case 'bobwhite':
       case 'turkey':
       $Desig = '<span style="padding: 2px; background: #900; color: #fff;"><sup>G</sup></span>';
       break;
       case 'peregrine falcon':
       $Desig = '<span style="padding: 2px; background: #f00; color: #fff;"><sup>R</sup></span>';
       break;
       case 'wood duck':
       $Desig = '<span style="padding: 2px; background: #00f; color: #fff;"><sup>W</sup></span>';
       break;
       case 'Kirtland’s warbler':
       $Desig = '<span style="padding: 2px; background: #f66;"><sup>B</sup></span>';
       break;
       case 'mourning dove':
       $Desig = '<span style="padding: 2px; background: #0ff;"><sup>S</sup></span>';
       break;
       default:
       $Desig = '';
       break;
      }
    So I deleted all the code inside the while loop, pasted it in a separate file, then included it...

    Code:
    require($BaseINC."/2B/inc/D/Content/Child/DB/RefGS/PlanimalsCode.php");
    But it doesn't work - the code, that is. If I insert echo 'Hello World'; on the included file, that works.

    Can anyone tell me how to include a file inside a while loop?

    Thanks.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    You're trying to define a function. So wrap it as such.
    function sup($MyURL,$Symbol) {
    //the code from above here
    }


    and then in the page when you need to call it...
    sup($url,$symbol);

    That said... if you're doing a lot of this stuff, i'd suggest using CSS to define class types and use your switch to more intelligently assign things (or how about... class='$Symbol' and dont use a switch at all?)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks; those are good tips.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I guess I don't understand your code after all. It's supposed to go on the includED file, right? So I would link to the include as before...

    Code:
    require($BaseINC."/2B/inc/D/Content/Child/DB/RefGS/PlanimalsCode.php");
    ...then I would put your code in PlanimalsCode.php, where it would function in lieu of a PHP switch, right?

    I get this error:

    Cannot redeclare sup() (previously declared

    But I don't know how to implement your code correctly. This is obviously not right:

    Code:
    function sup($MyURL,$Symbol) { 
    
      switch($Symbol)
      {
       case 'bobwhite':
       case 'turkey':
       $Desig = '<span style="padding: 2px; background: #900; color: #fff;"><sup>G</sup></span>';
       break;
       case 'peregrine falcon':
       $Desig = '<span style="padding: 2px; background: #f00; color: #fff;"><sup>R</sup></span>';
       break;
       case 'wood duck':
       $Desig = '<span style="padding: 2px; background: #00f; color: #fff;"><sup>W</sup></span>';
       break;
       case 'Kirtland’s warbler':
       $Desig = '<span style="padding: 2px; background: #f66;"><sup>B</sup></span>';
       break;
       case 'mourning dove':
       $Desig = '<span style="padding: 2px; background: #0ff;"><sup>S</sup></span>';
       break;
       default:
       $Desig = '';
       break;
      }
    
      switch($IDArea)
      {
       case 'us-de':
       case 'us-ri':
       case 'us-sd':
       $TRStyle = ' class="linkDark" style="background: #060; color: #fff;"';
       break;
       case 'us-wy':
       $TDStyle = ' style="border-bottom: none;"';
       break;
       default:
       $TRStyle = '';
       break;
      }
    
    }
    But if I replace $MyURL with the actual value, I get this...

    Code:
    function sup(State_Birds,$Symbol)
    ...which can't work, either.

    Thanks.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    So how are you getting the values for $MyURL and $Symbol? Pulling them from a table, the URL string itself?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Addict
    Join Date
    Jan 2012
    Posts
    254
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yikes, I thought I replied, but I just checked and my response didn't get published.

    Anyway, the values are taken from a database, where values in a field named URL determine a pages URL ($MyURL). So if there's a value "State_Birds," a page will display at MySite/Topics/State_Birds.

    So I could organize my code with a PHP switch based on URL's...

    Code:
    switch($MyURL)
    {
     case 'State_Birds':
     // CODE
     break;
     case 'State_Flowers':
     // CODE
     break;
     default:
     break;
    }
    ...but that's going to lead to code bloat. So I'd like to put the PHP switch in a separate file.

    Sorry for the late responses; I don't have an Internet connection at home, so I have to post from a library or WiFi cafe.

  7. #7
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    So... there's two ways I could go with this;
    #1: CSS.
    One nice big CSS file with all the possible stylings.

    .State_Birds_Warbler {
    padding: 2px;
    background-color: #900;
    color: #fff;
    }

    and then something to the effect of;
    echo "<span class='".$MyURL."_".$Symbol."'><sup>G</sup></span>";


    OR..
    2: Database Driven
    Add the following fields to your table:
    background
    color
    symbol
    select the extra fields when you select the data, and use that to construct your styling inline.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

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