Search code examples
phpmysqlvariableshref

how insert php variable inside a href to create link from that retrieve result of the variable?


<?php
mysql_connect("localhost","username","password") or die (mysql_error());
mysql_select_db("databasename") or die (mysql_error());

$sqlmydata = mysql_query("SELECT id, label, title, info, body FROM table LIMIT 0,10");

// Build the Output Section Here
$outputListdata1 = '';
while($row = mysql_fetch_array($sqldata)){ 

    $id = $row["id"];
    $label = $row["label"];
    $title = $row["title"];
    $info = $row["info"];
    $body = $row["body"];

    $outputListdata1 .= '<div class="content1">
                    <a href= "#" >' .$title. '</a>
                    </div><!-- end of content1 -->

                    <div class="content2">
                    ' .$info. '
                    </div><!-- end of content2 -->';
} // close while loop
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>my site</title>

    </head>

    <body>

    <div class="container">
        <?php print "$outputListdata1"; ?> 
    </div><!-- end of container -->

</body>
</html>

look at the line14 in $outputListdata1 where $title is placed in between href:

<a href="#" >' .$title. '</a>

i want to insert a $label variable in place of # inside the href. And so, it should display the result of the variable from my database.

<a href="$label" >' .$title. '</a>

For example:

if my database have the result for the $label variable is karthik means then it should look like(consider variable $title has the result as fan of stack overflow),

<a href="karthik" > fan of stack overflow</a>

i know my question maybe simple or tough but i'm 100% sure you guys come forward with an answer..thanks in advance.

(note: this is my second question so plz excuse it my questioning method is awkward)


Solution

  • Just insert the $Label Var in the Attribute:

    <?php 
    
    $outputListdata1 .= '<div class="content1">
                         <a href= "' .$label. '" >' .$title. '</a>
                         </div><!-- end of content1 -->
                         <div class="content2">' .$info. '</div><!-- end of content2 -->';
    
    ?>
    

    You should learn the Basics of PHP, maybe do a Tutorial like this: php-for-the-absolute-beginner.