I am relatively new to arrays, and struggling with the amount of information out there to solve my problem succinctly. This is as far as I have progressed.
My code:
<?php
$imagearrdisplay = ",8,7,22,";
$displayitem = explode(",", $imagearrdisplay);
for($i = 0; $i < count($displayitem); $i++){
echo "
<div class='banner'><img src='/images/transitions/$displayitem[$i].jpg' /></div>
Count: $i = $displayitem[$i] ";
}
?>
It currently returns a result of:
<div class='banner'><img src='/images/transitions/.jpg' /></div>
Count: 0 =
<div class='banner'><img src='/images/transitions/8.jpg' /></div>
Count: 1 = 8
<div class='banner'><img src='/images/transitions/7.jpg' /></div>
Count: 2 = 7
<div class='banner'><img src='/images/transitions/22.jpg' /></div>
Count: 3 = 22
<div class='banner'><img src='/images/transitions/.jpg' /></div>
Count: 4 =
What I would like to achieve is the following:
To remove the first and last comma within the imagearraydisplay when it explodes so that I do not end up with a blank image at the beginning and end.
Randomise the results of the array
Perform an if / else function to find the [0] and adjust the first result so that I can use css to style the image accordingly.
To be able to then produce an outcome that looks like:
<div class='firstbanner'><img src='/images/transitions/22.jpg' /></div>
<div class='banner'><img src='/images/transitions/7.jpg' /></div>
<div class='banner'><img src='/images/transitions/8.jpg' /></div
I know it's possible, though struggling through the maze of tutes out there. Any help would be really appreciated.
Try with:
$imagearrdisplay = trim(',8,7,22,', ',');
$displayitem = explode(',', $imagearrdisplay);
shuffle($displayitem);
To check if element is first just do:
if ( $i == 0 ) {}
Have you tried ltrim? http://php.net/manual/en/function.ltrim.php
$imagearrdisplay = ltrim(",8,7,22,", ",");
Also, you can use shuffle to randomize your array: http://php.net/manual/en/function.shuffle.php
To style the first image, you could use CSS instead.
.banner:first-child
{
background-color:yellow;
height:50px
width:50px
}
<div class='banner'><img src='/images/transitions/22.jpg' /></div>
<div class='banner'><img src='/images/transitions/7.jpg' /></div>
<div class='banner'><img src='/images/transitions/8.jpg' /></div>
preg_split can be used to split so that it drops empty pieces: $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
PHP has suffle for array randomization.
For this, something like $array[0] = str_replace(..)
(first element, I suppose), would be enough.
An easy way to solve this would be to alter the string before you split it into an array, using e.g trim($imagearrdisplay, ',')
. But since you ask for a way to remove the first and last values have a look at array_pop()
and array_shift()
.
To randomize the content in the array you can use shuffle()
.
To get the first element you can use an if-statement on the $i
. if($i == 0) {
Use trim
to remove ,
on the left and right. Use shuffle
to randomize the array.
For the output initialize a $class
variable to the first class, then iterate over all numbers, set the $class
to the normal class after the first item:
$imagearrdisplay = ",8,7,22,";
$displayitem = explode(",", trim($imagearrdisplay, ','));
shuffle($displayitem);
$class = 'firstbanner';
foreach($displayitem as $item)
{
$src = sprintf('/images/transitions/%d.jpg', $item);
printf("<div class=\"%s\">
<img src=\"%s\">
</div>
", $class, $src);
$class = 'banner';
}
Note: If you use a recent CSS version, you can identify the first banner as well with the :first-child
pseudo-class.
function trim_comma($string) {
$string = ($string[0] == ',') ? substr($string, 1, strlen($string)) : $string;
$string = ($string[strlen($string) - 1] == ',') ? substr($string, 0, strlen($string) - 1) : $string;
return $string;
}
echo trim_comma(",test,");
//test
this will remove your comma at the begining and at the end.