donglie9067 2014-03-10 14:37
浏览 39
已采纳

将PHP重复节/数组封装到字符串中

I have a repeating section from a MySQL look-up - the code (including HTML) is this:

<br>
    <?php do { ?>
    <?php if (substr($row_mDetails['Lo'], 0, 1) === '-') {echo str_replace('.', '', number_format($row_mDetails['Lo'], 5));} else {echo '+'.str_replace('.', '', number_format($row_mDetails['Lo'], 5));}; ?> | 
    <?php if (substr($row_mDetails['La'], 0, 1) === '-') {echo str_replace('.', '', number_format($row_mDetails['La'], 5));} else {echo '+'.str_replace('.', '', number_format($row_mDetails['La'], 5));}; ?>
| <?php echo $row_mDetails['Name']; ?> | 0 |<br>
      <?php } while ($row_mDetails = mysql_fetch_assoc($mDetails)); ?>

How do I get the output of this to a string (for example, $MyDataString = *the output from the above*)

Whenever I try this, I get the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

(Edited to include surrounding HTML)

  • 写回答

2条回答 默认 最新

  • douwei4370 2014-03-10 14:48
    关注

    You need to remove the php from the start of the second line. Then the code runs fine. I would advocate making this code slightly more readable:

    <?php
    do {
        if (substr($row_mDetails['Lo'], 0, 1) === '-') {
            echo str_replace('.', '', number_format($row_mDetails['Lo'], 5));
        } else {
            echo '+' . str_replace('.', '', number_format($row_mDetails['Lo'], 5));
        }
    ?> | <?php
        if (substr($row_mDetails['La'], 0, 1) === '-') {
            echo str_replace('.', '', number_format($row_mDetails['La'], 5));
        } else {
            echo '+' . str_replace('.', '', number_format($row_mDetails['La'], 5));
        }
    ?> | <?php
        echo $row_mDetails['Name'];
    ?> | 0 |<br> <?php
    } while ($row_mDetails = mysql_fetch_assoc($mDetails));
    ?> 
    

    I removed the unnecessary ; from after your elses.

    It's a question of personal preference but you could also replace your

    ?> | <?php
    

    with

    echo " | ";
    

    to avoid opening and closing tags.

    edit

    If you want to build your string in a loop, you could do something like this:

    do {
        if (substr($row_mDetails['Lo'], 0, 1) === '-') {
            $str .= str_replace('.', '', number_format($row_mDetails['Lo'], 5));
        } else {
            $str .= '+' . str_replace('.', '', number_format($row_mDetails['Lo'], 5));
        }
        $str .= " | ";
    
        if (substr($row_mDetails['La'], 0, 1) === '-') {
            $str .= str_replace('.', '', number_format($row_mDetails['La'], 5));
        } else {
            $str .= '+' . str_replace('.', '', number_format($row_mDetails['La'], 5));
        }
        $str .= " | " . $row_mDetails['Name'] . " | 0 |<br> ";
    } while ($row_mDetails = mysql_fetch_assoc($mDetails));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应