【问题标题】:a href = php variable inside html diva href = html div 中的 php 变量
【发布时间】:2013-07-23 14:10:37
【问题描述】:

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

//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>

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

好吧,它现在正在工作,答案是由 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>

【问题讨论】:

    标签: php mysql call href


    【解决方案1】:

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

    由于您没有更多的行,因此您不需要全部循环。

    完成的代码:

    //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>
    

    【讨论】:

    • 我刚刚复制了您的代码并将其放入我拥有的文件中,但它没有显示数字。我将尝试用我的手机调用它,看看它是否真的有效。我已经很兴奋了。我测试了它,它正在工作。非常感谢您的回答。
    • 在您的编辑中,您似乎在mysql_fetch_array 之后错过了;
    【解决方案2】:

    如果您的数据库查询中只有一行,则不需要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' 现在回答了问题,感谢您的输入
    • 对不起,我错过了这行末尾的分号$row = mysql_fetch_assoc($resultaat);我已经更新了我的答案
    【解决方案3】:

    $telephone移入WHILE标签

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

    【讨论】:

    • 感谢您的输入,该问题已被其他用户回答。欣赏!
    【解决方案4】:

    您可以在 PHP 中回显&lt;a&gt; 标签:

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

    【讨论】:

    • 我试过你的建议,但输出只有电话:它没有显示号码
    • 也许你需要把东西放在一个循环中
    • 感谢您提出该问题已由其他用户回答的建议。但我非常感谢您的意见。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多