SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    multi dimensional array?

    I made this simple php script to display an image, and its alt attribute, I simply creased an array of the building numbers and echoed an image based on what in $id (which is either 0-8 (the array key))
    PHP Code:
    <?php
    $id 
    $_GET['id'];
    $maps = array (
        
    1720,
        
    1730,
        
    1750,
        
    1770,
        
    1760,
        
    1780,
        
    1810,
        
    1820,
        
    1830
    );
    if (
    id==0)
    echo 
    '<img src="/images/map-'.$maps[0].'.jpg" alt="La Sierra">';
    elseif (
    id==1)
    echo 
    '<img src="/images/map-'.$maps[1].'.jpg" alt="Cabrillo">';
    elseif (
    id==2)
    echo 
    '<img src="/images/map-'.$maps[2].'.jpg" alt="La Princessa">';
    elseif (
    id==3)
    echo 
    '<img src="/images/map-'.$maps[3].'.jpg" alt="Las Flores">';
    elseif (
    id==4)
    echo 
    '<img src="/images/map-'.$maps[4].'.jpg" alt="La Playa">';
    elseif (
    id==5)
    echo 
    '<img src="/images/map-'.$maps[5].'.jpg" alt="La Perla">';
    elseif (
    id==6)
    echo 
    '<img src="/images/map-'.$maps[6].'.jpg" alt="El Encanto">';
    elseif (
    id==5)
    echo 
    '<img src="/images/map-'.$maps[7].'.jpg" alt="El Mirador">';
    else
    echo 
    '<img src="/images/map-'.$maps[8].'.jpg" alt="El Camino">';
    ?>
    I was thinking, theres got to be a better way to do this (two dimensional arrays) to hold both the alt thing (building name) and the #..
    Can you show me how to do that?

    Thx.
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,122
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Something like this?
    PHP Code:
    <?php
    $id 
    = (int)$_GET['id'];
    $maps = array (
        array(
    'map' => 1720'alt' => 'La Sierra'),
        array(
    'map' => 1730'alt' => 'Cabrillo'),
        array(
    'map' => 1750'alt' => 'La Princessa'),
        array(
    'map' => 1770'alt' => 'Las Flores'),
        array(
    'map' => 1760'alt' => 'La Playa'),
        array(
    'map' => 1780'alt' => 'La Perla'),
        array(
    'map' => 1810'alt' => 'El Encanto'),
        array(
    'map' => 1820'alt' => 'El Mirador'),
        array(
    'map' => 1830'alt' => 'El Camino')
    );

    echo 
    '<img src="/images/map-'.$maps[$id]['map'].'.jpg" alt="'.htmlentities($maps[$id]['alt']).'">';
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could also add some checks to see if the ID being entered into the URI actually exists in the array:
    PHP Code:
    <?php

    $maps 
    = array(
        array(
    1720'La Sierra'),
        array(
    1730'Cabrillo'),
        array(
    1750'La Princessa'),
        array(
    1770'Las Flores'),
        array(
    1760'La Playa'),
        array(
    1780'La Perla'),
        array(
    1810'El Encanto'),
        array(
    1820'El Mirador'),
        array(
    1830'El Camino')
    );

    if(isset(
    $_GET['id']))
    {
        
    $id = (int) $_GET['id'];
        if(!
    array_key_exists($id$maps))
        {
            
    $id 0;
        }
    }
    else
    {
        
    $id 0;
    }

    echo 
    '<img src="/images/map-',$maps[$id][0],'.jpg" alt="',$maps[$id][1],'">';

    ?>

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,643
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thsnk you
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •