【问题标题】:How to create HTML table using loop which display Mysql data using php?如何使用循环创建 HTML 表,使用 php 显示 Mysql 数据?
【发布时间】:2015-11-17 11:13:35
【问题描述】:

我是 php 新手。我想使用循环创建一个表。我有一个选择查询,我希望该查询的结果显示在表中。我的表中有多个列。

<?php
$con=mysqli_connect("localhost","root","","pacra1");

        // $id2 = $_GET['id'];
        $sql = "SELECT pc.id, l.id, l.to_name, l.to_designation, l.company, l.address, l.confidential, l.date, l.rating_type_title, l.opinion_type, l.dear_sir, l.company_id, l.first_opinion_name, l.first_opinion_rating_type, l.first_opinion_action, l.first_opinion_outlook, l.first_opinion_long_term, l.first_opinion_p_long_term, l.first_opinion_short_term, l.first_opinion_p_short_term, l.second_opinion_name, l.second_opinion_rating_type, l.second_opinion_action, l.second_opinion_outlook, l.second_opinion_long_term, l.second_opinion_p_long_term, l.second_opinion_short_term, l.second_opinion_p_short_term, l.third_opinion_name, l.third_opinion_rating_type, l.third_opinion_action, l.third_opinion_outlook, l.third_opinion_long_term, l.third_opinion_p_long_term, l.third_opinion_short_term, l.third_opinion_p_short_term, l.forth_opinion_name, l.forth_opinion_rating_type, l.forth_opinion_action, l.forth_opinion_outlook, l.forth_opinion_long_term, l.forth_opinion_p_long_term, l.forth_opinion_short_term, l.forth_opinion_p_short_term, l.y_truly, l.s_name, l.uh1, l.uh2, l.uh1_designation, l.uh2_designation, l.s_designation, l.chk, l.chk1

FROM letter l
LEFT JOIN pacra_clients pc
ON l.company_id = pc.id
ORDER BY l.id DESC
LIMIT 1";

            $result=mysqli_query($con,$sql);

            $row= (mysqli_fetch_array($result,MYSQLI_ASSOC));
    $id= $row['id'];    
    $to_name= $row['to_name'];
    $to_designation= $row['to_designation'];
    $company= $row['company'];
    $address= $row['address'];
    $confidential = $row['confidential'];
    $date = $row['date'];
$phpdate = strtotime( $date );
$date = date( 'M d, Y', $phpdate );
    $rating_type_title= $row['rating_type_title'];
    $opinion_type= $row['opinion_type'];
    $dear_sir= $row['dear_sir'];
    $company_id= $row['company_id'];

    $first_opinion_name= $row['first_opinion_name'];
    $first_opinion_rating_type= $row['first_opinion_rating_type'];
    $first_opinion_action= $row['first_opinion_action'];
    $first_opinion_outlook= $row['first_opinion_outlook'];
    $first_opinion_long_term= $row['first_opinion_long_term'];
    $first_opinion_p_long_term= $row['first_opinion_p_long_term'];
    $first_opinion_short_term= $row['first_opinion_short_term'];
    $first_opinion_p_short_term= $row['first_opinion_p_short_term'];

    $second_opinion_name= $row['second_opinion_name'];
    $second_opinion_rating_type= $row['second_opinion_rating_type'];
    $second_opinion_action= $row['second_opinion_action'];
    $second_opinion_outlook= $row['second_opinion_outlook'];
    $second_opinion_long_term= $row['second_opinion_long_term'];
    $second_opinion_p_long_term= $row['second_opinion_p_long_term'];
    $second_opinion_short_term= $row['second_opinion_short_term'];
    $second_opinion_p_short_term= $row['second_opinion_p_short_term'];

    $third_opinion_name= $row['third_opinion_name'];
    $third_opinion_rating_type= $row['third_opinion_rating_type'];
    $third_opinion_action= $row['third_opinion_action'];
    $third_opinion_outlook= $row['third_opinion_outlook'];
    $third_opinion_long_term= $row['third_opinion_long_term'];
    $third_opinion_p_long_term= $row['third_opinion_p_long_term'];
    $third_opinion_short_term= $row['third_opinion_short_term'];
    $third_opinion_p_short_term= $row['third_opinion_p_short_term'];

    $forth_opinion_name= $row['forth_opinion_name'];
    $forth_opinion_rating_type= $row['forth_opinion_rating_type'];
    $forth_opinion_action= $row['forth_opinion_action'];
    $forth_opinion_outlook= $row['forth_opinion_outlook'];
    $forth_opinion_long_term= $row['forth_opinion_long_term'];
    $forth_opinion_p_long_term= $row['forth_opinion_p_long_term'];
    $forth_opinion_short_term= $row['forth_opinion_short_term'];
    $forth_opinion_p_short_term= $row['forth_opinion_p_short_term'];

    $y_truly= $row['y_truly'];
    $s_name= $row['s_name'];
    $s_designation= $row['s_designation'];
    $chk = $row['chk'];
    $chk1 = $row['chk1'];
    $uh1 = $row['uh1'];
    $uh2 = $row['uh2'];
    $uh1_designation = $row['uh1_designation'];
    $uh2_designation = $row['uh2_designation'];

    if ($s_designation == "coo"){
 $s_designation ="Chief Operating Officer";
}
elseif ($s_designation == "uh") {
    $s_designation ="Unit Head";
}


    ?>

