如何使用循环创建表并在其中打印变量使用PHP

时间:2015-09-03 07:02:50

标签: php mysql loops html-table

我是PHP新手。我想使用循环创建一个表,并希望打印从Mysql中获取的变量数据。在这里,我面临着创建表格的一些问题。

我想创建这样的表:

Generated PDF

但我没有这样做。

我的代码在这里:

\documentclass{article}
\begin{document}

<<results = "asis">>=
makeItNiceR <- function(x) {
  return(paste("\\fbox{\\texttt{", x, "}}"))
}

myoutput <- "slim"

cat("foo")
cat("\\emph{foo}")
cat(makeItNiceR("foo bar is nice"))
@

\paragraph{Outside of chunk:} ~\\

\Sexpr{myoutput} \\

Add formatting to \emph{\Sexpr{myoutput}} directly in LaTeX. \\

\Sexpr{makeItNiceR(paste(myoutput, "is nice"))}
\end{document}

我的代码输出是:

enter image description here

你很容易看到我的问题。

2 个答案:

答案 0 :(得分:0)

这是你想要的

    $q = "select * from letter_copy";
    $result = execute $q to get results



<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" border="1" cellpadding="10" cellspacing="0">
    <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>

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

        <?php
        $action = explode(",", $row->action);
        $long_term = explode(",", $row->long_term_rating);

        $short_term = explode(",", $row->short_term_rating);
        $outlook = explode(",", $row->outlook);

        for ($i = 0; $i < count($action); $i++) {

            ?>
            <tr>

                <td><?= $row->opinion ?></td>
                <td><?= $action[$i] ?></td>

                <td><?= $long_term[$i] ?></td>
                <td><?= $long_term[$i] ?></td>
                <td><?= $short_term[$i] ?></td>
                <td><?= $short_term[$i] ?></td>
                <td><?= $outlook[$i] ?></td>
                <td><?= $row->rating_type_title ?></td>
            </tr>

        <?php }
    }
    ?>

</table>

答案 1 :(得分:0)

我用这段代码做了

<!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>