Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question: Creating String array and linking $variable to specific string

    Hey guys,

    I have a website at http://www.wrcc.dri.edu/monitor/WWDT/index.php and I am trying to associate the $folder PHP variable with a string (text) so that I can display this nicely above and below the image chosen by the user. [right click and view source to see what I am talkin' about]

    For example, if users choose SPI 2 Month, $folder=spi2 and I want a string associated with that (lets says $fstring) to be 'SPI 2 Month' ... How do I create an array and link it to the $folder chosen?

    Thanks so much,
    Hope I asked my question properly and made it easy on you guys to answer.

    AJ

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    depending on how you generate your left hand menus there is probably a far more efficient way than this but I guess..

    PHP Code:
    $legends=array(
    'spi1'=>'SPI 1 MONTH',
    'spi2'=>'SPI 2 MONTH',
    //etc etc
    );
    echo 
    $legends($_GET['folder']); 
    if the spi$num format is ALWAYS the same you could

    PHP Code:
    $a=str_split($_GET['folder'],3);
    $legend strtoupper($a[0]).' '.$a[1]. ' MONTH';
    echo 
    $legend
    and avoid the array
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks i'll try the first example as the second wont work since it is not only spi$num variables...

  • #4
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I dont think
    Code:
    echo $legends($_GET['folder']);
    is the proper way to display the current folder/variable. The webpage is blank now. You sure the syntax is correct?

    Should I not use print_r???
    Last edited by ajoros; 07-02-2012 at 08:52 PM.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    sorry my bad again .. I must be getting old
    PHP Code:
    echo $legends[$_GET['folder']]; 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that!

    fire can you link me to somewhere online where I can find literature on this? ive only found stuff on f_print, etc.

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    well here is PHP's array page

    its mostly only hard core coders like Fou-Lou who use printf etc since its methodology is common to several languages

    print_r() is useful for debugging but rarely used in your actual logic
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahhh but I mean on that page where do you see an example of the $_GET function that you used. I wouldnt have known to use the $_GET within the $legend var... I am a nub.

  • #9
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still not working Fire.... code is below for you to check out:
    PHP Code:

    <?php
        $exp
    =time()+86400;
        
    $dir='images';
        
    $img='png';
        
    $cl='_cl';
        
    setcookie ("dirC",$dir,$exp);
        
    setcookie ("clC",$cl,$exp);
        
    setcookie ("imgC",$img,$exp);
        
    // Use value from URL and update cookie with new value
        
    if (isset($_GET['region'])) {
            
    $region $_GET['region'];
            
    setcookie('regionC'$region$exp);
        
    // Else, use cookie value if available
        
    } elseif (isset($_COOKIE['regionC'])) {
                
    $region $_COOKIE['regionC'];
        
    // Finally, no cookie and no URL value so use default
        
    } else {
                
    $region 'ww';
                
    setcookie('regionC'$region$exp);
        }
        
        
    // Use value from URL and update cookie with new value
        
    if (isset($_GET['ds'])) {
            
    $ds $_GET['ds'];
            
    setcookie('dsC'$ds$exp);
        } elseif (isset(
    $_COOKIE['dsC'])) {
                
    $ds $_COOKIE['dsC'];
        } else {
                
    $ds 'PRISM';
                
    setcookie('dsC'$ds$exp);
        }
        
        if (isset(
    $_GET['folder'])) {
            
    $folder $_GET['folder'];
            
    setcookie('folderC'$folder$exp);
        } elseif (isset(
    $_COOKIE['folderC'])) {
                
    $folder $_COOKIE['folderC'];
        } else {
                
    $folder 'pdsi';
                
    setcookie('folderC'$folder$exp);
        }
        
        if (isset(
    $_GET['product'])) {
            
    $product $_GET['product'];
            
    setcookie('productC'$product$exp);
        } elseif (isset(
    $_COOKIE['productC'])) {
                
    $product $_COOKIE['productC'];
        } else {
                
    $product 'per';
                
    setcookie('productC'$product$exp);
        }
        
        
    $legends_var = array(
        
    "pdsi" => "Palmer Drought Severity Index",    
        
    "pzi" => "Palmer Z-Index",
        
    "scpdsi" => "Self-Calibrated PDSI",
        
    "spi1" => "SPI 1 Month",    
        
    "spi2" => "SPI 2 Month",
        
    "spi3" => "SPI 3 Month",    
        
    "spi4" => "SPI 4 Month",
        
    "spi5" => "SPI 5 Month",    
        
    "spi6" => "SPI 6 Month",
        
    "spi7" => "SPI 7 Month",    
        
    "spi8" => "SPI 8 Month",
        
    "spi9" => "SPI 9 Month",    
        
    "spi10" => "SPI 10 Month",
        
    "spi11" => "SPI 11 Month",    
        
    "spi12" => "SPI 12 Month",
        
    "spi15" => "SPI 15 Month",    
        
    "spi18" => "SPI 18 Month",    
        
    "spi24" => "SPI 24 Month",    
        
    "spi30" => "SPI 30 Month",
        
    "spi36" => "SPI 36 Month",    
        
    "spi48" => "SPI 48 Month",
        
    "spi60" => "SPI 60 Month",    
        
    "spi72" => "SPI 72 Month",
        
    "mdn1" => "Temperature Anomaly 1 Month",    
        
    "mdn2" => "Temperature Anomaly 2 Month",
        
    "mdn3" => "Temperature Anomaly 3 Month",    
        
    "mdn4" => "Temperature Anomaly 4 Month",
        
    "mdn5" => "Temperature Anomaly 5 Month",    
        
    "mdn6" => "Temperature Anomaly 6 Month",
        
    "mdn7" => "Temperature Anomaly 7 Month",    
        
    "mdn8" => "Temperature Anomaly 8 Month",
        
    "mdn9" => "Temperature Anomaly 9 Month",    
        
    "mdn10" => "Temperature Anomaly 10 Month",
        
    "mdn11" => "Temperature Anomaly 11 Month",    
        
    "mdn12" => "Temperature Anomaly 12 Month",
        
    "mdn1per" => "Temperature Percentile 1 Month",    
        
    "mdn2per" => "Temperature Percentile 2 Month",
        
    "mdn3per" => "Temperature Percentile 3 Month",    
        
    "mdn4per" => "Temperature Percentile 4 Month",
        
    "mdn5per" => "Temperature Percentile 5 Month",    
        
    "mdn6per" => "Temperature Percentile 6 Month",
        
    "mdn7per" => "Temperature Percentile 7 Month",    
        
    "mdn8per" => "Temperature Percentile 8 Month",
        
    "mdn9per" => "Temperature Percentile 9 Month",    
        
    "mdn10per" => "Temperature Percentile 10 Month",
        
    "mdn11per" => "Temperature Percentile 11 Month",    
        
    "mdn12per" => "Temperature Percentile 12 Month",    
        
    "pon1" => "Precipitation Anomaly 1 Month",    
        
    "pon2" => "Precipitation Anomaly 2 Month",
        
    "pon3" => "Precipitation Anomaly 3 Month",    
        
    "pon4" => "Precipitation Anomaly 4 Month",
        
    "pon5" => "Precipitation Anomaly 5 Month",    
        
    "pon6" => "Precipitation Anomaly 6 Month",
        
    "pon7" => "Precipitation Anomaly 7 Month",    
        
    "pon8" => "Precipitation Anomaly 8 Month",
        
    "pon9" => "Precipitation Anomaly 9 Month",    
        
    "pon10" => "Precipitation Anomaly 10 Month",
        
    "pon11" => "Precipitation Anomaly 11 Month",    
        
    "pon12" => "Precipitation Anomaly 12 Month",
        
    "pon1per" => "Precipitation Percentile 1 Month",    
        
    "pon2per" => "Precipitation Percentile 2 Month",
        
    "pon3per" => "Precipitation Percentile 3 Month",    
        
    "pon4per" => "Precipitation Percentile 4 Month",
        
    "pon5per" => "Precipitation Percentile 5 Month",    
        
    "pon6per" => "Precipitation Percentile 6 Month",
        
    "pon7per" => "Precipitation Percentile 7 Month",    
        
    "pon8per" => "Precipitation Percentile 8 Month",
        
    "pon9per" => "Precipitation Percentile 9 Month",    
        
    "pon10per" => "Precipitation Percentile 10 Month",
        
    "pon11per" => "Precipitation Percentile 11 Month",    
        
    "pon12per" => "Precipitation Percentile 12 Month",
        );

        
    $legends_region = array(
        
    "ww" => "Western US"
        
    "az" => "Arizona",
        
    "ca" => "California"
        
    "co" => "Colorado",
        
    "id" => "Idaho"
        
    "mo" => "Montana",
        
    "nv" => "Nevada"
        
    "nm" => "New Mexico",
        
    "or" => "Oregon"
        
    "ut" => "Utah",
        
    "wa" => "Washington"
        
    "wy" => "Wyoming",    
        
    "us" => "United States",
        );
    ?>


     <?php
                $dir
    ='images';
                echo 
    "<h3> Path: $ds  >  $legends_var[$_GET['folder']]  >  $legends_region[$_GET['region']]</h3>";
                
    //echo "<h3>Currently viewing images using the <u>$ds</u> dataset.</h3>";             
                //sets the folder string name if it was set previously
                
    if(isset($_GET['folder'])) $folder $_GET['folder'];
                
    //sets the product string name if it was set previously
                //if(isset($_GET['product'])) $product = $_GET['product'];
                //if(isset($_GET['region'])) $region=$_GET['region']; 
                //sets the region string name if it was set previously, if not then 'us' is used by default
                
    $img='png';$cl='_cl';$current='_current';$underscore='_';
                  if (
    file_exists("$dir/$folder/$region$cl.$img"))
                  {
                    if (
    $region=='us')
                    { 
                        echo 
    "<!--Displaying image from /$dir/$folder/$region.$img<br>-->
                        <br><img src='$dir/$folder/$region.$img'>
                        <br><br>
                        <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.nc\"><u>Download $ds $legends_var[$_GET['folder']] NETCDF Data for United States</u></a>
                        <br> <br>
                        <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.tif\"><u>Download $ds $legends_var[$_GET['folder']] NETCDF Data for United States</u></a>"
    ;
                    }
                    else
                    { 
                        echo 
    "<!--Displaying image from /$dir/$folder/$region$cl.$img<br>-->
                        <br><img src='$dir/$folder/$region$cl.$img'>
                        <br><br>
                        <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.nc\"><u>Download $ds $legends_var[$_GET['folder']] NETCDF Data for United States</u></a>
                        <br> <br>
                        <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.tif\"><u>Download $ds $legends_var[$_GET['folder']] GeoTIFF Data for United States</u></a>"
    ;
                    }
                  }
                  else
                    { 
                        echo 
    "<h4>File image requested within the $ds dataset is not available</h4><br>"
                    }
                
    ?>
    What is wrong here???

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    your url has ?folder=whatever , that should therefore be present in $_GET['folder'] automagically.
    I think its just the way you are echoing out ...
    try replacing
    PHP Code:
    echo "<h3> Path: $ds  >  $legends_var[$_GET['folder']]  >  $legends_region[$_GET['region']]</h3>"
    with
    PHP Code:
    echo "<h3> Path: $ds  >  {$legends_var[$_GET['folder']]}  >  {$legends_region[$_GET['region']]}</h3>"
    the {braces} tell PHP (in this case) to treat the contents as an array variable
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    New Coder
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am trying to save it into memory (cookies now) but no luck... Its not saving legreg and legvar the way I want it to... upon cookies cleared it should by default have $legreg="Western US" and $legvar="Palmer Drought Severity Index"

    Check out the code :

    PHP Code:
    <?php
            $exp
    =time()+86400;
            
    $dir='images';
            
    $img='png';
            
    $cl='_cl';
            
    setcookie ("dirC",$dir,$exp);
            
    setcookie ("clC",$cl,$exp);
            
    setcookie ("imgC",$img,$exp);
            
    // Use value from URL and update cookie with new value
            
    if (isset($_GET['region'])) {
                    
    $region $_GET['region'];
                    
    setcookie('regionC'$region$exp);
            
    // Else, use cookie value if available
            
    } elseif (isset($_COOKIE['regionC'])) {
                            
    $region $_COOKIE['regionC'];
            
    // Finally, no cookie and no URL value so use default
            
    } else {
                            
    $region 'ww';
                            
    setcookie('regionC'$region$exp);
            }
           
            
    // Use value from URL and update cookie with new value
            
    if (isset($_GET['ds'])) {
                    
    $ds $_GET['ds'];
                    
    setcookie('dsC'$ds$exp);
            } elseif (isset(
    $_COOKIE['dsC'])) {
                            
    $ds $_COOKIE['dsC'];
            } else {
                            
    $ds 'PRISM';
                            
    setcookie('dsC'$ds$exp);
            }
           
            if (isset(
    $_GET['folder'])) {
                    
    $folder $_GET['folder'];
                    
    setcookie('folderC'$folder$exp);
            } elseif (isset(
    $_COOKIE['folderC'])) {
                            
    $folder $_COOKIE['folderC'];
            } else {
                            
    $folder 'pdsi';
                            
    setcookie('folderC'$folder$exp);
            }
           
            if (isset(
    $_GET['product'])) {
                    
    $product $_GET['product'];
                    
    setcookie('productC'$product$exp);
            } elseif (isset(
    $_COOKIE['productC'])) {
                            
    $product $_COOKIE['productC'];
            } else {
                            
    $product 'per';
                            
    setcookie('productC'$product$exp);
            }
           
            if (isset(
    $_COOKIE['legvarC'])) {
                            
    $legvar $_COOKIE['legvarC'];
            } else {
                            
    $legvar "Palmer Drought Severity Index";
                            
    setcookie('legvarC'$legvar$exp);
            }
           
            if (isset(
    $_COOKIE['legregC'])) {
                            
    $legreg $_COOKIE['legregC'];
            } else {
                            
    $legreg "Western US";
                            
    setcookie('legregC'$legreg$exp);
            }
           
            
    $legends_var = array(
            
    "pdsi" => "Palmer Drought Severity Index",     
            
    "pzi" => "Palmer Z-Index",
            
    "scpdsi" => "Self-Calibrated PDSI",
            
    "spi1" => "SPI 1 Month",       
            
    "spi2" => "SPI 2 Month",
            
    "spi3" => "SPI 3 Month",       
            
    "spi4" => "SPI 4 Month",
            
    "spi5" => "SPI 5 Month",       
            
    "spi6" => "SPI 6 Month",
            
    "spi7" => "SPI 7 Month",       
            
    "spi8" => "SPI 8 Month",
            
    "spi9" => "SPI 9 Month",       
            
    "spi10" => "SPI 10 Month",
            
    "spi11" => "SPI 11 Month",     
            
    "spi12" => "SPI 12 Month",
            
    "spi15" => "SPI 15 Month",     
            
    "spi18" => "SPI 18 Month",     
            
    "spi24" => "SPI 24 Month",     
            
    "spi30" => "SPI 30 Month",
            
    "spi36" => "SPI 36 Month",     
            
    "spi48" => "SPI 48 Month",
            
    "spi60" => "SPI 60 Month",     
            
    "spi72" => "SPI 72 Month",
            
    "mdn1" => "Temperature Anomaly 1 Month",       
            
    "mdn2" => "Temperature Anomaly 2 Month",
            
    "mdn3" => "Temperature Anomaly 3 Month",       
            
    "mdn4" => "Temperature Anomaly 4 Month",
            
    "mdn5" => "Temperature Anomaly 5 Month",       
            
    "mdn6" => "Temperature Anomaly 6 Month",
            
    "mdn7" => "Temperature Anomaly 7 Month",       
            
    "mdn8" => "Temperature Anomaly 8 Month",
            
    "mdn9" => "Temperature Anomaly 9 Month",       
            
    "mdn10" => "Temperature Anomaly 10 Month",
            
    "mdn11" => "Temperature Anomaly 11 Month",     
            
    "mdn12" => "Temperature Anomaly 12 Month",
            
    "mdn1per" => "Temperature Percentile 1 Month"
            
    "mdn2per" => "Temperature Percentile 2 Month",
            
    "mdn3per" => "Temperature Percentile 3 Month"
            
    "mdn4per" => "Temperature Percentile 4 Month",
            
    "mdn5per" => "Temperature Percentile 5 Month"
            
    "mdn6per" => "Temperature Percentile 6 Month",
            
    "mdn7per" => "Temperature Percentile 7 Month"
            
    "mdn8per" => "Temperature Percentile 8 Month",
            
    "mdn9per" => "Temperature Percentile 9 Month"
            
    "mdn10per" => "Temperature Percentile 10 Month",
            
    "mdn11per" => "Temperature Percentile 11 Month",       
            
    "mdn12per" => "Temperature Percentile 12 Month",       
            
    "pon1" => "Precipitation Anomaly 1 Month",     
            
    "pon2" => "Precipitation Anomaly 2 Month",
            
    "pon3" => "Precipitation Anomaly 3 Month",     
            
    "pon4" => "Precipitation Anomaly 4 Month",
            
    "pon5" => "Precipitation Anomaly 5 Month",     
            
    "pon6" => "Precipitation Anomaly 6 Month",
            
    "pon7" => "Precipitation Anomaly 7 Month",     
            
    "pon8" => "Precipitation Anomaly 8 Month",
            
    "pon9" => "Precipitation Anomaly 9 Month",     
            
    "pon10" => "Precipitation Anomaly 10 Month",
            
    "pon11" => "Precipitation Anomaly 11 Month",   
            
    "pon12" => "Precipitation Anomaly 12 Month",
            
    "pon1per" => "Precipitation Percentile 1 Month",       
            
    "pon2per" => "Precipitation Percentile 2 Month",
            
    "pon3per" => "Precipitation Percentile 3 Month",       
            
    "pon4per" => "Precipitation Percentile 4 Month",
            
    "pon5per" => "Precipitation Percentile 5 Month",       
            
    "pon6per" => "Precipitation Percentile 6 Month",
            
    "pon7per" => "Precipitation Percentile 7 Month",       
            
    "pon8per" => "Precipitation Percentile 8 Month",
            
    "pon9per" => "Precipitation Percentile 9 Month",       
            
    "pon10per" => "Precipitation Percentile 10 Month",
            
    "pon11per" => "Precipitation Percentile 11 Month",     
            
    "pon12per" => "Precipitation Percentile 12 Month",
            );
     
            
    $legends_region = array(
            
    "ww" => "Western US",
            
    "az" => "Arizona",
            
    "ca" => "California",
            
    "co" => "Colorado",
            
    "id" => "Idaho",
            
    "mo" => "Montana",
            
    "nv" => "Nevada",
            
    "nm" => "New Mexico",
            
    "or" => "Oregon",
            
    "ut" => "Utah",
            
    "wa" => "Washington",
            
    "wy" => "Wyoming",     
            
    "us" => "United States",
            );
    ?>
     
          <?php
                            $dir
    ='images';
                            
    $legvar=$legends_var[$_GET['folder']]; $legreg=$legends_region[$_GET['region']];
                            echo 
    "<h3> Current Product: $ds  >  " $legvar "  >  " $legreg "</h3>";
                            
    //echo "<h3>Currently viewing images using the <u>$ds</u> dataset.</h3>";                      
                            //sets the folder string name if it was set previously
                            
    if(isset($_GET['folder'])) $folder $_GET['folder'];
                            
    //sets the product string name if it was set previously
                            //if(isset($_GET['product'])) $product = $_GET['product'];
                            //if(isset($_GET['region'])) $region=$_GET['region'];
                            //sets the region string name if it was set previously, if not then 'us' is used by default
                            
    $img='png';$cl='_cl';$current='_current';$underscore='_';
                              if (
    file_exists("$dir/$folder/$region$cl.$img"))
                              {
                                    if (
    $region=='us')
                                    {
                                            echo 
    "<!--Displaying image from /$dir/$folder/$region.$img<br>-->
                                            <br><img src='$dir/$folder/$region.$img'>
                                            <br><br>
                                            <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.nc\"><u>Download $ds " 
    $legvar " NETCDF Data for United States</u></a>
                                            <br> <br>
                                            <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.tif\"><u>Download $ds " 
    $legvar " NETCDF Data for United States</u></a>";
                                    }
                                    else
                                    {
                                            echo 
    "<!--Displaying image from /$dir/$folder/$region$cl.$img<br>-->
                                            <br><img src='$dir/$folder/$region$cl.$img'>
                                            <br><br>
                                            <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.nc\"><u>Download $ds " 
    $legvar " NETCDF Data for United States</u></a>
                                            <br> <br>
                                            <a class=\"hvr\" href=\"/monitor/WWDT/data/PRISM/$folder/$folder$current$underscore$ds.tif\"><u>Download $ds " 
    $legvar " GeoTIFF Data for United States</u></a>";
                                    }
                              }
                              else
                                    {
                                            echo 
    "<h4>File image requested within the $ds dataset is not available</h4><br>";
                                    }
                            
    ?>


  •  

    Posting Permissions

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