...

View Full Version : Help with php array



Andy92
08-05-2008, 07:52 PM
I need help with a php array.

At first, i had 2 sections on my website. Forums and Tutorials. This simple php script would get the forumid that you are on, pass it through all the possibly tutorial section forumid's, and determine if yu were viewing the forum, or the tutorials section...



$filtered = FALSE;
$filter = array
(
'33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50',
);
$filter = array_map( 'strtolower', $filter );

foreach ( $filter as $word )
{
if ( strpos( $forumid, $word ) !== FALSE )
{
$filtered = TRUE;
$menututorials = "tutorials2";
$menuforum = "forum";
break;
} else {
$menututorials = "tutorials";
$menuforum = "forum2";
}
}


Now that worked fine! But now i have 5 different sections, and i have tried to evolve that script, and it just doesnt work.

The 5 sections i have now are forum, resources, marketplace, tutorials and jobs. I have written this script below, but it just allways says that you are on the forum even if you are on other sections.

Can anyone pick up on whats wrong with it?



$filtered = FALSE;
$fresources = array
(
'11','51','52','53','54','55','56','57','58','59','60','61','62',
);
$fmarketplace = array
(
'25','26','27','28','63','64','65','66','67','68','69',
);
$ftutorials = array
(
'33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50',
);
$fjobs = array
(
'70','71','72','73','74','75','76',
);

$fresources = array_map( 'strtolower', $fresources );
$fmarketplace = array_map( 'strtolower', $fmarketplace );
$ftutorials = array_map( 'strtolower', $ftutorials );
$fjobs = array_map( 'strtolower', $fjobs );

foreach ( $fresources as $wresources )
foreach ( $fmarketplace as $wmarketplace )
foreach ( $ftutorials as $wtutorials )
foreach ( $fjobs as $wjobs )
{
if ( strpos( $forumid, $wresources ) !== FALSE )
{
$filtered = TRUE;
$menuforum = "forum";
$menuresources = "resources2";
$menumarketplace = "marketplace";
$menututorials = "tutorials";
$menujobs = "jobs";
break;
} else if ( strpos( $forumid, $wmarketplace ) !== FALSE ) {
$filtered = TRUE;
$menuforum = "forum";
$menuresources = "resources";
$menumarketplace = "marketplace2";
$menututorials = "tutorials";
$menujobs = "jobs";
break;
} else if ( strpos( $forumid, $wtutorials ) !== FALSE ) {
$filtered = TRUE;
$menuforum = "forum";
$menuresources = "resources";
$menumarketplace = "marketplace";
$menututorials = "tutorials2";
$menujobs = "jobs";
break;
} else if ( strpos( $forumid, $wjobs ) !== FALSE ) {
$filtered = TRUE;
$menuforum = "forum";
$menuresources = "resources";
$menumarketplace = "marketplace";
$menututorials = "tutorials";
$menujobs = "jobs2";
break;
} else {
$menuforum = "forum2";
$menuresources = "resources";
$menumarketplace = "marketplace";
$menututorials = "tutorials";
$menujobs = "jobs";
}
}

malfist
08-05-2008, 08:25 PM
what is this:


$fresources = array
(
'11','51','52','53','54','55','56','57','58','59','60','61','62',
);
$fmarketplace = array
(
'25','26','27','28','63','64','65','66','67','68','69',
);
$ftutorials = array
(
'33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50',
);
$fjobs = array
(
'70','71','72','73','74','75','76',
);


And this:


foreach ( $fresources as $wresources )
foreach ( $fmarketplace as $wmarketplace )
foreach ( $ftutorials as $wtutorials )
foreach ( $fjobs as $wjobs )


Do you realize exactly what they're doing? Don't you think there's a better, more clear way? I don't see any comments either.

Andy92
08-06-2008, 12:53 AM
I dont really know. I havent really learnt php arrays yet. But all the numbers are basically mapping out what forum id's are in each section, like resources, jobs etc, then it matches it against the forum id it has at the moment and determines what section you are on.

Can you help me?

malfist
08-11-2008, 10:00 PM
You shouldn't copy and paste code unless you understand what it's doing. The PHP site has a really good explanation about arrays, you should check it out.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum