Php - PostgreSQL echo data into one cell (HTML Table)

时间:2017-12-18 08:07:09

标签: php html-table postgresql-9.1

I'm trying to echo data from a database in the format shown below;

 <th>
        <td><input type="radio" value="<?php echo $row['opt1']. ?>'<br>';/></td>

        </th>

That means, the data I'm selecting from the database is such that the people in column 4, 5 received the message (column-3) from the person on column-1. But according to my code that echoes data from the db, I have a new column for every other person on column 4 and 5 as shown below;

|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name |  Date  |Message | People |Phone #s|
                           | People |Phone #s|
                           | People |Phone #s|
                           | People |Phone #s|
                           | People |Phone #s|

Below is my php code that generates the table;

|column-1|column-2|column-3|column-4|column-5|
|--------|--------|--------|--------|--------|
|My Name |  Date  |Message | People |Phone #s|
|My Name |  Date  |Message | People |Phone #s|
|My Name |  Date  |Message | People |Phone #s|
|My Name |  Date  |Message | People |Phone #s|
|My Name |  Date  |Message | People |Phone #s|

So the question is how can I output column 4 and 5 data into a single cell or alternatively, echo out the data into different cells without repeating columns 1 to 3?

1 个答案:

答案 0 :(得分:1)

A typical way to handle your problem is to build a small state machine which keeps track of change in the column values. In your case, it appears that if any of the values of the first three columns change, then you want to print a full record. Otherwise, just print the last two columns. The trick used below is that we always drop 5 rem Bypass "Terminate Batch Job" prompt. if "%~1"=="-FIXED_CTRL_C" ( REM Remove the -FIXED_CTRL_C parameter SHIFT ) ELSE ( REM Run the batch with <NUL and -FIXED_CTRL_C CALL <NUL %0 -FIXED_CTRL_C %* GOTO :EOF ) tags down, but for repeat rows we only assign empty string into those cells.

<td>

Important: This answer will only work, and only makes sense, if your Postgres query sorted the result set using $col1 = NULL; $col2 = NULL; $col3 = NULL; echo "<table id='table'>"; while ($row = pg_fetch_assoc($result)) { $text1 = ''; $text2 = ''; $text3 = ''; if ($row['message_by'] != $col1 || $row['message_date'] != $col2 || $row['message_text'] != $col3) { $col1 = $row['message_by']; $col2 = $row['message_date']; $col3 = $row['message_text']; $text1 = $col1; $text2 = $col2; $text3 = $col3; } echo "<tr>"; echo "<td align='left' width='200'>" . $text1 . "</td>"; echo "<td align='left' width='200'>" . $text2 . "</td>"; echo "<td align='left' width='200'>" . $text3 . "</td>"; echo "<td align='left' width='200'>" . $row['phone_number'] . "</td>"; echo "<td align='left' width='200'>" . $row['recipient_name'] . "</td>"; echo "</tr>"; } echo "</table>"; . In particular, the query should have been ordered with the three message columns being sorted first.