/    Sign up×
Community /Pin to ProfileBookmark

Order an Array – Not your simple order

I am a bit new here, but I’ve been referred to this forum in hopes that you guys will help me. Please don’t disregard this topic because it’s so long, I really need your help.

I am currently trying to make a forum that looks sort of (or exactly like) that found on [URL=http://www.ticalc.org]ticalc.org[/URL], but I have met a problem. I am using a system where an ID of 1 would refer to the first reply to the main news item. An ID of 2 would refer to the second reply to the news item. However, an ID of 1.1 woud refer to the first reply to the first reply to the news item, while 1.2 would refer to the second reply to the first reply to the news item. This goes on (theoretically infinitely), meaning that you could have something like 1.2.1.1.2.5.19.30 (these aren’t decimals, they are merely integers separated by a period)

I know this is confusing, but it’s difficult to put into words. You should look at ticalc.org. Basically what I have done is made an array, for example $idsarray of x elements, where x is the number of total replies (ID of 1 counts as one element, ID of 1.4.29 counts as one element), and I want to display them in order. Because someone will reply to a previous reply even after the next reply of a higher priority has been created, I need to order the array based on the following example. I was wondering if anyone could help me with the script (or if anyone knows of a forum that is in the form of [URL=http://www.ticalc.org]ticalc.org[/URL]‘s).

Example:

I have the following elements to order:
1
1.1.1.1
1.1
2
1.2
1.2.1
1.1.3
1.1.1
1.1.2

I need them ordered as:
1
1.1
1.1.1
1.1.1.1
1.1.2
1.1.3
1.2
1.2.1
2

To start off, I have created a script that will find how many periods are in an element (or rather string, which is the type of element in the arrays):

CODE:
function num_times_str_appears($needle, $haystack)
{
$number = 0;
while (strstr($haystack, $needle))
{
// need to get rid of the “.”
// in the beginning of the new $haystack
$temp = strstr($haystack, $needle);
$haystack = ltrim($temp, $needle);
$number++;
}
return $number;
}

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NedalsApr 09.2004 — If you treat the element id's as strings, it gets pretty simple.

[code=php]
<html><head><title>Untitled</title>
<script type="text/javascript">
<!--
ary = new Array(
'1',
'1.1.1.1',
'1.1',
'2',
'1.2',
'1.2.1',
'1.1.3',
'1.1.1',
'1.1.2'
);

ary.sort();
data = "";

function test() {
for (i=0; i<ary.length; i++) {
data += '|'+ary[i]
}
alert(data);
}

window.onload = test
//-->
</script>
</head>

<body>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@zprogauthorApr 10.2004 — You guys are wonderful - exactly what I need. The only problem is that it's in JavaScript. Although I can play around with that for a while, is it possible for it to give me another array, say $newarray, of the same number of elements, just sorted?
Copy linkTweet thisAlerts:
@NedalsApr 11.2004 — It occurs to me that you might be trying to solve the wrong problem.

If you are saving your posts to a database (which you should), your database table is likely to be set up something like this..

postid | title | post where postid is type VARCHAR(xx)
--------|-------|----------
1.1.3.2 | text | text

Now all you need to do is...

.. "SELECT postid, title, post FROM the_table WHERE postid LIKE '1%' ORDER BY postid" ..

for thread 1

Fetch your results and they will all magically be in the order you want.

Hope that helps!
Copy linkTweet thisAlerts:
@zprogauthorApr 12.2004 — Thank you very much, that would work, and it is probably more effective. What I was trying to do was to minimize the amount of queries I have done to MySQL. I wanted to load absolutely everything into a multidimentional array and then display that from there. Maybe I'll just work with what you said above. That is much more effective. Thank you.

In addition, do any of you guys know if there is a downloadable forum system like that of [URL=http://www.ticalc.org]TICALC.ORG[/URL]?
Copy linkTweet thisAlerts:
@JuuitchanApr 14.2004 — Would "1.9" and "1.10" be in the correct sort order??

If you are going to treat these as strings, you might want a different method. Or does it matter to you?
Copy linkTweet thisAlerts:
@zprogauthorApr 14.2004 — [i]Originally posted by Juuitchan [/i]

[B]Would "1.9" and "1.10" be in the correct sort order??



If you are going to treat these as strings, you might want a different method. Or does it matter to you? [/B]
[/QUOTE]


Yep, that's correct order. It's not decimal. The period is just for separation.
Copy linkTweet thisAlerts:
@JuuitchanApr 14.2004 — I VERY strongly suggest you test your sort routine on these data:

1

1.1

1.2

1.2.1

1.3

1.4

1.5

1.6

1.7

1.8

1.9

1.10

Where does 1.10 end up?? At the end or just before 1.2 or what??
Copy linkTweet thisAlerts:
@NedalsApr 14.2004 — [i]Originally posted by Juuitchan [/i]

[B]Would "1.9" and "1.10" be in the correct sort order?? [/B][/QUOTE]
Juuitchan, you are absolutely right. It will not sort in the desired order using the method I posed earlier. Needs a rethink.
Copy linkTweet thisAlerts:
@JuuitchanApr 14.2004 — Try tokenizing the string, specifying "." as the separator. Is that possible?
Copy linkTweet thisAlerts:
@YoNApr 15.2004 — This does it:
[code=php]<?php
$arraynew = array(
'1',
'1.1',
'1.2',
'1.3',
'1.4',
'1.5',
'1.10',
'1.6',
'1.7',
'1.8',
'1.9',
'1.2.1'
);
natsort($arraynew);
echo '<pre>';
print_r($arraynew);
echo '</pre>';
?>[/code]
HTH
×

Success!

Help @zprog 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.21,
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,
)...