PHP Content Rotator Using Multidimensional Arrays

108 Views Asked by At

SOLVED: Please see my answer for how I fixed my issues.

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

I'm wanting to add advertisements onto my website.

I figured I could find a way to implement multidimensional arrays into this to control the content.

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 through a syntax checker and there were no errors so I figured I'm at least on the right track.

What I do not understand is how to write a foreach loop that randomly selects one of the advertisements.

Would 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 part of this in a for each loop.

I'm 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?

EDIT & 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 the answer given by Dynelight I've come up with the above code.

Now my only issue 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 are which my full code is:

<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 section and added missing bits of code:

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

Performed 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" } }

Going off of the full page code posted above the errors are 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
2

There are 2 best solutions below

7
Dynelight On

The foreach function is used to iterate over all the elements of the array. You want to get random elements of the array. Look into this function, perhaps it will be useful for you:

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

You get the elemenet randomly and reference it something 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>
0
Jesse Elser On

I figured it out. I'm posting this as an answer just in case another user has a similar issue and can use my question and answer as help :)

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 targeted them with $ads[$randomAd]['description'] and it works :)