1

i want to display database to tables, but if the text that i want to display is too long, then the text will not directly go down. the text on 'question' not directly enter down. please help

this is my code:

<table class="table">
                        <thead>
                        <tr>
                         <th>No</th>
                         <th>Question</th>
                         <th>A</th>
                         <th>B</th>
                         <th>C</th>
                         <th>D</th>
                         <th>Answer</th>
                         <th>Button</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php 
                        include "conection.php";
                        $no = 1;
                        $query = mysql_query("SELECT * FROM grammar");
                        if ($query) {
                        while ($row = mysql_fetch_array($query)) {
                            echo "
                                <tr>
                                    <td>".$no."</td>
                                    <td>".$row['question']."</td>
                                    <td>".$row['a']."</td>
                                    <td>".$row['b']."</td>
                                    <td>".$row['c']."</td>
                                    <td>".$row['d']."</td>
                                    <td>".$row['answer']."</td>
                                    <td>
                                        <a href=\"update.php?id=\">Edit</a> |
                                        <a href=\"delete_grammar.php?id=\">delete</a>
                                    </td>
                                </tr>";
                            $no++;
                            }
                        }
                        ?>
                        </tbody>
                    </table>

this is the picture: enter image description here

Thamilhan
  • 13,040
  • 5
  • 37
  • 59
Endone
  • 57
  • 6

2 Answers2

1

Try using word-wrap: break-word; in your style. It makes the string goes down if ever the max widht is reached.

<table class="table">
                        <thead>
                        <tr>
                         <th>No</th>
                         <th>Question</th>
                         <th>A</th>
                         <th>B</th>
                         <th>C</th>
                         <th>D</th>
                         <th>Answer</th>
                         <th>Button</th>
                        </tr>
                        </thead>
                        <tbody>
                        <?php 
                        include "conection.php";
                        $no = 1;
                        $query = mysql_query("SELECT * FROM grammar");
                        if ($query) {
                        while ($row = mysql_fetch_array($query)) {
                            echo "
                                <tr>
                                    <td>".$no."</td>
                                    <td style='width:40%; word-wrap: break-word;'>".$row['question']."</td>
                                    <td>".$row['a']."</td>
                                    <td>".$row['b']."</td>
                                    <td>".$row['c']."</td>
                                    <td>".$row['d']."</td>
                                    <td>".$row['answer']."</td>
                                    <td>
                                        <a href=\"update.php?id=\">Edit</a> |
                                        <a href=\"delete_grammar.php?id=\">delete</a>
                                    </td>
                                </tr>";
                            $no++;
                            }
                        }
                        ?>
                        </tbody>
                    </table>

Here is the sample JSFiddle

Lee Balino
  • 490
  • 2
  • 12
0

The answer from Lee will get you the result you need but do note that all lengths of words can break that way if they reach a set width which may reduce readability. The screen shot you took puts that column as a question field. That question looks like a potentially unrealistic test of content but it is good to account for oddities. Email address or URL columns can have this issue of overflow problems and readability may not be as much of a need for a user. Most users probably want to be able to quickly read the question and broken words may make that difficult.

If possible when readability is still needed, you could wrap the content of that cell inside of a div that has overflow-x:auto or hidden and width 100% depending on desired result.

This will either cut off such possible rare instances or at least let the user scroll through the text.

The container td would need a specific width in pixels or other unit that isn't variable in order for tables to be handled correctly across all browsers. If you want you can also instead add a pixel width on the div rather than 100% width.

Nick Goodrum
  • 391
  • 1
  • 7