Click to See Complete Forum and Search --> : how to make a numerical table,.
rapidz
12-27-2006, 03:39 PM
i want to output some data from a database.
but need the table to display each row numerically i.e.
PLACE NAME
1st -----
2nd -----
3rd -----
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
any ideas?
many thanks in advance.
so_is_this
12-27-2006, 06:42 PM
As a demo:
<pre><?php
$endings = array('th','st','nd','rd','th','th','th','th','th','th');
for ($x=1; $x<=25; ++$x)
{
echo $x . $endings[($x<11||13<$x)?($x%10):0] . " ...\n";
}
?></pre>
NightShift58
12-27-2006, 06:57 PM
If your list will have less than 32 entries, use date("S") and you won't need all that code.
so_is_this
12-27-2006, 07:25 PM
If your list will have less than 32 entries, use date("S") and you won't need all that code.
"...all that code," yeah, right. The array, for sure. But, say, why don't you give an example of using that function to achieve such results on the numbers from 1 to 31. Let's see if you'll save on "...all that code." Teach me, please. Thanks. ;)
so_is_this
12-27-2006, 07:50 PM
Perhaps you didn't believe me when I said, "Teach me, please." I meant it. ;) So, I taught myself. Result? Both of the following yield the same result for the numbers from 1 through 31:
<pre><?php
$endings = array('th','st','nd','rd','th','th','th','th','th','th');
for ($x=1; $x<=31; ++$x)
{
echo $x . $endings[($x<11||13<$x)?($x%10):0] . " ...\n";
}
?></pre>
<pre><?php
for ($x=1; $x<=31; ++$x)
{
echo $x . date('S', mktime(0,0,0,1,$x,2000)) . " ...\n";
}
?></pre>
So, does using the date() function truly save on "...all that code"? I don't think so. Only an array is saved, but me thinks it takes more to execute those two functions repeatedly (in the second example) than it does to reference an array which is built just once. On the other hand, though the first example is not restricted to a particular range of numbers, the second example does not require one to come up with the correct algorithm for extracting such endings from an array. ;)
NogDog
12-27-2006, 08:12 PM
Don't know why, I just felt like using the date('S') thing but making it work for any integer, positive or negative. This is the function I came up with -- and that I doubt I'd ever actually use. :)
<?php
function get_ordinal_suffix($int)
{
settype($int, "integer");
$int = abs($int);
while($int > 31)
{
$int -= 10;
}
return $int === 0 ? "th" : date('S', mktime(1,1,1,1,$int));
}
// TEST IT:
header("Content-Type: text/plain");
for($ix = -101; $ix <= 101; $ix++)
{
echo "$ix".get_ordinal_suffix($ix)."\n";
}
?>
so_is_this
12-27-2006, 08:15 PM
I think you'd find things are bit off when your number reached 111 through 113.
NogDog
12-27-2006, 08:23 PM
You are correct, yet another reason not to use it. :)
NogDog
12-27-2006, 08:26 PM
Added one line:
function get_ordinal_suffix($int)
{
$int = substr($int, -2);
settype($int, "integer");
$int = abs($int);
while($int > 31)
{
$int -= 10;
}
return $int === 0 ? "th" : date('S', mktime(1,1,1,1,$int));
}
:)