Link to home
Start Free TrialLog in
Avatar of Robert Granlund
Robert GranlundFlag for United States of America

asked on

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.
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

This looks wrong to me.  var_dump() returns void.

echo var_dump($region_id);
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of Robert Granlund

ASKER

@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

I think you may want to use $haystack[0]['states'] or similar to get to the string variable.
@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

I will re-post in a while.  I realized I was looking at the wrong field.
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

Where is a test data set we can use to create the values for $rs and $a?
This got me me going in the right direction.