Brian Ranney
Brian Ranney

Reputation: 1

How to create a php variable which will allow me to incrementally change the href of an html <a> tag

I am working on a project that allows users to search Flickr and select certain images from the list of results which can then be viewed in a gallery. I'll write out the process that I have created so far and explain where I am stuck.

The index page is just a search bar and a submit button. The user types in a search term and hits submit where the form action is action=flickrcall.php

flickrcall.php does this:

<?php
    require_once('flickr.php'); 
    $Flickr = new Flickr; 
    $data = $Flickr->search($_POST["search"]);
    foreach($data['photos']['photo'] as $photo) { 
        echo '<img class="large" onclick="select(this)" src="' . 'http://farm' .  $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">'; 
    }
?>

This will generate a list of image results up to whatever number I specify, right now it is 10. The outcome then is 10 of these:

<img class="large" id="test" onclick="select(this)" src="flickr.img.url.jpg" />

What I want to do is wrap the first <img> with <a href="#img1"></a> then wrap the second <img> with <a href="#img2"></a> and so on. (this is so I can use a CSS lightbox effect on the images later)

I think the way to do it is somehow create a variable (lets call it $hrefNumber) which will count up to however many results there are and use that variable like this in the echo statement above:

echo '<a href="#img' . $hrefNumber . '">
<img class="large" onclick="select(this)" src="' . 'http://farm' . $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg">'
</a>
; 

The problem is that I do now know how to create such a variable nor where to declare/write it.

Does this make sense? Does it sound like I am on the right track or am I way off?

Upvotes: 0

Views: 51

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

$counter=0;

foreach($data['photos']['photo'] as $photo) { 
$counter++;
        echo '<a href=\'#img'.$counter.'\'><img class="large" onclick="select(this)" src="' . 'http://farm' .  $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg"></a>'; 
    }

or

foreach($data['photos']['photo'] as $counter => $photo) {
            echo '<a href=\'#img'.$counter.'\'><img class="large" onclick="select(this)" src="' . 'http://farm' .  $photo["farm"] . '.static.flickr.com/' . $photo["server"] . '/' . $photo["id"] . '_' . $photo["secret"] . '.jpg"></a>'; 
        }

Upvotes: 1

Related Questions