General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old October 22nd, 2003, 11:27 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
building a table into an array

Hi everyone,
I am writing some code that will go through a chunk of text and compares each word in the text to the contents of a table (glossary table with term and definition). When it finds a word in the glossary table it incases it with some javascript that will display the definition.

i got this to work using a nested loops but it was running a SELECT on the table for every word and therefore taking ages.

what i want to do is build the glossary table into an array once and then search one array against the other.

my question: whats the easiest way of doing this?

thanks in advance

toby

Reply With Quote
  #2  
Old October 22nd, 2003, 03:59 PM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Building an array in PHP is very easy.
If you have an array called $glossary, then all you need to do inside a loop:

foreach ( $data as $glossary_word ) {
$glossary[] = $glossary_word;
}
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old October 23rd, 2003, 02:29 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Sorry reading back my message i wasn't clear at all. I want to build an array that holds both the term and the definition, ie

foreach ( $data as $glossary_word ) {
$glossary[][] = $glossary_term , $glossary_definition;
}

I haven't tried the code above and it is a guess. I know two dimentional arrays exsit but not how to create one.

Reply With Quote
  #4  
Old October 23rd, 2003, 02:39 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
In this case you would do the following (assuming you have a database recordset):

PHP Code:
while ( $rs mysql_fetch_object$result ) ) {
  
$glossary[$rs->term] = $rs->definition;


Reply With Quote
  #5  
Old October 23rd, 2003, 03:40 AM
tobycatlin tobycatlin is offline
Hooner
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: norwich
Posts: 51 tobycatlin User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
That looks pretty good to me. Can i trouble you for a bit more time and ask for a short explaination of whats going on? well actually if i say what i think it happening

PHP Code:
while ( $rs mysql_fetch_object$result ) ) {
  
$glossary[$rs->term] = $rs->definition;



$rs is the recordset
$glossary is our 2 dim array containing the term and the definition

I can loop through a normal array, what do i need to do to output it.
one more thing...
can i use inarray() function on a 2 dimentional array?

thanks again

Reply With Quote
  #6  
Old October 23rd, 2003, 04:58 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Actually here are a few facts:

1. What we just looked at is not really a two dimensional array. It is still one dimension, however, it is not indexed as you would normally imagine.

It is called an associative array. Normal arrays are indexed by number, such as: $element[0], $element[1]... [n]

2. To Print this array's elements to screen you would use:
PHP Code:
 print_r$element ); 


3. The same works for an associative array. Basically the only difference is that an associative array is index by a string instead of numbers.

4. Yes, $rs is the recordset

5. $glossary is our an associative array containing the term (as the key) and the definition (as the value).

6. in_array() would actually search through the values of the array, not the keys.

use array_key_exists() instead

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > building a table into an array


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway