Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
Good Morning All,

Currently, i'm trying to write a script that pulls some strings from a database that looks similar to this:
this||is|the||lunch||6

this||is|the||lunch||7
Where the number on the end is a unique ID from the database. After I pull these strings down I am trying to search through each one for another string, also pulled from a database, the string pulled from the database in this case looks like this:

After I have all these values I explode each string at their respective delimiters ', and ||'. and use in_array() to search

I'm using forloops to do all of these manipulations but the loops seem to only find the first occurrence. For example, in the example I posted above it will find 'this', but not 'is'. And if i change the order to 'is, this' then it finds 'is' and not 'this'.

Below is the entire snippet of code.
Code:
$idList = explode(",", substr($idList, 1, strlen($idList)));
foreach($idList as $id){
      echo "<br>";
        $query55  = "SELECT * FROM table WHERE col = '$id'";
	$result55 = mysql_query($query55);
	while($row55 = mysql_fetch_array($result55))
	{	
	     $menu = $row55[$timeofday];    //variable previously declared
	     $menuItems = explode("||", $menu);  //this||is||lunch||$id
	     echo '<br>';
	     foreach($favorites as $fav)   //this,is
	     {
		echo '<br>';
		echo 'Searching  For '. $fav;

                //All variations of string
		$firstcap = ucwords(strtolower($fav));
		$allLower = strtolower($fav);
		$allupper = strtoupper($fav);
		
                 if (in_array($firstcap, $menuItems) || in_array($allLower,   $menuItems) || in_array($allupper, $menuItems))
			{
				///ONE OF YOUR FAVORITES WAS FOUND///
					echo '    ------> found';
			}	
							
		}
	}
}

Any help is greatly appreciated it. Thanks!
 
Here is some modified code I have worked on aswell as a page where the output is going., still getting the same result though

Code:
$userquery  = "SELECT * FROM users WHERE userId = '$user'";
$userresult = mysql_query($userquery);
while($userresultrow = mysql_fetch_array($userresult))
{
	$diningID = $userresultrow['diningHall];
	$menuquery = "SELECT * FROM menus WHERE diningHallid = '$diningID'";
	$menuresult = mysql_query($menuquery);
	while($menuresultrow = mysql_fetch_array($menuresult))
		{
		echo "<br>";
		foreach($favorites as $favorite){
			echo "Searching For " . $favorite. "<br>";
			echo $menuresultrow['lunch'] . "<br>";
			if (strlen(strstr($menuresultrow['lunch'],$favorite))>0) {
				echo 'found - ' .$favorite . "<br>";
			}else{
				echo 'did not find - ' .$favorite . "<br>";
			}
					
		}
	}
 }

http://www.mycampusmenu.com/scripts/test.php
 
The inner foreach loop references $favorites, which you don't show the code for. This seems to be the loop where it stops at so it may be helpful to see all of the variables related.
 
First I'm assuming that you had an error in your posting where there is only one pipe between "this" and "is".... "this||is|the||lunch||6". I think the problem your having is with the comma in the $fav. I broke the code down to simpler terms below to test that.

PHP:
$fav = "this, is";  // the comma is the problem
$menu = "this||is||the||lunch||6";
$id = 4;
$menuItems = explode("||", $menu);  //this||is||lunch||$id

// ---- your code -----
foreach($favorites as $fav)   //this,is
{
	echo '<br>';
	echo 'Searching  For '. $fav;

	//All variations of string
	$firstcap = ucwords(strtolower($fav));
	$allLower = strtolower($fav);
	$allupper = strtoupper($fav);

	if (in_array($firstcap, $menuItems) || in_array($allLower,   $menuItems) || in_array($allupper, $menuItems))
	{
	  ///ONE OF YOUR FAVORITES WAS FOUND///
	  echo '    ------> found';
	}
}

There are some things I'm not clear about like why you use || as a separator when a space would work? The php function str_word_count will put words in an array.

PHP:
print_r(str_word_count("this is the lunch", 1));

Edit - Regarding the comment below... Sorry I thought there was an array function to convert all values to upper or lowercase, unfortunately only the array KEYS can be changed with those functions. So I guess the way you're doing it is OK.
************
Also all the lines about $firstcap, $allLower, $allUpper... why not make the $menuItems array all caps and make $fav all caps and compare those two values? Anyway the comma looks to be the hangup and not sure why it's needed either?
************
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.