和 HTML

<div style=" font-family: 'Times New Roman', Times, serif;">

    <div style=" margin:auto; width:60px; height:auto; align: middle">
    <img src="image/pacra_logo.png" alt="logo">
    </div>
    <div style="margin:auto; width:auto; text-align:center; font-family:'Times New Roman', Times, serif; font-variant: small-caps; font-size:20px; font-weight:bold">
    The Pakistan Credit Rating Agency Limited 
    <hr>
    </div>
    </div>
<div style="margin-top: 7px; width: auto; font-family:'Times New Roman', Times, serif; float:left">NL FY <?php echo date('y') ?> - <?php echo $id ?>
   </div>
     <div style="clear: both;"></div>

 <div style=" margin-top:20px; width:250px; float:left; font-family:'Times New Roman', Times, serif; text-align:left; font-size:14px; l">
<b> <?php echo $to_name?>  </b> <br /><?php echo $to_designation?> </br> <?php echo $company?> </br> <?php echo $address ?>
 </div> 

 <div style=" margin-top:20px; margin-left:300px; width:auto; float:left; font-family:'Times New Roman', Times, serif; font-size:14px;">
 <b> <u> <?php echo $confidential ?> </u> </b> </br> <?php echo $date ?>
 </div>
  <div style="clear: both;"></div>
 <div style="margin-top:20px; margin-right:auto; width:auto; font-family:'Times New Roman', Times, serif; text-align:center; font-variant:small-caps; font-size:18px; font-weight:bold; color:#009"> <?php echo $company?> <br /> Ratings - <?php
 if ($opinion_type == "up") {
     echo "Update";}
     elseif ($opinion_type =="in") {
         echo "Initial";}
  ?>
 </div>
 <div style="margin-top:10px; width:auto; float:left; font-family:'Times New Roman', Times, serif">
 <?php echo $dear_sir?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:10px; width:auto; float:left; font-family:'Times New Roman', Times, serif; text-align:justify">
<?php
if ($opinion_type == "up") {
    echo "This is with the reference to"; echo ' '; echo "ratings of"; echo ' '; echo $company ; 
    echo ". PACRA has updated its opinions, following are the respective details.";
} elseif ($opinion_type == "in") {
    echo "This is with the refrence to "; echo $rating_type_title; echo " ratings of "; echo $company ; echo ". PACRA assign its opinions, following are the rspective detail";
} 
?>
</div>

<div style="clear: both;"></div>
<div style="margin-top:20px; width:auto; align:middle; font-family:'Times New Roman', Times, serif">
<!--<table width="100%">
        <tr>  <td colspan="03"> <b> Name:</b> </td>
                <td width="429"><b> <?php //echo $company?></b> </td>
         </tr>
</table> -->
</div>
<div style="margin-top:40px; width:auto;font-family:'Times New Roman', Times, serif; text-align:left; font-size:12px; text-align:center">

<table width="657">
        <tr>
            <td width="225"> <strong>Opinion</strong></td>
            <td width="62"> <strong>Action</strong></td>
            <td colspan="4"><strong>Ratings</strong></td>
            <td width="54"><strong>Outlook</strong></td>
            <td width="67"><strong>Rating Type</strong></td>
        </tr>
        <tr>
          <td width="225">&nbsp;</td>
          <td width="62">&nbsp;</td>
          <td colspan="2"><b>Long Term</b></td>
          <td colspan="2"><b>Short Term</b></td>
          <td width="54">&nbsp;</td>
          <td width="67">&nbsp;</td>
        </tr>
        <tr>
          <td width="225">&nbsp;</td>
          <td width="62">&nbsp;</td>
          <td width="52"><b>Current</b></td>
          <td width="45"><b>Previous</b></td>
          <td width="49"><b>Current</b></td>
          <td width="51"><b>Previous</b></td>
          <td width="54">&nbsp;</td>
          <td width="67">&nbsp;</td>
        </tr>
        <tr>
          <td><?php echo $first_opinion_name?></td>
          <td><?php echo $first_opinion_action ?></td>
          <td><?php echo $first_opinion_long_term ?></td>
          <td><?php echo $first_opinion_p_long_term?></td>
          <td><?php echo $first_opinion_short_term ?></td>
          <td><?php echo $first_opinion_p_short_term ?></td>
          <td><?php echo $first_opinion_outlook ?></td>
          <td><?php echo $first_opinion_rating_type ?></td>
        </tr>
        <tr>
           <td><?php echo $second_opinion_name?></td>
          <td><?php echo $second_opinion_action ?></td>
          <td><?php echo $second_opinion_long_term ?></td>
          <td><?php echo $second_opinion_p_long_term?></td>
          <td><?php echo $second_opinion_short_term ?></td>
          <td><?php echo $second_opinion_p_short_term ?></td>
          <td><?php echo $second_opinion_outlook ?></td>
          <td><?php echo $second_opinion_rating_type ?></td>
        </tr>
        <tr>
           <td><?php echo $third_opinion_name?></td>
          <td><?php echo $third_opinion_action ?></td>
          <td><?php echo $third_opinion_long_term ?></td>
          <td><?php echo $third_opinion_p_long_term?></td>
          <td><?php echo $third_opinion_short_term ?></td>
          <td><?php echo $third_opinion_p_short_term ?></td>
          <td><?php echo $third_opinion_outlook ?></td>
          <td><?php echo $third_opinion_rating_type ?></td>
        </tr>
        <tr>
          <td><?php echo $forth_opinion_name?></td>
          <td><?php echo $forth_opinion_action ?></td>
          <td><?php echo $forth_opinion_long_term ?></td>
          <td><?php echo $forth_opinion_p_long_term?></td>
          <td><?php echo $forth_opinion_short_term ?></td>
          <td><?php echo $forth_opinion_p_short_term ?></td>
          <td><?php echo $forth_opinion_outlook ?></td>
          <td><?php echo $forth_opinion_rating_type ?></td>
        </tr>


