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

asked on

MVC ZEND Framework and select options

I'm looking through a piece of code Built on the Zend Framework. The following line generates a Select Dropdown:

<div>Property: <?= $this->formSelect('property', $this->property, array('style'=>'width:150px'), $this->properties) ?></div>

Open in new window


it appears like this on the webpage:
<select id="property" style="width:150px" name="property">
<option label="Select a Property" value="">Select a Property</option>
<option label="Alhambra" value="42">Alhambra</option>
<option label="Banning" value="49">Banning</option>
<option label="Barstow" value="1">Barstow</option>
<option label="Bedford Pancakes" value="25">Bedford Pancakes</option>
<option label="Boulder" value="4">Boulder</option>
</select>

Open in new window


The code filters it by location; Alhambra, Banning etc.  It has an output of 10 locations in each city and then Paginates to the next page and shows 10 more results and so on.  However, when you paginate to the next page, it looses the filter.  If you had chose Alhambra and there are 100 returns, you see the first 10.  Go to the next page and it is no longer filtered by Alhambra.  Can anyone shed any light on how I maintain the filter?  Do I need to start a session?
Avatar of Robert Granlund
Robert Granlund
Flag of United States of America image

ASKER

At the bottom is the statement but I'm not sure where it goes:
public function pnlsAction()
{
	$db = Stax_Db::getConnection();
	$where = array();
	//if ($this->_getParam('search') != "") {
	//			$where[] = 'vendor_name LIKE "%' . $this->_getParam('search') . '%"';
	//			$this->view->search = $this->_getParam('search');
	//		}
	if (is_numeric($this->_getParam('property'))) {
		$where[] = 'property_id = ' . $db->quote($this->_getParam('property'), Zend_Db::INT_TYPE);
		$this->view->property = $this->_getParam('property');
	}

	$where = count($where) ? $where : $this->getProps($db);

	$records = $this->user->getAllPNLs(implode(' OR ',$where));

	/*
	$table = new Db_PNLs();
	$sel = $table->select();
	$sel->where('property_id = ?',$props[0]->property_id);
	*/
	$paginator = Zend_Paginator::factory($records);
	$paginator->setCurrentPageNumber($this->_getParam('page'));
	$paginator->setItemCountPerPage(50);

	$availableProperties = array();
	if ($this->user->access != Stax_User::ACCESS_ADMIN) {
		foreach ($this->user->getProperties() as $current) {
			$availableProperties[$current->property_id] = $current->name;
			//if(isset($_POST['property'])) {echo 'value="'.$_POST['property'].'"';}
		}
	} else {
		$availableProperties = $db->fetchPairs("SELECT property_id, name FROM properties WHERE active = '1' ORDER BY name");
	}
	$availableProperties = array(''=>'Select a Property') + $availableProperties;

	$this->view->properties = $availableProperties;
	$this->view->paginator = $paginator;
	
//if(isset($_POST['property'])) {echo 'value="'.$_POST['property'].'"';}
}

Open in new window

Avatar of Rob
Lines 9 to 12 above indicates to me that if the filter has been set then get the property (from the $_POST i suspect) and then set the property in the view:

if (is_numeric($this->_getParam('property'))) {
		$where[] = 'property_id = ' . $db->quote($this->_getParam('property'), Zend_Db::INT_TYPE);
		$this->view->property = $this->_getParam('property');
	}

Open in new window


So the first snippet of code you posted, that's in the view right? Try dumping the $this->property just before the select:

<div>Property: <? echo print_r($this->property,true) . "<br>"; echo $this->formSelect('property', $this->property, array('style'=>'width:150px'), $this->properties) ?></div>

Open in new window

@tagit, that did not work.  However, I think I have a clue.
The script says basically choose all records or choose WHERE.  How can I switch them?  The script is as follows:

	$where = array();
	if (is_numeric($this->_getParam('property'))) {
		$where[] = 'property_id = ' . $db->quote($this->_getParam('property'), Zend_Db::INT_TYPE);
		$this->view->property = $this->_getParam('property');
	}
	$where = count($where) ? $where : $this->getProps($db);

	$records = $this->user->getAllPNLs(implode(' OR ',$where));

Open in new window


How would I do this:

<div>Property: <?= $this->formSelect('property', $this->property, array('style'=>'width:150px'), $this->properties If(isset($_POST['property'] [echo 'checked="check"'}); ?></div>
I think you should check it's posting the data ok:

$this->_getParam('property') should be getting the posted value of the select element but it's also checking if it is numeric, which looks correct given the values of each town is a number.

I think you can also use $this->getPost('property'), which is worth trying to see if it holds the value you expect it to.

Regarding the "where", that code looks right in that it's saying if you selected a value in the select then use and remember that.  I suspect that the $this->_getParam('property') isn't returning what you expect it to hence my suggetion to output it to the browser so you can see if it is remembering what you are selecting each time.
The number prints to the page when it is first selected.  However, when I paginate to the next page, it is not there.  AND the weird thing is, when I go to the next page, it shows me all of the results that should be on the next page if I had not filtered.  AND if I choose the same city again and filter it shows me the second page of results.
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia 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
@tagit:
When I do this:
"Before this line add print_r($_REQUEST['property']); and post back the results"
When I filter the first time, it prints the number but when I click next it does not.  However, If I filter again, it gives me the second page of the original results.  Actually, it gives me the second page of anything I filter on.

It is not retaining the value.  In addition, when I first filter, the url looks like this:
MYSITE.com/reports/pnls/
When I click the NEXT button the ZEND Framework pagination changes the url to this:
MYSITE.com/reports/pnls/page/2/
Thanks for doing that, I just needed to be sure that it wasn't sending firm data to the php script but is in fact doing a url rewrite.
I won't be able to look at this for at least a day so feel free to request attention if you need this more urgently but I'll definitely check back then.
Are you able to show me the rendered Html of the page with this dropdown on it? ie right click the page, view source, copy and paste here