【问题标题】:Displaying text in table PHP在表格 PHP 中显示文本
【发布时间】:2018-06-28 03:17:17
【问题描述】:

我想创建一个包含 3 列的表,其中一列用于值的名称,例如“追随者数量”,第二列用于代码中描述的 $tweetstimeline 值,第三列用于 '$ tweetstimeline_2' 值。

我尝试过使用<tr><td> 和<th>,但是我还是新手,没有得到正确的结果。有人可以提供解决方案吗?

echo "Number of Followers: ".$tweetstimeline[0]->user->followers_count;
echo "<br />\n";
echo "Name of Twitter Page: ".$tweetstimeline[0]->user->name;
echo "<br />\n";
echo "Location: ".$tweetstimeline[0]->user->location;
echo "<br />\n";
echo "Description: ".$tweetstimeline[0]->user->description;
echo "<br />\n";
echo "URL: ".$tweetstimeline[0]->user->url;
echo "<br />\n";
echo "Number of Friends: ".$tweetstimeline[0]->user->friends_count;
echo "<br />\n";
echo "Number of Statuses: ".$tweetstimeline[0]->user->statuses_count;
echo "<br />\n";
echo "<br />\n";


echo "Number of Followers: ".$tweetstimeline_2[0]->user->followers_count;
echo "<br />\n";
echo "Name of Twitter Page: ".$tweetstimeline_2[0]->user->name;
echo "<br />\n";
echo "Location: ".$tweetstimeline_2[0]->user->location;
echo "<br />\n";
echo "Description: ".$tweetstimeline_2[0]->user->description;
echo "<br />\n";
echo "URL: ".$tweetstimeline_2[0]->user->url;
echo "<br />\n";
echo "Number of Friends: ".$tweetstimeline_2[0]->user->friends_count;
echo "<br />\n";
echo "Number of Statuses: ".$tweetstimeline_2[0]->user->statuses_count;
echo "<br />\n";
echo "<br />\n";

【问题讨论】:

  • 输出的html是什么?
  • 您的tr, td, ths 在table 中吗?
  • 如果没有 &lt;tr&gt;&lt;td&gt;&lt;th&gt; 元素,我们可以在 HTML 中谈论表格。你没有表格,只有很多行纯文本。

标签: php html-table


【解决方案1】:

我认为您想要的表结构不正确。试试这样的:

<table>
    <thead>
        <tr>
            <td># Tweetstimeline</td>
            <td>Number of followers</td>
            <td>Name</td>
            <td>Location</td>
            <td>Description</td>
            <td>Url</td>
            <td>Friends Count</td>
            <td>Status count</td>       
        </tr>
    </thead>
    <tbody>
        <?php
            $rows = 2; //number of rows goes here
            for($i = 0 ; $i < $rows ; $i++){
                //output html row:
                echo "<tr>";
                    echo "<td>$i</td>";
                    echo "<td> $tweetstimeline[$i]->user->followers_count </td>";
                    echo "<td> $tweetstimeline[$i]->user->name </td>";
                    echo "<td> $tweetstimeline[$i]->user->location </td>";
                    echo "<td> $tweetstimeline[$i]->user->description </td>";
                    echo "<td> $tweetstimeline[$i]->user->url </td>";
                    echo "<td> $tweetstimeline[$i]->user->friends_count </td>";
                    echo "<td> $tweetstimeline[$i]->user->statuses_count </td>";
                echo "</tr>";
            }               
        ?>  
    </tbody>    
</table>

PS:对不起我的英语,我正在学习

【讨论】:

    【解决方案2】:

    尝试基本的html表结构:

    <table>
        <thead>
            <tr>
                <th>header 1</th>
                <th>header 2</th>
                <th>header 3</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>content 1</td>
                <td>content 2</td>
                <td>content 3</td>
            </tr>
        </tbody>
    </table>
    

    &lt;tbody&gt; 之后,您可以使用foreach 遍历所有元素。

    foreach ($tweetstimeline as $row) {
        echo "<tr>";
        echo "<td>{$row->user->name}</td>";
        echo "<td>{$row->user->location}</td>";
        echo "<td>{$row->user->description}</td>";
        echo "</tr>";
    }
    

    【讨论】:

      【解决方案3】:
      echo "<table style='width:100%'>";
      echo "<tr>";
      echo "<td>Number of Followers</td>";
      echo "<td>$tweetstimeline</td>      ";
      echo "<td>$tweetstimeline_2</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td>100</td>";
      echo "<td>wtfgoeshere?</td>";   
      echo "<td>and here?</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td>30</td>";
      echo "<td>wtfgoeshere</td>  ";  
      echo "<td>and here?</td>";
      echo "</tr>";
      echo "<tr>";
      echo "<td>60</td>";
      echo "<td>wtfgoeshere</td>  ";  
      echo "<td>and here?</td>";
      echo "</tr>";
      echo "</table>";
      

      尝试这样的事情作为起点,只要你觉得合适就做“code”+ varhere+“endcode”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        相关资源
        最近更新 更多