doujie3888 2013-10-17 15:13
浏览 31

在循环中调用PHP中的变量

i have a website structure as follow

<div id="title">
<h1> //call $title here after executing loop </h1>
</div>
<?php 
   ...
     while ($row = $result->fetch_assoc()) { $title = $row['title']; ?>

<h2> this is the <?php echo $title;?> </h2>
<?php } ?>

is there a way i could still use or call the $title variable on an html element on top of the while loop statement?

whenever i call it above the while loop i get errors like Undefined variable: id..

edit: change id into 'title' instead

  • 写回答

2条回答 默认 最新

  • drra6593 2013-10-17 15:16
    关注

    PHP will read code line by line so variable can't be used before declaration.

    Only one option is to move loop above your div, get html from loop inside variable and print it later.

    I think that you've got only one row in that loop so use this code instead.

    $row = $result->fetch_assoc();
    $title = $row['title'];
    echo '<h2> this is the '.$title.' </h2>';
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致
  • ¥15 禅道二次开发编辑版本,上传不了发行包