dqk94069 2018-03-05 17:25
浏览 132
已采纳

一个数据库。 一个领域。 在html动态表中。 无法提取有限数量的数据

enter image description hereHere's the deal. I'm a beginner in PHP, MySQL and HTML. I want to copy part of a database field and insert it into an html dinamic table (by dinamic I mean that the number of rows grows depending from the amount of rows in the database table). Let's say that I already limited the width of the cell in the html table in which I want to insert the data by a certain percentage of width. The problem lies in the copying. How can I copy the necessary amount of symbols so that it won't make my cell grow in height but stay only in a height equal to one line? Before copying, I want to ensure that there is "..." at the end of the line, right where I cut the text. One important detail. I'm working both with russian and english language here. My database fields that have text have "utf8-general-ci" collation. But I already use mysqli_set_charset and set cp1251 when ENTERING data into my database and mysqli_set_charset with utf8 when READING data from it.

Here is the php file where I am trying to read the data into the html table. Hope you can help me. Especially with the encoding problem. The current php file makes a � symbol at the end of the line, which is very troubling.

Hope you can help me.

    <?php
  include_once 'Connection.php';
  ?>


 <!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        table, th {
        border: 1px solid black;
       }
    </style>
</head>



<body>
    <h1 style="Text-align: center;">Существующие работы </h1><br><br><br>

<table style="width:100%">
     <tr>
        <th style="width:13%">Работодатель</th>
        <th>Описание работы</th>
        <th style="width:8%">Оплата</th>
        <th style="width:8%">Количество людей</th>
        <th style="width:10%">Срочность</th>
        <th style="width:8%">Сложность</th>     
        </tr>
   </table>


 <?php
    $sql = "SELECT * FROM request_data;";
    $conn = Connect();
    mysqli_set_charset($conn,'utf8');
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<table style = "width:100%">
            <tr>
              <th style="width:13%">'.$row['ID'].'</th>
              <th>'.substr($row['The_Job'],0,145).'...'.'</th>
              <th style="width:8%">'.$row['Paymentfro'].'-'.$row['Paymentto'].'</th>
              <th style="width:8%">'.$row['Amount'].'</th>
              <th style="width:10%">'.$row['Urgency'].'</th>
              <th style="width:8%">'.$row['Difficulty'].'</th>      
            </tr>
          </table>';
    }
   }

  ?>

 </body>

    </html>
  • 写回答

2条回答 默认 最新

  • doudu5498 2018-03-05 17:54
    关注

    This would be achieved with css (don't need substr nor mb_substr, let css add ellipses).

    For that second td, you add a class defined as:

    <style>
    .job-detail {
        width: 53%; /* change this to the width you need */
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        }
    </style>
    

    That will do the automatic ellipsis when that one cell reaches beyond the bounds.

    And then set the class on your td:

    while ($row = mysqli_fetch_assoc($result)) {
        echo '<tr>
              <td style="width:13%">'. $row['ID'] .'</td>
              <td class="job-detail">'. $row['The_Job'] .'</td>
              <td style="width:8%">'. $row['Paymentfro'] .'-'.$row['Paymentto'].'</td>
              <td style="width:8%">'. $row['Amount'] .'</td>
              <td style="width:10%">'. $row['Urgency'] .'</td>
              <td style="width:8%">'. $row['Difficulty'] .'</td>      
            </tr>';
    }
    

    NOTE Your second question about encoding, should be made as a second question since it is entirely unrelated to your first question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等