...

View Full Version : how to echo a value from a group of random values



DJ Hands3
03-17-2009, 07:53 PM
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

function display_random_img($array) {
$key = rand(0 , 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 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 echo display_random_img($alt_description); ?>

funnymoney
03-17-2009, 08:13 PM
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




$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']

DJ Hands3
03-17-2009, 09:11 PM
I have had a go but no luck I have included the the full page code below



<?php

function display_random_img($array) {
$key = rand(0 , 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?

funnymoney
03-17-2009, 11:25 PM
code works for me.

neat code i must admit.

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

DJ Hands3
03-18-2009, 01:15 AM
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

function display_random_img($array) {
$key = rand(0 , 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'
)
);
?>

F-b0mb
03-18-2009, 01:28 AM
Is this what you are trying to do?

<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>

DJ Hands3
03-18-2009, 01:48 AM
I think you know what I was after the only problem this now gives me
Parse error: syntax error, unexpected ')'

F-b0mb
03-18-2009, 02:09 AM
Try now, I edited my other post

DJ Hands3
03-18-2009, 08:46 PM
I'v tried this


<?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

function display_random_img($array) {
$key = rand(0 , 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'
)
);
?>

DJ Hands3
03-18-2009, 10:02 PM
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:thumbsup:



<?php

function display_random_img($array) {
$key = rand(0 , 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 echo display_random_img($ads_array); ?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum