Javascript Have description限制页面上的字符数。PHP
好的,我在一个PHP网站上工作,我在图片下有产品的描述,我需要做的是限制页面上的字符数量,并添加一个单击此处或。。让观众直接进入该产品页面以查看完整描述。仅供参考,这是我到目前为止对PHP非常陌生的内容。所以问题是我是使用PHP还是javascript,或者两者都使用,以及我如何做到这一点Javascript Have description限制页面上的字符数。PHP,javascript,php,sql,Javascript,Php,Sql,好的,我在一个PHP网站上工作,我在图片下有产品的描述,我需要做的是限制页面上的字符数量,并添加一个单击此处或。。让观众直接进入该产品页面以查看完整描述。仅供参考,这是我到目前为止对PHP非常陌生的内容。所以问题是我是使用PHP还是javascript,或者两者都使用,以及我如何做到这一点 <?php // Include need php scripts require_once ("Includes/simplecms-config.php"); require_once ("Incl
<?php
// Include need php scripts
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");
include ("Includes/header.php");
if (!empty($_GET['cat'])) {
$category = $_GET['cat'];
$query = mysqli_query($db, "SELECT * FROM products WHERE category = '".$category."'");
} else {
$query = mysqli_query($db, "SELECT * FROM products");
}
if (!$query) {
die('Database query failed: ' . $query->error);
}
?>
<section>
<div id="productList">
<?php
$row_count = mysqli_num_rows($query);
if ($row_count == 0) {
echo '<p style="color:red">There are no images uploaded for this category</p>';
} elseif ($query) {
while($products = mysqli_fetch_array($query)){
$file = $products['image'];
$product_name = $products['product'];
$image_id = $products['id'];
$price = $products['price'];
$desc = $products['description'];
echo '<div class="image_container">';
echo '<a href="viewProduct.php?id=' . $image_id . '"><p><img src="Images/products/'.$file.'" alt="'.$product_name.'" height="250" /></p>';
echo $product_name . "</a><br>$" . $price . "<br>" . $desc;
echo '</div>';
if (is_admin()){
echo "<a href='deleteproduct.php'><button>delete</button></a>";
}
}
} else {
die('There was a problem with the query: ' .$query->error);
}
mysqli_free_result($query);
?>
</div>
</section>
<?php include ("Includes/footer.php"); ?>
使用strlen和substr我们可以实现这一点
$length = 150
$x = 'string';
if(strlen($x)<=$length)
{
echo $x;
}
else
{
$y=substr($x,0,$length) . '...';
echo $y;
}
这看起来并不粗鲁,但您可能想进一步学习PHP和Javascript。Udemy有一些很棒的便宜菜。。还有其他人在那里。。我决不赞成我的。。但在我参加的许多在线课程中,它们是最好的。。除非你想花几千