普通网友 2016-05-12 19:04
浏览 51
已采纳

将URL链接到图像或标题而不是回显链接,从php中的数据库检索图像,标题和链接

I am getting image, title and external URL from database, and display it on my page, code is working well, But i don't want to display URL, I want when I click image or title it should go direct to that URL in new window. My Code is

<?php 
    include_once("init.php");

    $msg='';
    $adDir ='./'.EDITORIALNEWS;
$qr = mysql_query("select * from " .enews) or die('Error in connection');
while($rs = mysql_fetch_array($qr))
{
    $adv = '';
    if($rs['adv']!='') {
        $adv = '<img src="'.$adDir.$rs['adv'].'" style="width:60px; height:60px" >&nbsp;';
    }
    $image = $adv;
 $msg .='<tr>
    <td>'.$image.'</td>
    <td>'.$rs['title'].'</td>
    <td>'.$rs['adv_url'].'</td>
  </tr>';
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Editorialt News  Lists</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="headerstyle.css" />
</head>

<body>

<div class="tabl">

<table width="95%" border="1" align="center" cellpadding="10">
  <tr>
  <td width="35%"><strong>File</strong></td>
    <td width="30%"><strong>Title</strong></td>
    <td width="30%"><strong>Link</strong></td>



  </tr>
 <?php echo $msg;?>
</table>

</body>
</html>

Image, Title, URL are adv, title and adv_url respectively. I think the line added should be like

<a href= "#" onClick="window.open('<?=$adv['adv_url']?>', '_blank')"><img src="<?=VIDEO.$adv['adv']?>" width="98%"  height="90" alt=""></a>

but how and where to use it don't know. Being a student I have keen interest in learning PHP through projects.

  • 写回答

1条回答 默认 最新

  • drmq16019 2016-05-12 19:11
    关注

    You can do it like as follows:

    Replace the following line of your code with my code:

    if($rs['adv']!='') {
        $adv = '<img src="'.$adDir.$rs['adv'].'" style="width:60px; height:60px" >&nbsp;';
    }
    

    //My code

    if($rs['adv']!='') {
    $adv = '<a href="'.$adv['adv_url'].'" target="_blank"><img src="'.$adDir.$rs['adv'].'" style="width:60px; height:60px" >&nbsp;</a>';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?