/    Sign up×
Community /Pin to ProfileBookmark

how to make a numerical table,.

i want to output some data from a database.

but need the table to display each row numerically i.e.

[code]
PLACE NAME

1st —–
2nd —–
3rd —–
4th
5th
6th
7th
8th
9th
10th
11th
12th
13th
[/code]

any ideas?

many thanks in advance.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — As a demo:
[code=php]
<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>[/code]
Copy linkTweet thisAlerts:
@NightShift58Dec 28.2006 — If your list will have less than 32 entries, use [B]date("S")[/B] and you won't need all that code.
Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — If your list will have less than 32 entries, use [B]date("S")[/B] and you won't need all that code.[/QUOTE]
"...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. ?
Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — 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:
[code=php]
<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>[/code]

[code=php]
<pre><?php
for ($x=1; $x<=31; ++$x)
{
echo $x . date('S', mktime(0,0,0,1,$x,2000)) . " ...n";
}
?></pre>[/code]

So, does using the [B]date()[/B] 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. ?
Copy linkTweet thisAlerts:
@NogDogDec 28.2006 — 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. ?
[code=php]
<?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";
}
?>
[/code]
Copy linkTweet thisAlerts:
@so_is_thisDec 28.2006 — I think you'd find things are bit off when your number reached 111 through 113.
Copy linkTweet thisAlerts:
@NogDogDec 28.2006 — You are correct, yet another reason not to use it. ?
Copy linkTweet thisAlerts:
@NogDogDec 28.2006 — Added one line:
[code=php]
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));
}
[/code]

?
×

Success!

Help @rapidz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...