Search code examples
phpmysqlcallhref

a href = php variable inside html div


I have been looking for hours and i have tried multiple items from different websites but still i cant get the following code to work. Let me explain what i want to achieve. In my client databases they have a field where they can add there phonenumber. Now i want to use this phonenumber to create a click to call button. I have the following code already. And I am doing something wrong but i cant figure out what i do wrong.

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

I have also tried this:

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

basically the output should be like this:

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

Thanks for any answers.

From the answers I received so far I changed the code to the following.

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

But it is not working the link is now tel: So it is not showing the number.

Okey it is working now the answer was given by NoLifeKing

Below the code that worked:

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

Solution

  • Replace the while-loop with $row = mysql_fetch_array($resultaat);

    Since you don't have more rows you don't need to loop it all.

    Finished code:

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