dongyang0005 2014-10-21 14:36
浏览 81
已采纳

在PHP代码中嵌入HTML

I have to fetch data using a REST Api, and my response is in json. After decoding the response is in an array I have to display it in a webpage such that each of the responses in the array get displayed in a specific display format (Just Like tweets in twitter are displayed in a specific box type format).How to loop through the array in php and display it ? The only solution I find is embedding the HTML in php using echo which I did but it is not working .And the debugger isn't helping much. What am I missing ?

Please advise what to do.

Here is my code:

    echo  "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-  popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890"  data-name="" data-user-id="2665607941" data-expanded-footer="&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;" data-you-follow="false"data-you-block="false">" ;

    echo "<div class="content">" ;

    echo "<div class="stream-item-header">" ;

    echo "<a  class="account-group js-account-group js-action-profile js-user-profile-link js-nav" data-user-id="2665607941">
           <img class="avatar js-action-profile-avatar" src="https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg" alt="">
           <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part> </strong>
           <span>&rlm;</span><span class="username js-action-profile-name" data-aria-label-part><s></s><b>   </b></span>
           </a>" ;

    echo "<small class="time">
          <a  class="tweet-timestamp js-permalink js-nav js-tooltip" title="11:00 PM - 25 Sep 2014" ><span class="_timestamp js-short-timestamp " 
           data-aria-label-part="last" data-time="1411711218" data-time-ms="1411711218000" data-long-form="true">Sep 25</span></a>
          </small>" ;


    echo"</div>";

    echo "<p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0"> </p>" ;

    echo "<div class="stream-item-footer">";

    echo "<ul class="tweet-actions js-actions">";

    echo "<li class="more-tweet-actions">";
    echo "<div class="action-more-container">";
    echo "<div class="dropdown">";
    echo "<button type="button" class="btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip">";
    echo "<span class="Icon Icon--delete"></span>";
    echo "<b>Delete</b>";
    echo "</button>";
    echo "<div class="dropdown-menu">";
   echo "<div class="dropdown-caret">";
echo "<div class="caret-outer"></div>";
echo "<div class="caret-inner"></div>";
echo "</div>";

echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>"; 
echo "</div>";
echo "</div>";

After Scott's answer I changed my code , but still it is not working and I am not getting it.

 echo  "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable js-original-tweet   

my-tweet\" data-tweet-id="515380319977893890\"data-item-id=\"515380319977893890\" data-name=\"\" data-user-id=\"2665607941\" data-expanded-footer=\"<div class="js-tweet-details-fixer tweet-details-fixer"> <div class="entities-media-container js-media-container" style="min-height:0px"> </div> <div class="js-machine-translated-tweet-container"></div> <div class="js-tweet-stats-container tweet-stats-container "> </div> <div class="client-and-actions"> <span class="metadata"> <span>11:00 PM - 25 Sep 2014</span> &middot; <a class="permalink-link js-permalink js-nav" href="/status/515380319977893890" tabindex="-1">Details</a> </span> </div> </div> " data-you-follow=\"false\"data-you-block=\"false\">" ;

echo "" ;

  echo   "<div class=\"stream-item-header\">" ;

     echo "<a  class=\"account-group js-account-group js-action-profile js-user-profile-link js-nav\" data-user-id=\"2665607941\
     ">
           <img class=\"avatar js-action-profile-avatar\" src=\"https://pbs.twimg.com/profile_images/506790785572814848/vdj_UAuZ_bigger.jpeg\" alt=\"\">
           <strong class=\"fullname js-action-profile-name show-popup-with-id\" data-aria-label-part> </strong>
           <span>&rlm;</span><span class=\"username js-action-profile-name\" data-aria-label-part><s></s><b>   </b></span>
           </a>" ;

    echo "<small class=\"time\">
          <a  class=\"tweet-timestamp js-permalink js-nav js-tooltip\" title=\"11:00 PM - 25 Sep 2014\" ><span class=\"_timestamp js-short-timestamp \" 
           data-aria-label-part=\"last\" data-time=\"1411711218\" data-time-ms=\"1411711218000\" data-long-form=\"true\">Sep 25</span></a>
          </small>" ;


    echo"</div>";

    echo "<p class=\"js-tweet-text tweet-text\" lang=\"en\" data-aria-label-part=\"0\"> </p>" ;

    echo "<div class=\"stream-item-footer\">";

    echo "<ul class=\"tweet-actions js-actions">";

    echo "<li class=\"more-tweet-actions\">";
    echo "<div class=\"action-more-container\">";
    echo "<div class=\"dropdown\">";
    echo "<button type=\"button" class=\"btn-link with-icn dropdown-toggle js-dropdown-toggle js-tooltip\">";
    echo "<span class=\"Icon Icon--delete\"></span>";
    echo "<b>Delete</b>";
    echo "</button>";
    echo "<div class=\"dropdown-menu\">";
   echo "<div class=\"dropdown-caret\">";
echo "<div class=\"caret-outer\"></div>";
echo "<div class=\"caret-inner\"></div>";
echo "</div>";

echo "</div>";
echo "</div>";
echo "</div>";
echo "</li>";
echo "</ul>";
echo "</div>"; 
echo   "</div>";
echo   "</div>";
  • 写回答

5条回答 默认 最新

  • dsdfd2322 2014-10-21 14:40
    关注

    Check your single and double quotes. The strings are being returned are being truncated. This is invalid:

     echo   "<div class="stream-item-header">" ;
    

    Change it to:

    echo   "<div class=\"stream-item-header\">" ;
    

    Or:

    echo   "<div class='stream-item-header'>" ;
    

    And repeat this fix for each of your echo lines.

    For example, this:

    echo  "<div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-  popup-actionable js-original-tweet my-tweet" data-tweet-id="515380319977893890"data-item-id="515380319977893890"  data-name="" data-user-id="2665607941" data-expanded-footer="&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;" data-you-follow="false"data-you-block="false">" ;
    

    Becomes this:

    echo  "<div class=\"tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-  popup-actionable js-original-tweet my-tweet\" data-tweet-id=\"515380319977893890\" data-item-id=\"515380319977893890\"  data-name=\"\" data-user-id=\"2665607941\" data-expanded-footer=\"&lt;div class=&quot;js-tweet-details-fixer tweet-details-fixer&quot;&gt;&#10;&#10;&#10;&#10;    &lt;div class=&quot;entities-media-container js-media-container&quot; style=&quot;min-height:0px&quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;js-machine-translated-tweet-container&quot;&gt;&lt;/div&gt;&#10;    &lt;div class=&quot;js-tweet-stats-container tweet-stats-container &quot;&gt;&#10;    &lt;/div&gt;&#10;&#10;  &lt;div class=&quot;client-and-actions&quot;&gt;&#10;  &lt;span class=&quot;metadata&quot;&gt;&#10;    &lt;span&gt;11:00 PM - 25 Sep 2014&lt;/span&gt;&#10;&#10;       &amp;middot; &lt;a class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/status/515380319977893890&quot;  tabindex=&quot;-1&quot;&gt;Details&lt;/a&gt;&#10;    &#10;&#10;&#10;  &lt;/span&gt;&#10;&lt;/div&gt;&#10;&#10;&#10;&lt;/div&gt;&#10;\" data-you-follow=\"false\" data-you-block=\"false\">" ;
    

    For a more advanced trick, you can move your entire HTML to a separate file, an HTML template if you will. Then read in the file contents via PHP and find/replace the variables or dynamic parts as you need. This may be beyond the scope of your question however.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误