0

I have saved images in database with URL's. What I am doing is that I am fetching images from database and showing that in the Bootstrap carousel. It works perfectly. But I want to get the URL also so that when someone clicks on image, he/she will be redirected to that URL linked with that image.

Here is coding what I have done for showing images in slider.

<div id="myCarousel" class="carousel slide" data-ride="carousel" data-interval="15000">
<!-- Indicators -->
<?php

mysql_connect("localhost","twbpla5_eduardo","[2015]Honor");
mysql_select_db("twbpla5_website_dev"); 
$query = mysql_query("SELECT * FROM banner");

?>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php
while($row=mysql_fetch_array($query))
{
  echo '<div class="item"><img src="images/banners/'.$row[image].'" alt="'.$row[image].'"></div>';
}
?>
</div>

<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
</a>
</div>

<link rel="stylesheet" type="text/css" href="css/carousel.css" />
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#myCarousel .item').first().addClass('active');
$('#myCarousel').carousel();
});
</script>

What should I add in the above code?

Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
Bilal Khawaja
  • 27
  • 3
  • 11
  • 2
    ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 13 '16 at 16:16
  • before i answer i need following thing clear, 1. is your target url same as image url (after adding base bath in case if you have any)? 2. if target url is some other url, did you save it in separate column ? – abhirathore2006 Jul 16 '16 at 12:41

2 Answers2

1

Try to add a link to the image:

<?php
while($row=mysql_fetch_array($query)) {
    echo '<div class="item">
              <a href="images/banners/'.$row[image].'">
                  <img src="images/banners/'.$row[image].'" alt="'.$row[image].'">
              </a>
          </div>';
}
?>
adistoe
  • 97
  • 1
  • 2
  • 9
  • Its not what i am asking about. I want to get the target URL of every image saved in database so that when someone clicks on that image so he/she should be redirected to that target URL. – Bilal Khawaja Jul 13 '16 at 17:12
0

Problem solved... Below is the solution to my question.

<?php
while($row=mysql_fetch_array($query)) {
echo '<div class="item">
          <a href="'.$row[targetURL].'">
              <img src="images/banners/'.$row[image].'" alt="'.$row[image].'">
          </a>
      </div>';
}
?>
Bilal Khawaja
  • 27
  • 3
  • 11