1

I have a HTML Table that I'd like to add a column of links to.

Here's my array

$unis = array( 
               array("title"=>"http://www.aber.ac.uk/en/"),
               array("title"=>"http://www.bangor.ac.uk/")
             ); 

I want to track through the array and print out each link. So I have two Universities in the HTML table and their respective links to appear alongside of them.

Here's my script so far but this prints out two columns with the above links for every record in the table so it looks like

foreach ($a as $i) {
                  echo "<tr><td><img class='uni-image' src='" . $dir . '/' . $i . "' /></td>";
                  echo "<td>" . pathinfo($i, PATHINFO_FILENAME) . "</td>";
                 foreach ($unis as $row) {
                    array_map('htmlentities', $row);

                    echo "<td>" . implode($row) . "</td>";
                 }
                  echo "</tr>";

And the result is

Uni logo | Uni name | http://www.aber.ac.uk | http://www.bangor.ac.uk
Uni logo#2 | Uni name#2 | http://www.aber.ac.uk | http://www.bangor.ac.uk

When what I really want is

Uni logo | Uni name | http://www.aber.ac.uk
Uni logo#2 | Uni name#2 | http://www.bangor.ac.uk
3
  • Why not use this to output data: foreach ($unis as $title=>$url) { Commented Aug 12, 2014 at 15:02
  • Could you expand on this solution @BaileyHerbert with a full example? Commented Aug 12, 2014 at 15:03
  • $a is an array loaded with the image and university names @u_mulder Commented Aug 12, 2014 at 15:04

1 Answer 1

1

Because you are looping over every title you will see every title for every university. Instead try:

foreach ($a as $key => $i) {
    echo "<tr><td><img class='uni-image' src='" . $dir . '/' . $i . "' /></td>";
    echo "<td>" . pathinfo($i, PATHINFO_FILENAME) . "</td>";
    echo "<td>" . htmlentities($unis[$key$]['title']) . "</td>";
    echo "</tr>";
}

However rather than having two arrays that happen to be in the right order I would recommend keeping the data together. I.e. perhaps have an array like:

$unis = array( 
    array("title"=>"http://www.aber.ac.uk/en/",'img'=>'abertayImage'),
    array("title"=>"http://www.bangor.ac.uk/",'img'=>'bangorImage')
); 

This would simplify outputting this information together.

Sign up to request clarification or add additional context in comments.

Comments

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.