sunny sunny - 3 years ago 54
PHP Question

How to create table using loop and print variables in it | using PHP

I am new in PHP. I want to create a table using loop and wants to print variables data in it fetching from Mysql. Here, I face some problems for creating table.

I want to create table like this:

enter image description here

But I failed in doing so.

My code is here:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
table {
border-collapse: collapse;
}

table, td, th {
border: 1px solid black;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
ini_set('error_reporting', E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";

$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT * FROM `letter_copy` WHERE `id` = 1";

$conn->multi_query($sql);

//$conn->next_result();

$result = $conn->use_result();

echo $conn->error;
$row = $result->fetch_assoc();

$opinion = $row['opinion'];
$action = $row['action'];

$opinion_chunks = explode(",", $opinion);

?>
<div style="margin-top:auto; 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>
</table>
</div>
<?php

echo '<table border="01">';
foreach($opinion_chunks as $row){
echo '<tr>';
$row = explode(',',$row);
$row1 = explode(",", $action);
foreach($row as $cell){
if ($cell == "")
continue;
echo '<td>';
echo $cell;
echo '</td>';
foreach($row1 as $cell1){
if ($cell1 == "")
continue;
echo '<td>';
echo $cell1;
echo '</td>';
}
}
echo '</tr>';
}
echo '</table>';
?>

</body>
</html>


And the output of my code is:

enter image description here

You can easily seen my problem.

Answer Source

i did it by this code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
table {
    border-collapse: collapse;
}

table, td, th {
    border: 1px solid black;
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
ini_set('error_reporting', E_ALL);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pacra1";


$conn = new mysqli($servername, $username, $password, $dbname);

  $sql = "SELECT * FROM letter_copy WHERE id = 3"; 
  $conn->multi_query($sql); 
  //$conn->next_result(); 
  $result = $conn->use_result();

?>

<div style="margin-top:auto; width:auto;font-family:'Times New Roman', Times, serif; text-align:left; font-size:12px">
<table width="657">
    <tr>
        <td width="225"> <strong>Opinion</strong></td>
        <td width="62"> <strong>Action</strong></td>
        <td colspan="4" style="text-align:center"><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>

    <?php foreach ($result as $row) { ?>

        <?php
        //$a = $row['action'];
        $action = explode(",", $row['action']);
        $long_term = explode(",", $row['long_term_rating']);
        $p_long_term = explode(",", $row['p_long_term_rating']);
        $short_term = explode(",", $row['short_term_rating']);
        $p_short_term = explode(",", $row['p_short_term_rating']);
        $outlook = explode(",", $row['outlook']);
        $opinion = explode(",", $row['opinion']);
        $rating_type = explode(",", $row['rating_type']);

        for ($i = 0; $i < count($opinion); $i++) {
            if ($opinion[$i] == "")continue;

            ?>
            <tr>
                <td><?= $opinion[$i] ?> </td>
                <td><?= $action[$i] ?> </td>
                <td><?= $long_term[$i] ?>  </td>
                <td><?= $p_long_term[$i] ?>  </td>
                <td><?= $short_term[$i] ?></td>
                <td><?= $p_short_term[$i] ?></td>
                <td><?= $outlook[$i] ?> </td>
                <td><?= $rating_type[$i]?></td>
            </tr>

        <?php }
    }
    ?>

</table>
</div>
</body>
</html>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download