</table>
</div>
<div style="clear: both;"></div>
<div style="margin-top:50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif">
<?php echo $y_truly ?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:70px; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
<?php 
if ($s_name == "shahzad"){
 echo '<b>'; echo "(Muhammad Shahzad Saleem)"; echo '</b>' ;
 echo '<br/>'; 
 echo $s_designation;
}

  ?> 
  </div>
  <div style="margin-top:50px; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
  <?php
if ($s_name == "hanif") {
    echo '<b>'; echo "(Muhammad Jhangeer Hanif)"; echo '</b>' ;
    echo '<br/>'; 
 echo $s_designation;

}
elseif ($s_name == "rana") {
    echo '<b>'; echo "(Rana Muhammad Nadeem)"; echo '</b>' ;
    echo '<br/>'; 
 echo $s_designation;

}
  ?>
</div>
<div style="margin-top:50px; text-align:left; font-family:'Times New Roman', Times, serif; float:left; margin-left:220px">
<?php
if ($s_name == "rana") {
    echo '<b>'; echo "(Muhammad Jhangeer Hanif)"; echo '</b>' ;
    echo '<br/>'; 
 echo $s_designation;

}
elseif ($s_name == "hanif") {
    echo '<b>'; echo "(Rana Muhammad Nadeem)"; echo '</b>' ;
    echo '<br/>'; 
 echo $s_designation;

}
  ?> 
</div>
<div style="margin-top:50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
<?php if ($s_designation == "coo"){
 echo "Chief Operating Officer";
}
elseif ($s_designation == "uh") {
    echo "Unit Head";
}
?>
</div>

<div style="margin-top:auto; width:auto; text-align:left; font-family:'Times New Roman', Times, serif; float:left; margin-left:362px">
<?php if ($s_designation == "uh") {
    echo "Unit Head";
}
?>
</div>
<div style="clear: both;"></div>
 <div style="margin-top: 50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif">
 <?php if ($chk == "p_release"){
 echo "Encl: 1) Press Release";
 }
 echo '</br>';
 if ($chk1 == "r_report"){

 echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; echo  "Rating Report";

}
?> 



 </div>

我为什么要这个?

因为我通过我的数据库在表格中显示我的记录。有时只有两行表的记录。我静态地创建了四行表。如果有两行记录,那么我的两行铰孔将显示为空。我想根据我的数据库中的数据创建我的表。

我不知道我该怎么做?

你能帮帮我吗?

【问题讨论】:

    标签: php mysql loops select html-table


    【解决方案1】:
     use foreach loop after mysql fetch query.    
    
    <?php
       foreach($row as $rows)
       {
           $id= $rows['id'];    
           $to_name= $rows['to_name'];
           $to_designation= $rows['to_designation'];
           $company= $rows['company'];
           $address= $rows['address'];
           $confidential = $rows['confidential'];
           $date = $rows['date'];
           $phpdate = strtotime( $date );
           $date = date( 'M d, Y', $phpdate );
           $rating_type_title= $rows['rating_type_title'];
           $opinion_type= $rows['opinion_type'];
           $dear_sir= $rows['dear_sir'];
           $company_id= $rows['company_id'];
    
           $first_opinion_name= $rows['first_opinion_name'];
           $first_opinion_rating_type= $rows['first_opinion_rating_type'];
           $first_opinion_action= $rows['first_opinion_action'];
           $first_opinion_outlook= $rows['first_opinion_outlook'];
           $first_opinion_long_term= $rows['first_opinion_long_term'];
           $first_opinion_p_long_term= $rows['first_opinion_p_long_term'];
           $first_opinion_short_term= $rows['first_opinion_short_term'];
           $first_opinion_p_short_term= $row['first_opinion_p_short_term'];
       }
    
      ?>
    

    【讨论】:

    • 那怎么建表呢?
    • 你在问哪张桌子
    • 我在其中回显我的数据的 HTML 表格
    • 在表格标签前使用foreach循环,然后关闭foreach循环
    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 2015-03-16
    • 2016-03-10
    • 2021-10-23
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多