繁体   English   中英

HTML div中的href = php变量

[英]a href = php variable inside html div

我一直在寻找时间,并且尝试了来自不同网站的多个项目,但仍然无法获取以下代码。 让我解释一下我想要实现的目标。 在我的客户数据库中,他们有一个可以在其中添加电话号码的字段。 现在,我想使用此电话号码创建点击通话按钮。 我已经有以下代码了。 我做错了什么,但我不知道自己做错了什么。

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
while($row = mysql_fetch_array($resultaat))
  {
  echo $row['telephone'];
  echo "<br>";
  }
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>

我也尝试过这个:

<a href="tel:<?php echo $row['telephone']; ?>"><img src="contact.png"></a>

基本上输出应该是这样的:

<a href="tel:+1800229933">Call us free!</a>

感谢您的任何答案。

从到目前为止收到的答案中,我将代码更改为以下代码。

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_assoc($resultaat))
$telephone = $row['telephone'];
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<?php
echo '<a href="tel:'.$row['telephone'].'"><img src="contact.png"></a>';
?>
</div>
<a href="tel:+1800229933">Call us free!</a>

但是它不起作用,链接现在是tel:所以它没有显示数字。

Okey它正在工作,NoLifeKing给出了答案

在有效的代码下面:

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_array($resultaat);
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>

$row = mysql_fetch_array($resultaat);替换while循环$row = mysql_fetch_array($resultaat);

由于您没有更多的行,因此不需要循环所有内容。

完成的代码:

//select the item from the table    
$sql = "SELECT * FROM $table WHERE id='1'";  
$resultaat = mysql_query($sql) 
  or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
//show the telephonenumber (UNCOMMENT FOR DEBUG)
$row = mysql_fetch_array($resultaat);
//make the phonenumber a variable   
$telephone = $row['telephone'];
?>
<div class="clicktocall">
<a href="tel:<?php echo $telephone ?>"><img src="contact.png"></a>
</div>

如果数据库查询中只有一行,则不需要while循环,因此可以将代码更改为此。

<?php
$sql = "SELECT * FROM $table WHERE id = '1'";  
$resultaat = mysql_query($sql) 
    or die (mysql_error('<div class="tc tc_red">Unable to select the table!</div><br>'));
$row = mysql_fetch_assoc($resultaat);
$telephone = $row['telephone'];
?>

然后,您可以随时随地使用$telephone

$telephone移至WHILE标签

while($row = mysql_fetch_array($resultaat))
{
   echo $telephone = $row['telephone']."<br />";
}

您可以在PHP中回显<a>标记:

while($row = mysql_fetch_array($resultaat))
{
   echo '<a href="tel:'.$row['telephone'].'"><img src="contact.png"></a>';
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM