在php中获取标签

时间:2015-03-30 07:16:16

标签: php html

我想在我的php中获取一些div标签。正在运行的原始代码如下所示:

echo "Date: ".$row{'date'}."<br>"."Day: ".$row{'day'}."<br>"."From Time: ".$row{'fromtime'}."<br>"."To Time: ".$row{'totime'}; //display the results

当我运行此代码时,数据将低于彼此。我希望数据库中的每个输出都连续出现在一些div标签中,所以我可以控制它。我试图制作它,但我真的怀疑语法是什么?

<?php

echo "<div class="column1">""Date: ".$row{'date'}"</div>";
echo "<div class="column2">""Day: ".$row{'day'}"</div>";
echo "<div class="column3">""From Time: ".$row{'fromtime'}"</div>";
echo "<div class="column4">""To Time": ".$row{'totime'}"</div>";
?>

这种剂量有效。有谁知道我怎么解决这个问题?

新代码:

非常感谢您的回答。我实际上是想放一张桌子。当我打印出div时,它看起来像这样: http://postimg.org/image/jq60jujv1/

所以我想这会是

//fetch the data from the database 
Print"<h3>Return from database:</h3>"."<br>";

print '<table border="1">';
print '<tr>';
print '<td>date</td>';
print '<td>day</td>';
print '<td>fromtime</td>';
print '<td>totime</td>';
print '</tr>'."<br>";
print '</table>'."<br>";
while ($row = mysql_fetch_array($result)) {

    print "<tr>"."<td>"."Date: ".$row{'date'}."</td>"."</tr>";
    print "<tr>"."<td>"."Day: ".$row{'day'}."</td>"."</tr>";
    print "<td>"."From Time: ".$row{'fromtime'}."</td>";
    print "<td>"."To Time: ".$row{'totime'}."</td>"."<br>";

}

或? 最好的祝福 MADS

3 个答案:

答案 0 :(得分:1)

试试这个而不是你的,

<?php
echo "<div class='column1'>"."Date: ".$row{'date'}."</div>";
echo "<div class='column2'>"."Day: ".$row{'day'}."</div>";
echo "<div class='column3'>"."From Time: ".$row{'fromtime'}."</div>";
echo "<div class='column4'>"."To Time: ".$row{'totime'}."</div>";
?>

答案 1 :(得分:0)

试试这个

<?php
echo "<div class='column1'>Date: ".$row{'date'}."</div>
      <div class='column2'>Day: ".$row{'day'}."</div>
      <div class='column3'>From Time: ".$row{'fromtime'}."</div>
      <div class='column4'>To Time: ".$row{'totime'}."</div>";
?>

编辑: 如果您希望输出为表

<table>
    <tr>
        <th>Date</th>
        <th>Day</th>
        <th>From Time</th>
        <th>To Time</th>
    </tr>   
<?php
    foreach($result as $row){
      echo "<tr><td>".$row{'date'}."</td>
          <td>".$row{'day'}."</td>
          <td>".$row{'fromtime'}."</td>
          <td>".$row{'totime'}."</td></tr>";
    }
?>

</table>

答案 2 :(得分:0)

由于您用于echo ".."的包装引号,您不能在该字符串中使用相同的引号:

echo "<div class="column1">""Date: ".$row{'date'}"</div>";
                 ^--- string ended

您可以通过多种方式解决此问题,已经建议在内部使用单引号,但您也可以逃避当前使用的那些:

echo "<div class=\"column1\">Date: ".$row{'date'}."</div>";
                 ^        ^

你可以通过不使用echo来输出所有标记来避免所有这些问题,而是开始/结束php标记你想要打印的地方
变量或做一些逻辑:

<div class="column1">Date: <?php echo $row{'date'}; ?></div>

输出为表格

HTML表使用<table></table>标记, <tr>是表格行<th> 常规表格单元格的标题和<td>

<?php
/* the code block where you connect to db etc.. */
?>
<table>
  <!-- headers -->
  <tr>
    <th>Date</th>
    <th>Day</th>
    <th>From</th>
    <th>To</th>
  </tr>
  <!-- Now a row for each new set of data, here you probably need to
    loop through some data set you retrieve from the database -->
  <?php while($row = mysql_fetch_array($result)): ?>
  <tr>
    <td><?php echo $row{'date'};?></td>
    <td><?php echo $row{'day'};?></td>
    <td><?php echo $row{'fromtime'};?></td>
    <td><?php echo $row{'totime'};?></td>
  </tr>
  <?php endwhile; ?>
</table>