this code shows a set of html statements embedded in a php file. Here in the second line i want that reference (href) value to be a variable depending upon the loop value ($i). But when I click on the link it goes to $i.html instead of going to the value in $i. please help
echo '<li>
<div class="imgholder">
<a href= "$i.html">
<img src="images/demo/imgl.png" alt="" />
</a>
</div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';
Try this
echo "<li>
<div class='imgholder'><a href= '$i.html'>
<img src='images/demo/imgl.png' /></a></div>
<div class='latestnews'>
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class='clear' />
</li>"
There is concatenation operator ('.'), which returns the concatenation of its right and left arguments. Try with .
:
echo '<li>
<div class="imgholder"><a href= "'. $i .'.html">
<img src="images/demo/imgl.png" alt="" /></a></div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';
Try with
echo '<li>
<div class="imgholder"><a href= "'.$i.'.html">
<img src="images/demo/imgl.png" alt="" /></a></div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div>
<br class="clear" />
</li>';
You have to concatenate the PHP variable with the HTML tags. You got href
as $i.html
as the value of the variable was not substituted correctly.
How about you make it like this:
<?php for($i = 0; $i<10; $i++){ ?>
<li>
<div class="imgholder">
<a href="<?php echo $i; ?>.html"> <img src="images/demo/imgl.png" alt="" /></a>
</div>
<div class="latestnews">
<h2>Text 1</h2>
<p>Text 2</p>
</div> <br class="clear" />
</li>
<?php } ?>
In this context you have $i is a static String. But you want to have a variable which is dynamically changing.
Edited: Something like this is what I used to do, but it is not "clean" php.