MVC Query to match

I trying to match two different  query returns to display information that a user can see.

First Query:
	$region_id = $this->view->regions = $this->db->fetchAll("SELECT states FROM regions WHERE manager_id='$user_id' ORDER BY states ASC");	
	echo '<pre>';
	echo var_dump($region_id);
	echo '</pre>'

//  That gives me the following //
array(1) {
  [0]=>
  array(1) {
    ["states"]=>
    string(16) "|CA|MA|NV|NH|NY|"
  }
}

Open in new window


Then I have another query to get the user information:
   $this->view->user = $this->user;
$user_id = $this->view->user->user_id;
$this->db->fetchAll("SELECT states FROM users WHERE user_id='$user_id' ORDER BY states ASC");
//  That gives me the following //
array(1) {
  [0]=>
  array(1) {
    ["states"]=>
    string(16) "|CA|NV|"
  }
}

Open in new window


What I want to do is, if the $user_id / States  (string(16) "|CA|NV|") are equal to any of the $manager_id States (string(16) "|CA|MA|NV|NH|NY|")

Then Do Something.
I have to have one match, not all match.

I'm not sure this is clear but it is a place to start.
LVL 8
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This looks wrong to me.  var_dump() returns void.

echo var_dump($region_id);
0
Ray PaseurCommented:
Please see http://www.laprbass.com/RAY_temp_rgranlund.php
<?php // RAY_temp_rgranlund.php
error_reporting(E_ALL);

$haystack = '|CA|MA|NV|NH|NY|';
$needle   = '|CA|NV|';

$h = trim($haystack, '|');
$n = trim($needle,   '|');

$h = explode('|', $h);
$n = explode('|', $n);

