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)