-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathclass.loop.php
48 lines (40 loc) · 1.09 KB
/
class.loop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?PHP
class Loop
{
private $index;
private $elements;
private $numElements;
public function __construct()
{
$this->index = 0;
$this->elements = func_get_args();
$this->numElements = func_num_args();
}
public function __tostring()
{
return (string) $this->get();
}
public function get()
{
if($this->numElements == 0) return null;
$val = $this->elements[$this->index];
if(++$this->index >= $this->numElements)
$this->index = 0;
return $val;
}
public function rand()
{
return $this->elements[array_rand($this->elements)];
}
}
// Example:
// $color = new Loop('white', 'black');
//
// echo "<tr color='$color'/>";
// echo "<tr color='$color'/>";
// echo "<tr color='$color'/>";
//
// Or
//
// while($row = mysql_fetch_array($result))
// echo "<tr color'$color'>the row colors will alternate</tr>";