php如何在html页面中输出数组

html-css04

php如何在html页面中输出数组,第1张

用php判断查询数组是否为空,不为空就将给html赋值,控制是否显示图片。

$sql = "select * from ‘_goods_attr‘ where ‘attr_value‘=‘ ’"php查询,$res=mysql_query($sql)php处理,$arr = mysql_fetch_assoc($res)转为数组,后面就是自己判断数组是否为空和给html模板赋值的事情了。

$selv=array(1,2,3,4,5)//下拉列表值

$dbv=3

foreach($selv as $s){

$issl=''

if($s==$dbv) $issl='selected'

$str.="<option value='$s' $issl>-".$s."-</option>"

}

echo '<select>'.$str.'</select>'

?>

1、首先输入:

<?php

class xtable

{

private $tit,$arr,$fons,$sextra

public function __construct()

{

$this->tit=array()       // strings with titles for first row

$this->arr=array()       // data to show on cells

$this->fons=array("#EEEEEE","#CCEEEE")  // background colors for odd and even rows

$this->sextra=""       // extra html code for table tag

}

2、然后输入

public function extra($s)      // add some html code for the tag table

{

$this->sextra=$s

}

public function background($arr) {if (is_array($arr)) $this->fons=$arrelse $this->fons=array($arr,$arr)}

public function titles($text,$style="") {$this->tit=$text$this->sesttit=$style}

public function addrow($a) {$this->arr[]=$a}

public function addrows($arr) {$n=count($arr)for($i=0$i<$n$i++) $this->addrow($arr[$i])}

public function html()

3、然后输入:

{

$cfondos=$this->fons

$titulos="<tr>"

$t=count($this->tit)

for($k=0$k<$t$k++)

{

$titulos.=sprintf("<th>%s</th>",$this->tit[$k])

}

$titulos.="</tr>"

$celdas=""

$n=count($this->arr)

for($i=0$i<$n$i++)

4、然后输入:

{

$celdas.=sprintf("<tr style='background-color:%s'>",$this->fons[$i%2])

$linea=$this->arr[$i]

$m=count($linea)

for($j=0$j<$m$j++)

$celdas.=sprintf("<td  %s>%s</td>","",$linea[$j])

$celdas.="</tr>"

}

return sprintf("<table cellpadding='0' cellspacing='0' border='1' %s>%s%s</table>",$this->sextra,$titulos,$celdas)

}

public function example()

{

$tit=array("Apellidos","Nombre","Telefono")

$r1=array("Garcia","Ivan","888")

$r2=array("Marco","Alfonso","555")

$x=new xtable()

$x->titles($tit)     //take titles array

$x->addrows(array($r1,$r2))  // take all rows at same time

return $x->html()     //return html code to get/show/save it

}

}

5、然后输入:

// Example

$t1=new xtable()

echo $t1->example()."<hr />"

$t2=new xtable()

for($i=1$i<=10$i+=2)

{

$t2->addrow(array("ODD",$i))

$t2->addrow(array("EVEN",$i+1))

}

6、然后输入:

$t2->background(array("pink","gold"))

$t2->titles(array("TYPE","#"))

$t2->extra(" style='width:500pxbackground-color:cyancolor:navy'")

echo $t2->html()."<hr />"

$t3=new xtable()

for($i=1$i<=6$i++)

{

$t3->addrow(array("5x".$i,5*$i))

}

7、然后输入:

$t3->background(array("olive","maroon"))

$t3->titles(array("Multiplication table","5"))

$t3->extra("style='border:dotted red 10pxpadding-left:4pxpadding-right:4pxtext-align:rightwidth:500pxbackground-color:blackcolor:white'")

echo $t3->html()."<hr />"

$t4=new xtable()

$a=array("#")

for($i=1$i<=10$i++)

{

$a[]=$i

}

8、然后输入:

$t4->addrow($a)

$t4->background(array("pink","gold"))

$tit=array()$tit[]="Numbers"

for($i=1$i<=10$i++) $tit[]="#"

$t4->titles($tit)

$t4->extra("style='border:solid 1px silverpadding-left:4pxpadding-right:4pxtext-align:centerwidth:500pxbackground-color:cyancolor:navy'")

echo $t4->html()."<hr />"

?>

9、然后就完成了。

可以使用正则表达式来解决这个问题

具体的代码很多不详细写了,举个例子给题主吧

<?php

 

$htmlStr = '<li>首页</li>'

preg_match_all('/<li>.*?<\/li>/', $htmlStr , $res)

$ret = preg_replace('/(<li>|<\/li>)/', '', $res[0][0])

//输出res

//var_dump($res) 

echo $ret

最终输出的结果为:首页

在这个例子上题主所需要的值基本都可以用正则算法过滤出来

希望能帮到题主

如果可以,还请采纳