Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-01-2006, 02:24 AM   PM User | #1
Pennimus
Senior Coder

 
Join Date: Jul 2005
Location: UK
Posts: 1,051
Thanks: 6
Thanked 13 Times in 13 Posts
Pennimus is on a distinguished road
Working With The Results Of explode()

I've used explode() to seperate a string of words into a whole bunch of substrings, and now need to manipulate them a bit beyond merely echoing them out one by one.

Firstly, there will be duplicated substrings, how would I go about removing them?

After removing any duplicates I then need to echo all the remaining substrings as a list on my page, how would I run a while loop on them to do this?

Thanks in advance.
Pennimus is offline   Reply With Quote
Old 09-01-2006, 03:56 AM   PM User | #2
litebearer
Regular Coder

 
Join Date: Apr 2004
Posts: 290
Thanks: 0
Thanked 21 Times in 21 Posts
litebearer is on a distinguished road
Take a look a array_unique

http://us2.php.net/array_unique

as for a quick simple display
Code:
<PRE>
print_r($array_name);
</pre>
Lite...
litebearer is offline   Reply With Quote
Old 09-01-2006, 03:57 AM   PM User | #3
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Check out array_unique()

Loop your array:
count()
for loop

ref: http://us2.php.net/manual/en/languag...structures.php

Good luck;
__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 09-01-2006, 09:15 PM   PM User | #4
Pennimus
Senior Coder

 
Join Date: Jul 2005
Location: UK
Posts: 1,051
Thanks: 6
Thanked 13 Times in 13 Posts
Pennimus is on a distinguished road
Thank you.

Okay, I've taken my best stab at this now - how am I doing? Seriously, I'm a little stuck with the for loop and would appreciate some help.

Firstly, the data is a string of words seperated by spaces. Processing the array looks like this:

PHP Code:
$taglist mysql_query ("SELECT tags FROM bookmarks");
$tags1 explode (" "$taglist);
$tags array_unique ($tags1);
$numtags count ($tags); 
That seems simple enough so far. Now the for loop:

PHP Code:
for ($numtags$numtags 0$numtags--) {
  echo 
$tags[0];

Firstly, I'm assuming using $numtags as the initial value is the right thing to do here, given that I aim to echo each substring individually, correct?

Secondly, is "$numtags--" the right way to decrement?

Thirdly, how do I increment the array number in each iteration of the loop?

Thanks.
Pennimus is offline   Reply With Quote
Old 09-01-2006, 09:23 PM   PM User | #5
Brandoe85
teh Moderatorinator


 
Join Date: Sep 2004
Location: USA
Posts: 2,472
Thanks: 4
Thanked 40 Times in 40 Posts
Brandoe85 will become famous soon enough
Try:
PHP Code:
for($i 0$i $numtags$i++)
{
   echo 
$tags[$i] . "<br>";

__________________
-Brando
Why using tables for eating is stupid!
Brandoe85 is offline   Reply With Quote
Old 09-01-2006, 09:46 PM   PM User | #6
Pennimus
Senior Coder

 
Join Date: Jul 2005
Location: UK
Posts: 1,051
Thanks: 6
Thanked 13 Times in 13 Posts
Pennimus is on a distinguished road
Ah, the joys of lateral thinking. Thanks Brandoe

EDIT: A further problem has arisen.

The current code is echoing out the following:

Quote:
Resource id #2
I've read enough to establish that there is a problem in that I'm not converting the data in to a usable form. My gut instinct is that mysql_fetch_array needs to be incorporated somewhere but I'm not sure how or where.

PHP Code:
$taglist mysql_query ("SELECT tags FROM bookmarks") or die(mysql_error());
$tags array_unique (explode (" "$taglist));
$numtags count ($tags); 
PHP Code:
for($i 0$i $numtags$i++) 
{
   echo 
$tags[$i] . "<br>";

I've checked that I'm connected to the database, there is no problem there.

Last edited by Pennimus; 09-01-2006 at 10:37 PM..
Pennimus is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:31 AM.


Advertisement
Log in to turn off these ads.