Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-09-2006, 12: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
Old 03-09-2006, 01:13 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,503
sde is on a distinguished road
are you really meaning to search for the iterating number $i ? your code is a little confusing to me. i don't think you have to keep re-declaring global vars inside your for loop.

since i don't understand exactly what your code does, i'll suggest looking at this example from php.net
PHP Code:
<?php
$array 
= array(=> 'blue'=> 'red'=> 'green'=> 'red');

$key array_search('green'$array); // $key = 2;

echo "green key is: " $key "<br>";

$key array_search('red'$array);  // $key = 1;

echo "red key is: " $key "<br>";
?>
try running that to make sure the keys print as you expect.

another thing you might want to try if you haven't already, is dump your array to screen to make sure it contains what you think it contains.

PHP Code:
<?
echo "<pre>";
print_r($modulead);
echo 
"</pre>";
?>
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Similar problems..over and over! whiteFLAME PHP 6 09-02-2005 08:58 AM
cin.getline problems mconn86 Standard C, C++ 5 04-14-2005 01:02 PM
Problems debugging visual c++ in vs.net 2003 Engineer MS Technologies ( ASP, VB, C#, .NET ) 2 07-20-2004 12:19 PM
no dial tone problems fabiopao Linux / BSD / OS X 4 02-23-2003 10:51 AM
I have problems... anon Lounge 4 01-12-2003 07:47 PM


All times are GMT -8. The time now is 05:48 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting