0

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>
4
  • 2
    And what happens? What error do you get? Commented Nov 21, 2011 at 21:54
  • try harder to describe your actual problem. Commented Nov 21, 2011 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? Commented Nov 21, 2011 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. Commented Nov 22, 2011 at 16:22

3 Answers 3

0

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.

2
  • 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. Commented Nov 21, 2011 at 22:43
  • Thanks again, trouble shooting the path name, I fixed an error and it helped! Commented Nov 21, 2011 at 23:11
0

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.

2
  • 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! Commented Nov 21, 2011 at 22:41
  • you're suggestion gave me some ideas for the future. Thanks for the response! Commented Nov 21, 2011 at 23:15
-1

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.

4
  • 2
    Even though it's not needed it's nothing that will break the code. Commented Nov 21, 2011 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. Commented Nov 21, 2011 at 22:44
  • no, unfortunately this is still my output source: (see above source code in original question) Commented Nov 21, 2011 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! Commented Nov 21, 2011 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.