很多站长朋友们都不太清楚php数组在html,今天小编就来给大家整理php数组在html,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 在HTML如何引用PHP中的数组? 2、 php如何在html页面中输出数组 3、 php 一维数组怎么遍历html 4、 html页面如何处理php传过来的数组 5、 【php】php把数组传递到html页面中的问题 6、 php写一个数组用html表格输出这个数组 在HTML如何引用PHP中的数组?朋友,你对PHP和HTML的工作原理还不是很清楚。
PHP是在服务器端运行,运行结束后,输出HTML到客户端浏览器
浏览器解析HTML。
所以,PHP运行的时候HTML(包括JAVAScript)不会运行。而HTML运行的时候,PHP早已经运行完了。
你没有把代码贴全,不知道你的代码有什么具体的问题。
如果你想用多个随机数,也可以用javascript
php如何在html页面中输出数组用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>';
?>
php 一维数组怎么遍历htmlphp遍历数组的4种方法总结,它分分别是foreach循环、each函数、list函数、for循环,需要的朋友可以参考下
在php中可以用来遍历数组的函数有很多,如有:for语句、list、each、foreach这四个函数,这也是在php中遍历数组的几个主要的函数,下面我来给大家介绍。
foreach遍历数组
我们在运用数组时,常常要遍历数组并获得各个键或者元素值,php提供了一些专门遍历数组的函数。这里先介绍foreach遍历数组函数的用法。
结构形式:
复制代码 代码如下:
foreach ( array_expression as $value ) statement
/* array_expression是要遍历的数组
as作用是将数组的值赋给$value
statement是后续语句
*/
实例1:
复制代码 代码如下:
'白色' ,
'black' => '黑色' ,
'red' => '红色' ,
'green' => '绿色',
'yellow' => '黄色');
foreach( $color as $c) echo $c ."
";
?>
通过foreach不仅可以获得元素的值也可以获得键名,结构形式:
复制代码 代码如下:
foreach ( array_expression as $key => $value ) statement
将以上实例中第7行的代码:
复制代码 代码如下:
foreach( $color as $c) echo $c ."<br>";
改为:
复制代码 代码如下:
foreach( $color as $key => $c) echo $key.$c ."<br>";
each遍历数组
遍历数组是php数组操作一个重要的环节,除了前面提到的foreach函数,这里再介绍一个遍历数组的函数-each()。
用each()函数可以输出当前指针位置的键名和对应的元素值。可以使用”0″或”key”来访问键名(标识符),用”1″或”value”访问标识符对应的值。
实例:
复制代码 代码如下:
<?php
$languages=array(1=-->"php",
5=>"html",
10=>"css");
$a=each($languages); /* 第一次遍历数组 */
echo $a[0] ."t";
echo $a[1] ."<br>";
$a=each($languages); /* 第二次遍历数组 */
echo $a[key] ."t";
echo $a[value];
?>
list遍历数组
函数list可以在遍历数组时一次赋给变量,通常和each()函数配合使用。用list()函数使访问each()返回的键和值变得更加简单了。
实例:
复制代码 代码如下:
<?php
$date=array(1=-->"Monday",
2=>"Tuesday",
3=>"Wednesday");
list($key,$value)=each($date); /* 遍历函数 */
echo "$key $value" ."<br>"; /* 输出第一个数组 */
$next=next($date); /* 指针后移 */
echo "$next";
?>
ps:list()函数刚好与array()函数相反,array()将一系列数据构造成数组,而list()则将数组拆分成数据。
for遍历数组
除了php预定义的一些遍历数组函数,我们还可以利用for语句的循环特性将数组遍历输出。下面给出例子:
复制代码 代码如下:
<?php
$a[]="张学友"; /* 定义数组 */
$a[]="刘德华";
$a[]="黎明";
$a[]="郭富城";
$s=count($a); /* 统计数组个数 */
for($i=0;$i<$s;$i++){ /* 遍历数组 */
echo $a[$i] ."<br /-->"; /* 显示数组 */
}
?>
html页面如何处理php传过来的数组先把结果存数组里 然后
echo json_encode($arr);
转为json后 用js就好处理了
【php】php把数组传递到html页面中的问题将SQL查出来的结果赋给res。(这里用的是smarty模板引擎 $smarty->assign('res', $sql);)
然后在html页面遍历出来就行了
<table >
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
</tr>
<foreach name="res" item="result">
<tr>
<td>result['id']</td>
<td>result['username']</td>
<td>result['password']</td>
<td>result['email']</td>
</tr>
</foreach>
</table>
php写一个数组用html表格输出这个数组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=$arr; else $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:500px; background-color:cyan; color: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 10px; padding-left:4px;padding-right:4px; text-align:right;width:500px; background-color:black; color: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 silver; padding-left:4px;padding-right:4px; text-align:center;width:500px; background-color:cyan; color:navy;'");
echo $t4->html()."<hr />";
?>
9、然后就完成了。
关于php数组在html的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php数组在html php7 数组的详细内容...