doupu1727 2015-05-28 18:33
浏览 39
已采纳

在PHP中回显变量的最佳方法[关闭]

From what I've read, it isn't possible to echo more PHP code within an echo so I'm after a solution to the following:

Basically, if the 'closed_state' column in my DB equals 'yes', display a link with a variable on the end else display a different link with a variable on the end.

Here's my code:

<?php 

$result1    = mysqli_query($con,"SELECT closed_state FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result2    = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
$result3    = mysqli_query($con,"SELECT company_id FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());

while($row = mysqli_fetch_array($result1))
{
    if ($row['closed_state'] == "yes")
    {
        echo "<a href="customers_open.php?company_id=<?php while($row = mysqli_fetch_array($result2)) echo "{$row['company_id']}"; ?>">Reopen account</a>";
    } else {  
        echo "<a href="customers_close.php?company_id=<?php while($row = mysqli_fetch_array($result3)) echo "{$row['company_id']}"; ?>">Close account</a>";
    }
}   

?>

Obviously it doesn't work so what's the best way to achieve this?

  • 写回答

4条回答 默认 最新

  • dotif64826 2015-05-28 18:40
    关注

    I don't think you need 3 queries. One should be fine selecting both values. Queries 2 & 3 are identical anyway.

    $result1    = mysqli_query($con,"SELECT company_id, closed_state FROM tbl_company WHERE company_id='$company_id'") or die(mysql_error());
    while($row = mysqli_fetch_array($result1)){
        if ($row['closed_state'] == "yes"){
            echo '<a href="customers_open.php?company_id='.$row['company_id'].'">Reopen account</a>';
        }else{  
            echo '<a href="customers_close.php?company_id='.$row['company_id'].'">Close account</a>';
        }
    }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红