[英]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:所以它沒有顯示數字。
在有效的代碼下面:
//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.