如何使用PHP计算数组内容并分配数字位置 [英] How to count an array content and assign number position with php

查看:92
本文介绍了如何使用PHP计算数组内容并分配数字位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

am正在处理一个网站项目,在该项目中,我从数据库中获取数组,并使用foreach语句输出其内容。
我想使用php脚本来计算数组中有多少个项目,并为每个项目分配数字位置,这样,如果第一个项目的数字位置是奇数,我将对其使用css float:left样式,但是如果它的偶数会浮动:对

am working on a website project in which i fetch array from my database and use foreach statement to output its contents. I want use php script to count how many items are in the array and assign number position to each item so that if the number position of the first item is an odd number i will apply css float:left style to it but if its even number it will float:right

有任何帮助吗??。
谢谢。

any help please??. thank you.

推荐答案

CSS3具有不错的功能(。list:nth-​​child(odd){ float:left;} .list:nth-​​child(even){float:right;}),但请注意,这在许多浏览器(例如8及以下版本,较旧的firefox等)上均不起作用。使用WinXP + IE的每个用户都只会看到一个正常列表,没有不同的颜色。

CSS3 has nice features (.list:nth-child(odd) {float:left;} .list:nth-child(even) {float:right;} ), but beware that this will not work on many browsers (ie8 and below, older firefox, etc) .. at least every user with WinXP+IE will see just a normal list without the different colors.

jQuery(这里也指出)也可以使用 $('。xy:odd')。css({'float':'left'})选择; ,但是如果您在项目中未使用jQuery,则只会增加很大的开销(90kb jQuery库)。
如果使用php,性能也会更好。

jQuery (also noted here) can also select with $('.xy:odd').css({'float':'left'}); , but if you are not using jQuery in your project its just a big (90kb jQuery library) overhead. Performance is also better if going with php.

因此,最好使用php和模操作数(如果$ count%2),请在此处查看约瑟夫·西尔伯(Joseph Silber)的回答。

So you better go with php and the modulo operand (if $count % 2), see Joseph Silber's answer here.

这篇关于如何使用PHP计算数组内容并分配数字位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