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 10 of 10
  1. #1
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts

    how to echo a value from a group of random values

    In this code below I need to be able to echo $alt_description value on its own and display the rest as normal I have had a go but cant get to work

    PHP Code:
    <?php

    function display_random_img($array) {
        
    $key rand(count($array) -1);
        
    $link_url $array[$key]['url'];
        
    $alt_tag $array[$key]['alt'];
        
    $alt_description $array[$key]['description'];
        
    $random_img_url $array[$key]['img_url'];
        list(
    $img_width$img_height) = getimagesize($random_img_url);
        echo 
    "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
    }

    // Edit the following values accordingly
    $ads_array = array(
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg'
        
    ),
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
          
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009back.jpg'
        
    ),
        array(
            
    'url' => 'http://www.enticeparties.com/',
            
    'alt' => 'Check out the next Entice Parties event',
           
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/enticebabalou.jpg'
        
    )
    );

    ?>
    calling this is cool to get the overall code up

    PHP Code:
    <?php echo display_random_img($ads_array); ?>
    But how do I also call on its own the corresponding random value of the $alt_description

    I tried this below, but obviously theres more to it I cant really see to place
    PHP Code:
    <?php echo display_random_img($alt_description); ?>

  • #2
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    You have multidimensional array.

    In order to process inormation you need to use array key and description in order to get to the array value.

    so you need to use key to get to those values

    PHP Code:

    $array 
    = array (
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg'
        
    )
    );
    print 
    $array[0]['description']; 
    now if you want random description just create some random variable and get that description with $array[$random_variable]['description']

  • #3
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I have had a go but no luck I have included the the full page code below

    PHP Code:
    <?php

    function display_random_img($array) {
        
    $key rand(count($array) -1);
        
    $link_url $array[$key]['url'];
        
    $alt_tag $array[$key]['alt'];
        
    $random_description $array[$key]['description'];
        
    $random_img_url $array[$key]['img_url'];
        list(
    $img_width$img_height) = getimagesize($random_img_url);
        echo 
    "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
       print 
    $array[0]['description'];
    }

    // Edit the following values accordingly
    $ads_array = array(
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg'
        
    ),
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
          
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009back.jpg'
        
    ),
        array(
            
    'url' => 'http://www.enticeparties.com/',
            
    'alt' => 'Check out the next Entice Parties event',
           
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/enticebabalou.jpg'
        
    )
    );
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <title>{$page_title}</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://dj-promo.co.uk/templates/olate/global/animatedcollapse.js">

    /***********************************************
    * Animated Collapsible DIV v2.2- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
    * This notice MUST stay intact for legal use
    * Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
    ***********************************************/

    </script>


    <script type="text/javascript">

    animatedcollapse.addDiv('flyer', 'fade=1,hide=0')
    animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
        //$: Access to jQuery
        //divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
        //state: "block" or "none", depending on state
    }

    animatedcollapse.init()

    </script>

    </head>

    <body>

    <div id="wrapper">

        <div id="logo">
            <table border="0" width="100%">
                <tr>
                    <td width="61%">
                    <p align="left">
                    <a href="javascript:animatedcollapse.toggle('flyer')">where I need the description text value<?php echo display_random_img($array[0]

    [
    'description']); ?></a>

    <div id="flyer" style="width: 300px; background: #FFFFFF; display:none">
    <?php echo display_random_img($ads_array); ?>
    </div>
    </td>

    <div id="wrapper">

        <div id="logo">
                    <td width="38%">&nbsp;</td>
                </tr>
                </table>
        </div>
    as you can see theres a div layer I want to show but want the text link to have the text of the description from the php script how is this done?

  • #4
    Regular Coder funnymoney's Avatar
    Join Date
    Aug 2007
    Posts
    364
    Thanks
    17
    Thanked 24 Times in 24 Posts
    code works for me.

    neat code i must admit.

    i can't say why to you it doesnt' work

  • #5
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    your right it does work - this works below but I need to echo the description else where on the page, print $array[0]['description']; only works for me within the original <?php ?> tags but I want to echo this else where on the html content below any ideas?

    PHP Code:
    <?php

    function display_random_img($array) {
        
    $key rand(count($array) -1);
        
    $link_url $array[$key]['url'];
        
    $alt_tag $array[$key]['alt'];
        
    $random_description $array[$key]['description'];
        
    $random_img_url $array[$key]['img_url'];
        list(
    $img_width$img_height) = getimagesize($random_img_url);
        echo 
    "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>";
       print 
    $array[0]['description'];


    }

    // Edit the following values accordingly
    $ads_array = array(
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg'
        
    ),
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
          
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009back.jpg'
        
    ),
        array(
            
    'url' => 'http://www.enticeparties.com/',
            
    'alt' => 'Check out the next Entice Parties event',
           
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/enticebabalou.jpg'
        
    )
    );
    ?>

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Is this what you are trying to do?
    PHP Code:
    <div id="wrapper">

        <div id="logo">
            <table border="0" width="100%">
                <tr>
                    <td width="61%">
                    <p align="left">
                    <a href="javascript:animatedcollapse.toggle('flyer')"><?php echo $array[$key]['description']; ?></a>
    Last edited by F-b0mb; 03-18-2009 at 02:08 AM.

  • #7
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I think you know what I was after the only problem this now gives me
    Parse error: syntax error, unexpected ')'

  • #8
    New Coder
    Join Date
    Mar 2009
    Posts
    28
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Try now, I edited my other post

  • #9
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I'v tried this
    PHP Code:
    <?php echo $array[$key]['description']; ?>
    and it still doesn't work, I had come to this already not sure why it's not echoing the value - the only way I can get the value is with this, but it wont echo outside the original php tags for the code - print $array[0]['description'];

    PHP Code:
    <?php 

    function display_random_img($array) { 
        
    $key rand(count($array) -1); 
        
    $link_url $array[$key]['url']; 
        
    $alt_tag $array[$key]['alt']; 
        
    $random_description $array[$key]['description']; 
        
    $random_img_url $array[$key]['img_url']; 
        list(
    $img_width$img_height) = getimagesize($random_img_url); 
        echo 
    "<a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a>"
       print 
    $array[0]['description']; 




    // Edit the following values accordingly 
    $ads_array = array( 
        array( 
            
    'url' => 'http://www.urbanfmtv.com/'
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event'
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event'
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg' 
        
    ), 
        array( 
            
    'url' => 'http://www.urbanfmtv.com/'
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event'
          
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event'
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009back.jpg' 
        
    ), 
        array( 
            
    'url' => 'http://www.enticeparties.com/'
            
    'alt' => 'Check out the next Entice Parties event'
           
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event'
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/enticebabalou.jpg' 
        

    ); 
    ?>

  • #10
    New Coder
    Join Date
    Feb 2009
    Posts
    90
    Thanks
    5
    Thanked 0 Times in 0 Posts
    apologies on my understanding I just had to look at it all from a different approach - working code below thanks to all who inputed help

    PHP Code:
    <?php

    function display_random_img($array) {
        
    $key rand(count($array) -1);
        
    $link_url $array[$key]['url'];
        
    $alt_tag $array[$key]['alt'];
        
    $random_description $array[$key]['description'];
        
    $random_img_url $array[$key]['img_url'];
        list(
    $img_width$img_height) = getimagesize($random_img_url);
       echo 
    "<a href=\"javascript:animatedcollapse.toggle('flyer')\"><b>Latest events:</b> ".$array[$key]['description']." </a><div id=\"flyer\" style=\"width: 300px; background: #000000; display:none\"><a href=\"javascript:animatedcollapse.hide('flyer')\"><b>Hide Flyer</b></a><br /><a href=\"$link_url\"><img src=\"$random_img_url\" width=\"$img_width\" height=\"$img_height\" alt=\"$alt_tag\" /></a></div>";

    }

    // Edit the following values accordingly
    $ads_array = array(
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'description' => 'Urban Affair @ Camden Rock 27th March 2009',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009.jpg'
        
    ),
        array(
            
    'url' => 'http://www.urbanfmtv.com/',
            
    'alt' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
          
    'description' => 'Urban Affair @ Camden Rock 27th March 2009 Event',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/urbanaffairmarch272009back.jpg'
        
    ),
        array(
            
    'url' => 'http://www.garage-nation.com/',
            
    'alt' => 'Garage Nation Easter Bank Holiday Monday Special 13th April - DJ EZ Plus many more!',
           
    'description' => 'Garage Nation Easter Bank Holiday Monday Special 13th April - DJ EZ Plus many more!',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/garagenationapril132009front.jpg'
        
    ),    
             array(
            
    'url' => 'http://www.garage-nation.com/',
            
    'alt' => 'Garage Nation Easter Bank Holiday Monday Special 13th April - DJ EZ Plus many more!',
           
    'description' => 'Garage Nation Easter Bank Holiday Monday Special 13th April - DJ EZ Plus many more!',
            
    'img_url' => 'http://dj-promo.co.uk/mixes/flyers/garagenationapril132009back.jpg'
        
    )
    );
    ?>
    and then just echo the lot as usuall!

    PHP Code:
    <?php echo display_random_img($ads_array); ?>


  •  

    Posting Permissions

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