php经典代码
㈠ php输出1到100的奇数完整代码
以下是 PHP 输出 1 到 100 的奇数的完整代码:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 2 != 0) {
echo $i . " ";
}
}
?>
代码的执行过程如下:
使用 for 循环从 1 到 100 遍历并扮每个数。
判断当前数是否为奇数桥返,如果是,使用 echo 函数输出该数。
循环结束,输出所有的奇数。敏蔽饥
输出结果为:1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99
Regenerate response
㈡ 请 php 简单 产品分类代码
商品分类展示
设置商品分类显示不仅可使该购物系统的所有商品都分门别类的显示出来,而且为用户选择商品提供了很大的方便。首先应该建立一个单独的type表用来存储商品大类,之后在shangpin表中增加一个typeid字段,该字段中存储的内容是商品大类id值,利用这个值就可以确定该商品属于那一类。商品分类展示是在showfenlei.php中完成的,代码如下:
<!--*******************************showfenlei.php*******************************-->
<?php
include("top.php");
?>
<table width="800" height="438" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="200" height="438" valign="top" bgcolor="#E8E8E8"><div align="center">
<?php include("left.php");?>
</div></td>
<td width="10" background="images/line2.gif"></td>
<td width="590" valign="top"><table width="590" height="20" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="left">
<?php
$sql=mysql_query("select * from type order by id desc",$conn);
$info=mysql_fetch_object($sql);
if($info==false)
{
echo "本站暂无商品!";
}
else
{
do
{
echo "<a href='showfenlei.php?id=".$info->id."'>".$info->typename."</a>";
}
while($info=mysql_fetch_object($sql));
}
?>
</div></td>
</tr>
</table>
<?php
if($_GET[id]=="")
{
$sql=mysql_query("select * from type order by id desc limit 0,1",$conn);
$info=mysql_fetch_array($sql);
$id=$info[id];
}
else
{
$id=$_GET[id];
}
$sql1=mysql_query("select * from type where id=".$id."",$conn);
$info1=mysql_fetch_array($sql1);
$sql=mysql_query("select count(*) as total from shangpin where typeid='".$id."' order by addtime desc ",$conn);
$info=mysql_fetch_array($sql);
$total=$info[total];
if($total==0)
{
echo "<div align='center'>本站暂无该类产品!</div>";
}
else
{
?>
<table width="550" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="left"><span style="color: #666666; font-weight: bold"><span style="color: #000000">本类商品>></span><?php echo $info1[typename];?></span>
</div></td>
</tr>
</table>
<table width="550" height="10" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td background="images/line1.gif"></td>
</tr>
</table>
<table width="550" height="70" border="0" align="center" cellpadding="0" cellspacing="0">
<?php
$pagesize=10;
if ($total<=$pagesize)
{
$pagecount=1;
}
if(($total%$pagesize)!=0)
{
$pagecount=intval($total/$pagesize)+1;
}
else
{
$pagecount=$total/$pagesize;
}
if(($_GET[page])=="")
{
$page=1;
}
else
{
$page=intval($_GET[page]);
}
$sql1=mysql_query("select * from shangpin where typeid=".$id." order by addtime desc limit ".($page-1) *$pagesize.",$pagesize ",$conn);
while($info1=mysql_fetch_array($sql1)) //显示商品信息
{
?>
……
<?php
}
?>
</table>
<table width="550" height="25" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="right"> 本站共有该类商品
<?php
echo $total;
?>
件每页显示<?php echo $pagesize;?>件第<?php echo $page; ?> 页/共<?php echo $pagecount; ?>页
<?php
if($page>=2) //商品分页显示
{
?>
<a href="showfenlei.php?id=<?php echo $id;?>&page=1" title="首页"><font face="webdings"> 9 </font></a>
<a href="showfenlei.php?id=<?php echo $id;?>&page=<?php echo $page-1;?>" title="前一页"><font face="webdings"> 7 </font></a>
<?php
}
if($pagecount<=4){
for($i=1;$i<=$pagecount;$i++){
?>
<a href="showfenlei.php?id=<?php echo $id;?>&page=<?php echo $i;?>"><?php echo $i;?></a>
<?php
}
}
else
{
for($i=1;$i<=4;$i++){
?>
<a href="showfenlei.php?id=<?php echo $id;?>&page=<?php echo $i;?>"><?php echo $i;?></a>
<?php
}
?>
<a href="showfenlei.php?id=<?php echo $id;?>&page=<?php echo $page-1;?>" title="后一页"><font face="webdings"> 8 </font></a>
<a href="showfenlei.php?id=<?php echo $id;?>&page=<?php echo $pagecount;?>" title="尾页"><font face="webdings"> : </font></a>
<?php
}
?>
</div></td>
</tr>
</table>
<?php
}
?>
</td>
</tr>
</table>
<?php
include("bottom.php");
?>
㈢ 新人求助,学习需要,想要个php输出从1到100金字塔的代码,会的帮忙写下,谢谢! 1 23 345 6789,,到100
<?php
/**
金字塔正序
**/
for($a=1;$a<=10;$a++){
for ($b=10;$b>=$a;$b--){
echo " ";
}
for ($c=1;$c<=$b;$c++){
echo "*"." ";
}
echo "<br />";
}
?>
㈣ 求个简单的php代码
_tags($string, $replace_with_space = true)
{
if ($replace_with_space) {
return preg_replace('!<[^>]*?>!', ' ', $string);
} else {
return strip_tags($string);
}
}
截取字符函数(匹配各种编码)
function truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false){
if ($length == 0)
return '';
if (is_callable('mb_strlen')) {
if (mb_detect_encoding($string, 'UTF-8, ISO-8859-1') === 'UTF-8') {
// $string has utf-8 encoding
if (mb_strlen($string) > $length) {
$length -= min($length, mb_strlen($etc));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/u', '', mb_substr($string, 0, $length + 1));
}
if (!$middle) {
return mb_substr($string, 0, $length) . $etc;
} else {
return mb_substr($string, 0, $length / 2) . $etc . mb_substr($string, - $length / 2);
}
} else {
return $string;
}
}
}
// $string has no utf-8 encoding
if (strlen($string) > $length) {
$length -= min($length, strlen($etc));
if (!$break_words && !$middle) {
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1));
}
if (!$middle) {
return substr($string, 0, $length) . $etc;
} else {
return substr($string, 0, $length / 2) . $etc . substr($string, - $length / 2);
}
} else {
return $string;
}
}
综合就是
$arc=strip_tags($arc);
$arc=truncate($arc,200)