【问题标题】:how insert php variable inside a href to create link from that retrieve result of the variable?如何在 href 中插入 php 变量以从该变量的检索结果创建链接?
【发布时间】:2016-01-04 21:54:54
【问题描述】:
<?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>

查看 $outputListdata1 中的第 14 行,其中 $title 位于 href 之间:

&lt;a href="#" &gt;' .$title. '&lt;/a&gt;

我想在href 中插入一个$label 变量来代替#。因此,它应该显示我数据库中变量的结果。

&lt;a href="$label" &gt;' .$title. '&lt;/a&gt;

例如:

如果我的数据库中 $label 变量的结果是 karthik 意味着它应该看起来像(考虑变量 $title 的结果是堆栈溢出的粉丝),

&lt;a href="karthik" &gt; fan of stack overflow&lt;/a&gt;

我知道我的问题可能简单或棘手,但我 100% 确定你们会提出答案..提前致谢。

(注意:这是我的第二个问题,请原谅我的提问方式很尴尬)

【问题讨论】:

  • 所以像标题一样做同样的事情。究竟是什么问题?
  • 你能用我的代码示例写它吗...因为当我插入 ' .$title。 ' 像这样在 $outputListdata1 然后它显示错误..你能理解吗?请帮助过这座桥

标签: php mysql variables href


【解决方案1】:

您可以编写符合 HTML 的 php,使用 echo 命令可以将变量/字符串输出到将发送给用户的 HTML 中

&lt;a href="&lt;?php echo $var1; ?&gt;" &gt; &lt;?php echo $var2; ?&gt;&lt;/a&gt;

【讨论】:

  • 你能用我的代码示例写它吗?请...因为当我插入 ' .$title。 ' 像这样在 $outputListdata1 然后它显示错误..你能理解吗?请帮助过这座桥:(
  • 是的,我按照您所说的进行了尝试,但它在浏览器的代码视图中显示了类似的内容(ctrl+u)...一些文本 ..它只是显示变量而不是拉它的结果:(
【解决方案2】:

只需在属性中插入 $Label Var:

<?php 

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

?>

你应该学习 PHP 的基础知识,也许做一个这样的教程:php-for-the-absolute-beginner

【讨论】:

  • 谢谢,我肯定会在未来几天向像你这样的好心人学习。谢谢你回答我的问题..你能告诉我如何在href中插入两个变量..例如:' .$title. ' 对吗?
  • &lt;a href= "' .$label.$label. '" &gt;' .$title. '&lt;/a&gt; . 连接 2 个字符串。 ' 只是标记字符串的星号/趋势,但是因为你想连接 2 个变量,所以你不需要那里的 '。你的会创建文本'labeltext.labeltext'
【解决方案3】:

我只是把链接放在类似的地方

$SomeLink="index.php";

然后使用

echo "<div>$nam Your Password Could not be Found
<br><br>
<a href=$SomeLink>Click and Return to Login Page</a>
</div>";

两端带有双引号,可在 IE、Edge 和 Google Chrome 上运行

【讨论】:

    【解决方案4】:

    它不起作用,因为您用单引号封装了变量。使用双引号来检索变量的值而不是文字变量文本。

    $outputListdata1 .= "<div class='content1'><a href='$label'>$title</a></div><div class='content2'>$info</div>";
    

    【讨论】:

      【解决方案5】:

      您好,使用方式与使用 $title 相同:

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

      【讨论】:

      【解决方案6】:
      <a href = "$label" >' .$title. '</a>
      

      只需将那行代码粘贴到第 14 行,保证对您有用;)

      【讨论】:

      • 谢谢我知道你能帮助如何在同一个href中插入另一个变量..例如:' .$title。 ' 方法对吗?
      • @kyan2020 这取决于href中的要求......你在上面写的方式只会连接两个变量。
      【解决方案7】:
      <?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"]; 
      } // close while loop 
      ?> 
      <!DOCTYPE html> 
      <html>    
      <head>  
        <meta charset="UTF-8"> 
        <title>my site</title> 
      </head>  
      <body>     
        <div class="container">    
          <div class="content1">    
            <a href= "#" ><?php echo $title;?></a>    
          </div><!-- end of content1 -->               
          <div class="content2">          
            <?php echo $info;?>          
          </div>    
        </div><!-- end of container --> 
      </body>
      </html>
      

      【讨论】:

      • 非常感谢您的朋友花费您的时间来帮助和回答我的问题。没关系:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 2015-04-20
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多