如何使用LEFT()函数完成从数据库中选择的单词

时间:2018-06-16 09:45:50

标签: php mysqli

我正在尝试使用LEFT()函数从数据库中获取字段。我从描述字段中获取前20个字符。它获取并获得了结果,但有时候像这样得到了不完整的词 - 你过去的一些问题

所以,我想要显示这样的完整单词 - 你过去的一些问题

我知道我取了前20个字,这就是为什么我拿不完整的字。但是,如果有任何解决方案可用于获取完整的单词,请建议我。

<?php
    $query=mysqli_query($conn, "SELECT *, LEFT(descripation,20) AS descripation, LEFT(title,20) AS title FROM event WHERE status='Y'") or die("Could not retrieve image: " .mysqli_error($conn));
    if (mysqli_num_rows($query) > 0){
        while($row = mysqli_fetch_assoc($query)){
            ?>
                <!--Event Thumb Start-->
                <div class="col-md-4 col-sm-6">
                    <div class="msl-event">
                        <figure>
                            <img class="img-responsive" src="admin_panel/event/<?php echo $row['image'];?>">
                            <div class="theme-bg date-box">
                                <?php 
                                    echo $newDate = date("F d,Y", strtotime($row['date']));
                                ?>
                            </div>
                        </figure>
                        <div class="text white_bg">                                             
                            <h4 class="event-title"><a href="event-detail.php?id=<?php echo ($row['id']);?>"><?php echo $row['title'];?>...</a></h4>
                            <p><?php echo $row['descripation'];?>...</p>
                        </div>
                    </div>
                </div>
                <!--Event Thumb End-->
            <?php
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

如何选择30个字符然后修剪字符串的最后一个字?

<?php
    $query=mysqli_query($conn, "SELECT *, LEFT(description,30) AS description, LEFT(title,20) AS title FROM event WHERE status='Y'") or die("Could not retrieve image: " .mysqli_error($conn));
    if (mysqli_num_rows($query) > 0){
        while($row = mysqli_fetch_assoc($query)){
            //trim description
            $row['description'] = substr($row['description'], 0, strripos($row['description'], " ");
            //trim description
            ?>
                <!--Event Thumb Start-->
                <div class="col-md-4 col-sm-6">
                    <div class="msl-event">
                        <figure>
                            <img class="img-responsive" src="admin_panel/event/<?php echo $row['image'];?>">
                            <div class="theme-bg date-box">
                                <?php 
                                    echo $newDate = date("F d,Y", strtotime($row['date']));
                                ?>
                            </div>
                        </figure>
                        <div class="text white_bg">                                             
                            <h4 class="event-title"><a href="event-detail.php?id=<?php echo ($row['id']);?>"><?php echo $row['title'];?>...</a></h4>
                            <p><?php echo $row['description'];?>...</p>
                        </div>
                    </div>
                </div>
                <!--Event Thumb End-->
            <?php
        }
    }
?>

FYI描述拼写描述。