drdu53813 2014-05-07 15:37
浏览 35
已采纳

创建变量模板以显示数据库内容

I'm working on a database website currently and part of the project is to display the content of a database in a custom form using pixels to position the various fields.

I am however having trouble getting the code to work as the way I'm creating the form doesn't seem to work in a php tag, but It requires a WHILE loop in order to continue running until the database field is empty.

I'm only including the relevant code section so as to not clog up the post, all my coding is segmented so I can isolate bugs.

<?php 

$usernameh = 20;
$units = "px";

for ($x=0; $x<=2; $x++) {
$pix=$value . $units;

<div style="position:absolute;left:10px;top:20px;width:100px;height:20px;z-      index:5;text-align:left;">
<span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>


$value += 150;
}
?>

The idea is to substitute the 20px on the first line of the divcommand with $pix so that with each iteration the value increases by a set amount and separates out the entries.

I'm very new to php coding, as in only really started 2 weeks ago. I'm sure there is a simple solution to this but I'm not sure what question to ask Google to get the response I'm after.

I hope my problem makes sense. The database is in MySQL but all that is fine, its just the formatting I'm struggling with. Even without using a variable in the formatting code the script crashes while the php tag is in effect.

Can anyone offer any advice on where my problem is or suggest another alternative to this.

Thanks!

  • 写回答

1条回答 默认 最新

  • dongou5100 2014-05-07 15:43
    关注

    You need to break out of PHP ?> before HTML and then switch back to PHP <?php before more PHP code

    $value = 20;
    $units = "px";
    
    for ($x=0; $x<=2; $x++) {
    $pix = $value . $units;
    ?>
    
    <div style="position:absolute;left:10px;top:<?php echo $pix ?>;width:100px;height:<?php echo $pix ?>;z-      index:5;text-align:left;">
    <span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>
    
    <?php
    $value += 150;
    }
    

    Or for the HTML you can echo it from PHP:

    echo '<div style="position:absolute;left:10px;top:' . $pix . ';width:100px;height:' . $pix . ';z-      index:5;text-align:left;">
    <span style="color:#000000;font-family:Arial;font-size:15px;">Field Name:</span></div>';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并