View Single Post
Old 03-09-2006, 01:37 PM   #1 (permalink)
Deanthim
Registered User
 
Join Date: Mar 2006
Posts: 1
Deanthim is on a distinguished road
array_search problems

I'm trying to have a system on my website (only exists on my computer at the moment) where the user can specify different modules on the left of the page. I made a page where the user enters the order (I also made a little rss parser, so people can enter those for a module) of the modules, which then gets entered into a mysql table, with fields(in order) :

USER_ID
RSS1
RSS1_POS
1_ITEMS
RSS2
RSS2_POS
2_ITEMS
RSS3 varchar
RSS3_POS
3_ITEMS
NEWS_POS
MUSIC_POS
MOVIE_POS

Here is the array Im working with (with both assoc and num keys for clarity) :
Array (
[0] => 112 [USER_ID] => 112
[1] => http://rss.slashdot.org/Slashdot/slashdot [RSS1] => http://rss.slashdot.org/Slashdot/slashdot
[2] => 1 [RSS1_POS] => 1
[3] => 11 [1_ITEMS] => 11
[4] => http://engadget.com/rss.xml [RSS2] => http://engadget.com/rss.xml
[5] => 2 [RSS2_POS] => 2
[6] => 5 [2_ITEMS] => 5
[7] => http://digg.com/rss/index.xml [RSS3] => http://digg.com/rss/index.xml
[8] => 3 [RSS3_POS] => 3
[9] => 4 [3_ITEMS] => 4
[10] => 0 [NEWS_POS] => 0
[11] => 0 [MUSIC_POS] => 0
[12] => 0 [MOVIE_POS] => 0 )

Once I got this into an array, I have a for loop, in which the array search searches for item with pos = the variable of the loop (see the code below). When I array_search outside the loop it works as I want it to, however inside the loop it returns funky values.
Code:
for($i = 0;$i<6;$i++){ //technically there are 6 fields, but I wanted to fix main problem first
global $modulead; //the array of the mysql_query
print "<br><br>$i<br><br>$first :  {$modulead[$first-1]}, {$modulead[$first+1]}<br><br>"; // first-1 is the url of the rss, +1 is the number of items to be shown
$str = (string) $i; //cast it as string b/c the mysql query returns the values as strings


$first = array_search($i,$modulead); //broken part

//These modules will be the same for everyone, will result in an include() statement of some sort
//$moduleorder is already declared as array
if($first==10) {$moduleorder[] = "NEWS!"; continue;}  //access and print news module
if($first==11) {$moduleorder[] = "MUSIC!"; continue;} //access and print music module
if($first ==12) {$moduleorder[] = "MOVIES!"; continue;} //access and print movie module
$moduleorder[] = array($modulead[$first-1], $modulead[$first+1]); 

}
What it returns (its the needle of array search, 2 lines, the value returned from array search(: )the value of the key before it(,)value of key after it) :

0

: , http://rss.slashdot.org/Slashdot/slashdot



1

1 : 112, 1



2

2 : http://rss.slashdot.org/Slashdot/slashdot, 11



3

5 : http://engadget.com/rss.xml, 5



4

8 : http://digg.com/rss/index.xml, 4



5

9 : 3, 0


As you can see, searching for 2 returns the 2nd key, which holds 1, searching for 3 returns the 5th key, which holds 11, and searching for 4 returns eight, which oddly enough acutally holds 4. Is there something Im missing? A quirk in phps typecasting that Im not aware of? Any help is much appreciated
Deanthim is offline   Reply With Quote