SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Associative Arrays

    Ok.. I have some really bad code on a site that I am working on.

    Basically it has a "switch/case" statement that has 8 repetitive block. Each of these blocks outputs an iteration
    of this code:
    Code:
    <!-- Tools On -->
    <table border="0" cellpadding="0" cellspacing="0" width="763" height="33">
      <tr>
        <td width="8" height="33"><img src="images/tabs_on_0000.gif" border="0"></td>
        <td width="51" height="33"><a href="http://www.witchesgrove.com/default.php"><img src="images/tabs_off_0001.gif" border="0"></a></td>
        <td width="102" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/1"><img src="images/tabs_off_0002.gif" border="0"></a></td>
        <td width="78" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/2"><img src="images/tabs_off_0003.gif" border="0"></a></td>
        <td width="114" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/3"><img src="images/tabs_off_0004.gif" border="0"></a></td>
        <td width="97" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/4"><img src="images/tabs_on_0005.gif" border="0"></a></td>
        <td width="106" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/6"><img src="images/tabs_off_0006.gif" border="0"></a></td>
        <td width="130" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/5"><img src="images/tabs_off_0007.gif" border="0"></a></td>
        <td width="72" height="33"><a href="http://www.witchesgrove.com/default.php/cPath/7"><img src="images/tabs_off_0008.gif" border="0"></a></td>
        <td width="5" height="33"><img src="images/tabs_off_0009.gif" border="0"></td>
        <td width="1" height="33"></td>
      </tr>
      <tr>
        <td colspan="11" class="sectionOption">
          <table width="100%" cellspacing="0" cellpadding="3" border="0" align="center" class="sectionOption">
            <tr>
              <td valign="middle" align="center" width="16%"><!--Best Sellers--></td>
              <td valign="middle" align="center" width="16%"><a href="http://www.witchesgrove.com/default.php/cPath/4_79" class="whitelink">Spell Kits</a></td>
              <td valign="middle" align="center" width="18%"><a href="http://www.witchesgrove.com/altar.php" class="whitelink">Build Your Altar</a></td>
              <td valign="middle" align="center" width="18%"><a href="http://www.witchesgrove.com/tools.php" class="whitelink">Consecrate Tools</a></td>
              <td valign="middle" align="center" width="16%"><a href="http://www.witchesgrove.com/candlemagic.php" class="whitelink">Candle Magic</a></td>
              <td valign="middle" align="center" width="16%"><a href="http://www.witchesgrove.com/charmmagic.php" class="whitelink">Charm Magic</a></td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td width="8" height="1"></td>
        <td width="51" height="1"></td>
        <td width="102" height="1"></td>
        <td width="78" height="1"></td>
        <td width="114" height="1"></td>
        <td width="97" height="1"></td>
        <td width="106" height="1"></td>
        <td width="130" height="1"></td>
        <td width="72" height="1"></td>
        <td width="5" height="1"></td>
      </tr>
    </table>
    <!-- End Tools -->
    I rewrote the first row of the table so that it is generated dynamically via PHP. I also added rollovers and some IE
    transitions. All this is generated dynamically. The file looks like:
    PHP Code:
    <?php
      
    $top_category_id 
    5// Remove in live system

    // Initialize tabs array.
    // Array = Category, URL, Image Off Path, Image On Path, On/Off Flag, width
    $tabs = array (
      
    "homepage" => array (0,"http://www.witchesgrove.com/default.php","images/tabs_off_0001.gif","images/tabs_on_0001.gif",FALSE,51),
      
    "books" => array (1,"http://www.witchesgrove.com/default.php/cPath/1","images/tabs_off_0002.gif","images/tabs_on_0002.gif",FALSE,102),
      
    "apothocary" => array (2,"http://www.witchesgrove.com/default.php/cPath/2","images/tabs_off_0003.gif","images/tabs_on_0003.gif",FALSE,78),
      
    "home" => array (3,"http://www.witchesgrove.com/default.php/cPath/3","images/tabs_off_0004.gif","images/tabs_on_0004.gif",FALSE,114),
      
    "tools" => array (4,"http://www.witchesgrove.com/default.php/cPath/4","images/tabs_off_0005.gif","images/tabs_on_0005.gif",FALSE,97),
      
    "bath" => array (6,"http://www.witchesgrove.com/default.php/cPath/6","images/tabs_off_0006.gif","images/tabs_on_0006.gif",FALSE,106),
      
    "jewelry" => array (5,"http://www.witchesgrove.com/default.php/cPath/5","images/tabs_off_0007.gif","images/tabs_on_0007.gif",FALSE,130),
      
    "gifts" => array (7,"http://www.witchesgrove.com/default.php/cPath/7","images/tabs_off_0008.gif","images/tabs_on_0008.gif",FALSE,72));
    ?>
    <script>
    // Preload All On Images
    <?php
    while (list ($key$val) = each ($tabs)) {
      echo 
    $key."On = new Image(".$tabs[$key][5].",33);\n";
      echo 
    $key."On.src = ".$tabs[$key][3].";\n";
      echo 
    $key."Off = new Image(".$tabs[$key][5].",33);\n";
      echo 
    $key."Off.src = ".$tabs[$key][2].";\n\n";
    }  
    ?>

    function imgSwap(imgName, newImgSrc) {
      if (document.images) {
        if (document.images[imgName].filters[0])
          document.images[imgName].filters[0].apply();
        document.images[imgName].src = newImgSrc;
        if (document.images[imgName].filters[0])
          document.images[imgName].filters[0].play();
      }
    }
    </script>

    <table border="0" cellpadding="0" cellspacing="0" width="763" height="33">
      <tr>
        <td width="8" height="33" align="right"><img src="images/tabs_on_0000.gif" border="0"></td>
    <?php
    reset
    ($tabs);
    while (list(
    $key$val) = each($tabs)) {
      if (
    $tabs[$key][0]== $top_category_id) {
        echo 
    '    <td width="'.$tabs[$key][5].'" height="33"><a href="'.$tabs[$key][1].'"><img name="'.$key.'" src="'.$tabs[$key][3].'" width="'.$tabs[$key][5].'" height="33"  border="0" onMouseOver="imgSwap(\''.$key.'\',\''.$tabs[$key][2].'\');" onMouseOut="imgSwap(\''.$key.'\',\''.$tabs[$key][3].'\');" style="filter:progid:DXImageTransform.Microsoft.RandomDissolve(duration=0.50);" /></a></td>';
        echo 
    "\n";
      } else {
        echo 
    '    <td width="'.$tabs[$key][5].'" height="33"><a href="'.$tabs[$key][1].'"><img name="'.$key.'" src="'.$tabs[$key][2].'" width="'.$tabs[$key][5].'" height="33" border="0" onMouseOver="imgSwap(\''.$key.'\',\''.$tabs[$key][3].'\');" onMouseOut="imgSwap(\''.$key.'\',\''.$tabs[$key][2].'\');" style="filter:progid:DXImageTransform.Microsoft.RandomDissolve(duration=0.50);" /></a></td>';
        echo 
    "\n";    
      }  
    }  

    ?>
        <td width="5" height="33" align="left"><img src="images/tabs_off_0009.gif" border="0"></td>
        <td width="1" height="33"></td>
      </tr>
    </table>
    Basically what I want to know is if it is possible to associate a second array as an Item in my 2 dimensional array
    above. This "Third Dimension" would have 12 elements referenced in couplets. It would then be used to build the second table
    row listed above. Is this as simple as building the array (either statically or in a database table) and simply referencing
    its variable in the larger array and if so how would it be accessed.
    Last edited by Hierophant; Mar 5, 2002 at 15:51.
    Wayne Luke
    ------------


  2. #2
    SitePoint Columnist Skunk's Avatar
    Join Date
    Jan 2001
    Location
    Lawrence, Kansas
    Posts
    2,066
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't quite understand the question (I got confused on that second array) but if you want to store a complicated data structure in an associative array it might be worth using an array of objects instead. That way your data structure can be as complex as you like:

    $array['homepage'] = new DataObject('Constructor', 'lots of stuff here', 'even arrays', array(3, 4, 5));

  3. #3
    Your Lord and Master, Foamy gold trophy Hierophant's Avatar
    Join Date
    Aug 1999
    Location
    Lancaster, Ca. USA
    Posts
    12,305
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks... I didn't want to get involved with classes too much because of the complexity.

    Hard to imagine that making a simple tabbed header could get so complicated huh?
    Wayne Luke
    ------------


  4. #4
    Are you ready for BSD? Marshall's Avatar
    Join Date
    Dec 2001
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /* Nth dimension associative array example */

    $myarray = array(
       
    "row1" => array(
          
    "row1a" => array(
             
    "row1ai" => array(
                
    "row1ai-val1" => "My row1ai-val1",
                
    "row1ai-val2" => "My row1ai-val2"
             
    ),
             
    "row1aii" => array(
                
    "row1aii-val1" => "My row1aii-val1",
                
    "row1aii-val2" => "My row1aii-val3",
                
    "row1aii-val3" => "My row1aii-val3"
             
    )
          ),
          
    "row1b" => "My row1b",
          
    "row1c" => array(
             
    "row1ci" => "My row1ci",
             
    "row1cii" => "My roe1cii"
          
    ),
          
    "row1d" => "My row1d"
       
    ),
       
    "row2" => array(
          
    "row2a" => array(
             
    "row2ai" => "My row2ai"
          
    ),
          
    "row2b" => "My row2b"
       
    )
    );

    /* Displays "My row1aii-val3" */
    echo $myarray['row1']['row1a']['row1aii']['row1aii-val3'];

    /* Displays "My row2b" */
    echo $myarray['row2']['row2b']; 
    Hope that helps.

    - Marshall


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
  •