$i = array_intersect($h, $n);
if (!empty($i))
{
    // FOUND AN INTERSECTION
    var_dump($i);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Hmm... The PHP manual says var_dump() returns void, but apparently that statement is inoperative when var_dump() is used with echo.  Echo is not needed, and the output of echo var_dump() is the same as var_dump() without echo.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

rgranlundAuthor Commented:
@ray, if I start to adjust what you sent me I get a warning:

array(1) { [0]=> array(1) { ["states"]=> string(16) "|CA|MA|NV|NH|NY|" } }


Notice: Array to string conversion in BLAH BLAH BLAH

Line: 6

error_reporting(E_ALL);
$haystack = $this->db->fetchAll("SELECT states FROM regions WHERE manager_id='$user_id' ORDER BY states ASC");	
var_dump($haystack);
$needle   = '|CA|NV|';

$h = trim($haystack, '|');
$n = trim($needle,   '|');

$h = explode('|', $h);
$n = explode('|', $n);

$i = array_intersect($h, $n);
if (!empty($i))
{
    // FOUND AN INTERSECTION
    var_dump($i);
}

Open in new window

0
Ray PaseurCommented:
I think you may want to use $haystack[0]['states'] or similar to get to the string variable.
0
rgranlundAuthor Commented:
@Ray or whoever else may be interested.

I have a lot of notes in the script but I think you can follow it.
The way that I have it written, I never meet the criteria;  
$i = array_intersect($h, $ar);
if (!empty($i)) {
[code]
Here is the entire script:
[code]
		//get news
		$alerts = $this->user->getAlerts($_GET['sort'], $_GET['search']);
		$collection = array();

   $this->view->user = $this->user;
		
	//  START  //
	$user_id = $this->view->user->user_id;

	$hay = $this->db->fetchAll("SELECT states FROM regions WHERE manager_id='$user_id' ORDER BY states ASC");


	$haystack = $hay[0]['states'];
		echo 'HAYSTACK:<br />'.$haystack.'<br /><br />';
	$h = trim($haystack, '|');

	$h = explode('|',$h);

	echo '<pre>EXPLODED HAYSTACK:<br />';
		var_dump($h);
	echo '<br /><br /></pre>';
		foreach($alerts AS $item){
				$assigned_region = $item['assigned_regionals'];	
				if($assigned_region !='') {
			echo '<strong>ASSIGNED REGION:</strong><br />'.$assigned_region.'<br />';
				$ar = trim($assigned_region, '|');
				$ar = explode('|', $ar);
					echo '<pre>AR:';
						var_dump ($ar);
					echo '<br /></pre>';
				
				$i = array_intersect($h, $ar);

				if (!empty($i)) {
					$collection[] = new Stax_Alert($item);
		
					echo '<br /><br /><pre><strong>ALERT REGIONS:</strong><br />';
						var_dump($ar);
					echo '</pre>';
				}
			}
			
		}

Open in new window


Here is the what I get on the page:
HAYSTACK:
|CA|MA|NV|NH|NY|

EXPLODED HAYSTACK:
array(5) {
  [0]=>
  string(2) "CA"
  [1]=>
  string(2) "MA"
  [2]=>
  string(2) "NV"
  [3]=>
  string(2) "NH"
  [4]=>
  string(2) "NY"
}


ASSIGNED REGION:
|11|6|7|1|4|3|2|8|10|

AR:array(9) {
  [0]=>
  string(2) "11"
  [1]=>
  string(1) "6"
  [2]=>
  string(1) "7"
  [3]=>
  string(1) "1"
  [4]=>
  string(1) "4"
  [5]=>
  string(1) "3"
  [6]=>
  string(1) "2"
  [7]=>
  string(1) "8"
  [8]=>
  string(2) "10"
}

ASSIGNED REGION:
|11|6|7|1|4|

AR:array(5) {
  [0]=>
  string(2) "11"
  [1]=>
  string(1) "6"
  [2]=>
  string(1) "7"
  [3]=>
  string(1) "1"
  [4]=>
  string(1) "4"
}

Open in new window

0
rgranlundAuthor Commented:
I will re-post in a while.  I realized I was looking at the wrong field.
0
rgranlundAuthor Commented:
OK, I have straignted it out, however, if there is only one number in the first argumnet, I get this warning:
 
Warning: array_intersect() [function.array-intersect]: Argument #1 is not an array in       
$this->view->user = $this->user;
		
	//  START  //
	$user_id = $this->view->user->user_id;

	$region_id = $this->db->fetchAll("SELECT region_id FROM regions WHERE manager_id='$user_id' ORDER BY region_id ASC");	
	$ris = $region_id[0]['region_id'];
	$rs = trim($ris, '');

	echo '<pre><strong>ASSIGNED REGIONS:</strong><br />';
		var_dump($rs);
	echo '<br /><br /></pre>';
	 
	
		
    	$this->view->headScript()->appendFile("/js/jquery-ui-1.8.5.custom.min.js");

		$this->view->sidebar = $this->view->render("sidebars/alerts.phtml");

		//get news
		$alerts = $this->user->getAlerts($_GET['sort'], $_GET['search']);
		$collection = array();
	
		foreach($alerts AS $item){
				$ars = $item["assigned_regionals"];
					echo '<pre>ASSIGNED REGIONALS:<br />';
						var_dump($ars);
					echo '</pre>';	
					
					
					
			echo '<strong>ASSIGNED REGION:</strong><br />'.$ars.'<br />';
				$ar = trim($ars, '|');
				$a = explode('|', $ar);
				var_dump($a);
				
				
				$i = array_intersect($rs, $a);
				if (!empty($i)) {
					$collection[] = new Stax_Alert($item);
		
					echo '<br /><br /><pre><strong>ALERT REGIONS:</strong><br />';
						var_dump($a);
					echo '</pre>';
				}
			
		}

Open in new window

0
Ray PaseurCommented:
Where is a test data set we can use to create the values for $rs and $a?
0
rgranlundAuthor Commented:
This got me me going in the right direction.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.