Foreach Loop in ZEND

I have the following piece of code, built around the ZEND Framework.  It does a few things.

1. It grabs a Region_Id that a Manager is Assigned to.

2. Grabs all of the Property ID's that are assigned to that region (via the region_id).

3.  Grabs all of the Invoices with the Property_ID's. (Supplied from above.

My question is this; if Number#2 Returns 11 property numbers and in Number#3 there are a total of 100 different Invoices that have those 11 Property_ID's, how do I write the Third Query?  Do I turn The Number#2 return into an array?

You can see my code below and I started with a "FOREACH Statement" but that does not work.  Any Suggestions?
//  START GRAB REGION ID ASSOCIATED WITH THIS MANAGER ID  //	
		$sel = $db->query("SELECT region_id FROM regions WHERE manager_id ='$user_id'");			 
		$rid_view = $sel->fetchColumn(0);	
//  END GRAB REGION ID ASSOCIATED WITH THIS MANAGER ID  //
		
		
//  START GRAB PROPERTY ID FROM PROPERTIES WITH THE REGION ID  //
		$selb = "SELECT property_id FROM properties WHERE region_id ='$rid_view'";	
		$stmt = $this->db->fetchAssoc($selb);
			foreach($stmt as $row) {
			$pid = $row['property_id'];				
//  END GRAB PROPERTY ID FROM PROPERTIES WITH THE REGION ID  //


//  START GRAB ALL INVOICES WITH THE PROPERTY IDS ASSIGNED TO THAT REGION  //			
		$invoices = new Db_Invoices();
		$sel = $invoices->select();
		$sel->where('property_id='.$pid)
			->order(array(trim($order), 't_id DESC'));
		echo '<pre>';
		var_dump($sel);
		if ($where) {
			$sel->where($where);
		}
		
		}

		return $invoices->fetchAll($sel);

Open in new window

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:
I think you might benefit from some structured learning about MySQL.  You might be able to get all of this information in a single MySQL query by JOINing the tables.  I'll add a couple of zones to the question and see if we can get some sharper eyes on it.
0
Ray PaseurCommented:
Here is an object lesson in why neatness, alignment and coding standards matter.  Look at this expansion of the script.  Note that there is a consistent 4-space indentation for each control structure.  This causes the code to line up so the logic is more easily revealed.

The foreach() iterator on line 12 will loop through all of the rows in $stmt, and when there are no more rows returned, it will go to the end of the control structure at line 42.  Then the return statement will return whatever was left in the last variable.  If the return statement is inside the foreach loop, it will return on the first iteration, skipping any additional rows.

Also, check the comment on line 28.  If you use error_reporting(E_ALL) PHP will tell you about this sort of thing.  $where might be defined somewhere else in the code?

// START GRAB REGION ID ASSOCIATED WITH THIS MANAGER ID  //	
$sel = $db->query("SELECT region_id FROM regions WHERE manager_id ='$user_id' LIMIT 1");	 
$rid_view = $sel->fetchColumn(0);	

// START GRAB PROPERTY ID FROM PROPERTIES WITH THE REGION ID  //
$selb = "SELECT property_id FROM properties WHERE region_id ='$rid_view'";	
$stmt = $this->db->fetchAssoc($selb);

/* DIAGNOSTIC */
var_dump($stmt);

foreach($stmt as $row) 
{
    /* DIAGNOSTIC */
    var_dump($row);
    
    $pid = $row['property_id'];
    
    /* DIAGNOSTIC */
    echo '<br>' . $pid . PHP_EOL;


    // START GRAB ALL INVOICES WITH THE PROPERTY IDS ASSIGNED TO THAT REGION  //	
    $invoices = new Db_Invoices();
    $sel = $invoices->select();
    $sel->where('property_id='.$pid)->order(array(trim($order), 't_id DESC'));
    
    // UNDEFINED VARIABLE $where
    if ($where) 
    {
	$sel->where($where);
    }
    
    /* DIAGNOSTIC */
    echo '<pre>';
    var_dump($sel);
    
    $ret = $invoices->fetchAll($sel);

    /* DIAGNOSTIC */
    var_dump($ret);
}
return $ret;

Open in new window

0
rgranlundAuthor Commented:
Everything SEEMS to be fine.  But when I run my form, I don't get any returns and there should be some:

<form method="post" action="">
<div class="filter-bar">
	<div>Search: <?= $this->formText('search', $this->search, array('style'=>'width:150px')) ?></div>
	<div>Property: <?= $this->formSelect('property', $this->property, array('style'=>'width:150px'), $this->properties) ?></div>
	<div style="float:right">
		<?= $this->formSubmit(null, 'Filter') ?>
	</div>
</div>
</form>

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Ray PaseurCommented:
In your action script, add var_dump($_POST) so you can see what is being posted.  Then you can follow the variables through the script to the places that they are used.  This is complicated somewhat by the Zend framework, but that's just something you'll have to live with if you want to use Zend.
0
rgranlundAuthor Commented:
I get this message:

  Message: Action "prearray0" does not exist and was not trapped in __call()
Stack trace:
#0 /usr/local/lib/php/Zend/Controller/Action.php(515): Zend_Controller_Action->__call('prearray0Action', Array) #1 /usr/local/lib/php/Zend/Controller/Dispatcher/Standard.php(295): Zend_Controller_Action->dispatch('prearray0Action') #2 /usr/local/lib/php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #3 /usr/local/lib/php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #4 /usr/local/lib/php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #5 /home/mrstaxin/public_html/dev/index.php(37): Zend_Application->run() #6 {main}
Request Parameters:
array ( 'controller' => 'reports', 'action' => '

array(0) {}',
  'module' => 'default',
  'search' => 'test',
  'property' => '21',
)

Open in new window

0
Ray PaseurCommented:
Sorry we don't have a Zone for Zend at EE.

At this point, it looks like you'll need to adopt some rigid debugging rules.  Strip out all of your code and start over with the bare framework.  Add things back one-at-a-time testing each addition as you rebuild the application.  Use phpMyAdmin to check the data base and test your queries.  Keep a log book of the things that you've tested.  In my experience when you reach the point that you can't debug what you've built it's time to start over and debug something as you build it  Learn about phpUnit and mock objects.  This is the fastest path to correct code.
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
rgranlundAuthor Commented:
That is a great idea Ray.  I will do just that.
0
Ray PaseurCommented:
It's what I have always found to be the best / fastest approach.  You might be interested in Martin Fowler's book about Refactoring.  Google it -- he has some good ideas for dealing with complicated code sets.

Best regards, ~Ray
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
MySQL Server

From novice to tech pro — start learning today.