Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

QUESTION ANSWERED:

After messing around with a few things, The combination of the comma at the and of my last array element, and not having the image source path typed correctly was the problem.

Everything works great. It never ceases to amaze me at how a simple syntax error can throw everything out of whack.

Thank you Jonathan and tmsimont! The combo of you're answers fixed my problem.

Time to get a good PHP debugger, Netbeans probably, because Dreamweaver just isn't cutting it for me and I'm not versed enough to hand code YET!

EDIT:

I'm going to try and make myself a little more clear by showing my output source:

<body>
<div id="main">
<div class="container">
<div class="sponsorInner">
<a href="'.$company[1].'">
<img src="img/sponsors/'.$company[0].'.png">
</a>
</div>
</div>
'; } ?&gt;
</div>
</body>

3 things not working:I need the first div with the class gold to render, and the $'s from the array to populate the image link and path.

ORIGINAL QUESTION: I am trying to create multiple div arrangements on a page using a multidimentional array and a foreach loop to populate the div contents with a an image mnemonically named in a relative path folder and the image to be a hyperlink.

My output is not displaying the first correctly, and not populating the array $'s.

This seems right to me, am I missing some type of escape or syntax?

    <body>

    <?php   

        $sponsor = array(
            array('Allstate','http://www.allstateagencies.com/'),
            array('Arrowhead','http://www.arrowheadwater.com/'),
        );
    ?>  

<div id="main">

    <div class="container">     

        <?php           

            foreach($sponsor as $company)
            {
                echo '
                    <div class="gold">
                        <div class="sponsorInner">
                            <a href="'.$company[1].'"><img src="img/sponsors/'.$company[0].'.png"/></a>
                        </div>
                    </div>
                ';
            }
        ?>


    </div><!--.container-->

</div><!--#main-->

</body>
share|improve this question
2  
And what happens? What error do you get? –  SLaks Nov 21 '11 at 21:54
    
It seems to work quite well. –  Aurelio De Rosa Nov 21 '11 at 21:55
    
try harder to describe your actual problem. –  user247245 Nov 21 '11 at 21:56
    
I tried making my question a little easier to understand by showing my output source code. I seriously thought this would work first try, It seems like I'm not escaping quotes. Or maybe I need to drop in and out of PHP between HTML? –  user1058661 Nov 21 '11 at 22:46
    
What server are you using to parse/test the PHP? I copy/pasted the code from your question and it ran fine for me. –  Biotox Nov 22 '11 at 16:22

3 Answers 3

A total guess.. since that's all I can do with the way you've phrased your question... but if your problem is with the image output, try putting a "/" in beginning the src attribute of the image:

  <a href="'.$company[1].'"><img src="/img/sponsors/'.$company[0].'.png"/></a>

that way the browser looks for the img directory from the base of the site, instead of from within whatever subdirectory you're in.

share|improve this answer
    
Thanks, I tried to make my question a little easier to understand by showing my output source code. I've tried using relative and absolute paths, root folders, ect. I still get the problem with my output. –  user1058661 Nov 21 '11 at 22:43
    
Thanks again, trouble shooting the path name, I fixed an error and it helped! –  user1058661 Nov 21 '11 at 23:11

Why not do your array this way

$sponsor = array(
  'Allstate' => 'http://www.allstateagencies.com/',
  'Arrowhead' => 'http://www.arrowheadwater.com/'
);

and later

<?php
foreach($sponsor as $company_name => $company_site)
{
?>
  <div class="gold">
    <div class="sponsorInner">
      <a href="<?php echo $company_site ?>"><img src="img/sponsors/<?php echo $company_name ?>.png"/></a>
    </div>
  </div>
<?php
}
?>

Edit

Remember, your file name is case sensitive. So the .png will need capital letters in their name. You can add the strtolower() around $company_name if you need them lowercase.

share|improve this answer
    
I did name the png files accordingly, If I end up working with a large amount of images, I will put this to use. Cheers! –  user1058661 Nov 21 '11 at 22:41
    
you're suggestion gave me some ideas for the future. Thanks for the response! –  user1058661 Nov 21 '11 at 23:15

You have an comma at the end of this line which you don't need:

array('Arrowhead','http://www.arrowheadwater.com/'),

Please try to do basic debugging before posting.

share|improve this answer
2  
Even though it's not needed it's nothing that will break the code. –  Marcus Nov 21 '11 at 21:58
    
Awesome tip, I've seen arrays written both ways, I didn't know which was "standard". I'm an HTML whiz, but new to PHP. Thanks again. –  user1058661 Nov 21 '11 at 22:44
    
Did that fix it? –  Jonathan Nov 21 '11 at 22:48
    
no, unfortunately this is still my output source: (see above source code in original question) –  user1058661 Nov 21 '11 at 22:55
    
Yes, yes this was the problem. I will look into a good de-bug program. Netbeans probably. I was trying to hand code in Notepad++ and checked syntax in Dreamweaver. No errors = me pulling my hair out over something a basic debug probably would have caught. You're a hero! –  user1058661 Nov 21 '11 at 23:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.