PHP rotator using multidimensional arrays

RESOLVED: See my answer for how I fixed my problems.

I'm not sure if this is possible or not, as I don't know much about arrays, but nothing happens here.

I want to add advertisements to my site.

I figured I could find a way to implement multidimensional arrays for content management.

I came up with this:

$ads = array(
            "ad1" => array(
               title => "Advertisement Title",
               url => "http://example.com",
               image => "http://example.com/images/example.jpg",
               description => "Advertisement Description"),
            "ad2" => array(
               title => "Advertisement Title",
               url => "http://example.com",
               image => "http://example.com/images/example.jpg",
               description => "Advertisement Description"),
            "ad3" => array(
               title => "Advertisement Title",
               url => "http://example.com",
               image => "http://example.com/images/example.jpg",
               description => "Advertisement Description")
);

      

I ran this code with a syntax check and there were no errors, so I figured I was at least on the right track.

I don't understand how to write a loop foreach

that randomly selects one of the advertisements.

Do I need to change "ad1" => array(

to ad[1] => array(

?

I haven't used too many arrays, so I don't know how to target a specific portion of this in for each loop.

I am hoping to come up with a foreach loop that will output something like:

<a href="UrlFromArray"><img src="ImageSrcFromArray" alt="TitleFromArray">

<br>

<p>DescriptionFromArray</p>

Is this achievable?

CHANGE AND UPDATE:

function displayAds728x90() {
 $ads = array(
             "ad1" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad2" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad3" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description")
            );
            $randomAd = array_rand($ads);
             echo '<a href="'.$randomAd->url.'" target="_blank">';
             echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
             echo '</a>';
             echo '<p>';
             echo $randomAd->description;
             echo '</p>';

}
displayAds728x90();

      

Following on from Dynelight's answers, I came up with the above code.

Now my only problem is I am getting the following errors:

Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27

      

Just so you know which line numbers correspond to my complete code:

<img src="http://www.example.com/images/your_banner_here.png">

<?php
function displayAds728x90() {
 $ads = array(
             "ad1" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad2" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad3" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description")
            );
            $randomAd = array_rand($ads);
             echo '<a href="'.$randomAd->url.'" target="_blank">';
             echo '<img src="'.$randomAd->image.'" alt="'.$randomAd->title.'">';
             echo '</a>';
             echo '<p>';
             echo $randomAd->description;
             echo '</p>';

}
displayAds728x90();
?>

      

Any ideas on what is causing these errors?

UPDATE 2:

Edited the following division and added missing code snippets:

$randomAd = array_rand($ads);
             echo '<a href="'.$ads->$randomAd->url.'" target="_blank">';
             echo '<img src="'.$ads->$randomAd->image.'" alt="'.$ads->$randomAd->title.'">';
             echo '</a>';
             echo '<p>';
             echo $ads->$randomAd->description;
             echo '</p>';

      

Executed a var_dump

on $ads

and got the following:

array(3) { ["ad1"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad2"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } ["ad3"]=> array(4) { ["title"]=> string(19) "Advertisement Title" ["url"]=> string(18) "http://example.com" ["image"]=> string(37) "http://example.com/images/example.jpg" ["description"]=> string(25) "Advertisement Description" } }

      

Rejecting the full page code posted above errors is now:

 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 23
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 24
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27
 Sorry, an error happened. Please try again later
Error 8 : Trying to get property of non-object in /home/jollyrogerpcs/public_html/includes/adRotator728_90.php on line 27

      

+3


source to share


2 answers


The foreach function is used to iterate over all the elements of an array. You want to get random elements of the array. Take a look at this function, you might find it useful:

http://php.net/manual/en/function.array-rand.php



You get an element in random order and link to it like this:

<?php $random_element = array_rand ( $ads); ?>

<a href="<?php echo $ads->$random_element->url ?>">
<img src="<?php echo $ads->$random_element->image ?>" alt="<?php echo $ads->$random_element->title ?>"></a>
<p><?php echo $ads->$random_element->description; ?></p>

      

+3


source


I understood that. I am posting this as an answer in case another user has a similar problem and I can use my question and answer as reference :)

Full working code:



function displayAds728x90() {
 $ads = array(
             "ad1" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com/1",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad2" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com/2",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description"),
             "ad3" => array(
                'title' => "Advertisement Title",
                'url' => "http://example.com/3",
                'image' => "http://example.com/images/example.jpg",
                'description' => "Advertisement Description")
            );
            $randomAd = array_rand($ads);
             echo '<a href="'.$ads[$randomAd]['url'].'" target="_blank">';
             echo '<img src="'.$ads[$randomAd]['image'].'" alt="'.$ads[$randomAd]['title'].'">';
             echo '</a>';
             echo '<p>';
             echo $ads[$randomAd]['description'];
             echo '</p>';

}
 displayAds728x90();

      

Instead of targeting keys with, $ads->$randomAd->description

I target them $ads[$randomAd]['description']

and it works :)

0


source







All Articles