kohana 3.3 get next value in array - kohana

i have an array that am calling from the database using kohana 3.3 i have gotten the data to the view and i can get it with print_r($variable). i want to create a movenext, movefirst,moveprevious and movelast buttons based on the data that is in the array.
my model looks like this
public function get_customer_list(){
$cust_list= DB::select('*')->from('customers')
->order_by('id','ASC')
->execute()
->as_array();
//print_r($cust_list);
return $cust_list;
my controller looks like this
$id=#$_POST["id"];
//echo $id;
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'amending';
$this->content = View::factory('forms/amendment_next');
//$this->rightwidget =view::factory('rightwidget');
$this->amend = $amend;
$this->next = $id;
my view looks like this
foreach ($amend[0] as $key=>$value){
echo '<p><span>'.$key.'</span><input id="'.$next.'" class="contact" type="text" name="'.$key.'"value="'.$value.'" /></p>';
}
Next Record
Previous Record<button >Next Record</button><button>Last Record</button>
i dont mind using a link or a button
please assist am stack here and my brain is locked.

Try this in your view, just a some javascript code is needed (i have used jQuery)
---next() Example---
$(document).ready(function (){
var customers = <?php echo json_encode($amend) ?>;
var count = 0;
function nextItem(){
if(count >= customers.length )return;
console.log("customer:",customers[count]['id'],customers[count]['name']);
count =++count;
}
})

Your controller should look like this:
$id=#$_POST["id"];
//echo $id;
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'amending';
$inputArray = array('amend'=>$amend, 'id'=>$id);
$this->content = View::factory('forms/amendment_next',$inputArray);
//$this->rightwidget =view::factory('rightwidget');
You either have to pass the variables you want to use in your view directly to the factory as an associative array or you have to make them globally availible to the factory.
Your view should look like this:
<?
foreach ($amend[0] as $key=>$value){
echo '<p><span>'.$key.'</span><input id="'.$next.'" class="contact" type="text" name="'.$key.'"value="'.$value.'" /></p>';
}
?>
Next Record
Previous Record<button >Next Record</button><button>Last Record</button>
after passing the $inputArray to the factory the vairable $amend will be filled with $inputArray['amend'] and $id will be filled with $inputArray['id']

I used your example to project myself over the problem. and now it stands solved here is the
The Controller looks like this now
$id=#$_GET["id"];
$amend = Model::factory('amendments')->get_customer_list(#$d);
$this->page_title = 'I&M CRB Online App amending';
$this->content = View::factory('forms/amendments');
//$this->rightwidget =view::factory('rightwidget');
$this->amend = $amend;
$this->next = $id;
the view looks something like this
<?php
if (!$next){
$next = "0";}
$nextid = $next+"1";
$previd=$next-"1";
?>
First Record |
Previous Record |
move next |
Last Record
<?php
foreach (#$amend[#$next] as $key=>$value){
echo "<p><span>".$key."</span><input class='contact' type='text' name='".$key."'value='".$value."' /></p>";
}
?>
and for the last record i had to use a different model function
public function get_last_customer(){
$last_customer= DB::select()->from('customers')
->order_by('id','DESC')
->limit(1)
->execute()
->as_array();
return $last_customer;
Thank you all for the support and the answers you provided me with.

Related

Pagination doesn't work after perform search

I have a form in Laravel. I used pagination in this form and search option too. Pagination works fine when I didn't search anything. If I search something which has records more than 10 (my pagination record limit is 10), it shows result perfectly in first page but in second page or third page it shows the records of whole database. It's ignore the search criteria.
Controller Code:
public function index()
{
if (Input::has('invoiceNumber')) {
$invoiceNumber = Input::get('invoiceNumber');
$invoices = Invoice::where('invoiceNumber','like', '%'.$invoiceNumber.'%')->orderBy('date','desc')->paginate(10);
} elseif (Input::has('client')) {
$client = Input::get('client');
$invoices = Invoice::where('client','like', '%'.$client.'%')->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate') && Input::has('endDate')) {
$startDate = Input::get('startDate');
$endDate = Input::get('endDate');
$invoices = Invoice::whereBetween('date', [$startDate, $endDate])->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate')) {
$startDate = Input::get('startDate');
$invoices = Invoice::where('date', $startDate)->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('endDate')) {
$endDate = Input::get('endDate');
$invoices = Invoice::where('date', $endDate)->orderBy('date', 'desc')->paginate(10);
} else {
$invoices = DB::table('invoices')
->join('vehicles', 'invoices.vehicle', '=', 'vehicles.registration')
->select(
'invoices.*',
'vehicles.brand',
'vehicles.model',
'vehicles.seat',
'vehicles.remarks'
)
->orderBy('date', 'desc')
->paginate(10);
}
return view('invoice.index',compact('invoices'));
}
After performing search my URL look like this:
http://my-site.com/invoice?invoiceNumber=&client=&startDate=2016-01-01&endDate=2016-02-08
After this search when I click on page 2, the URL become this:
http://my-site.com/invoice?page=2
Try to use this bellow code (change as per your requirement)
return view('invoice.index')->withInvoices($invoices->appends(Input::except('page'));
Hope it's working for you...
In your view file, the render has to be like given below:
{!! $invoices->appends(Input::except('page'))->render() !!}
Simply use this on the result page.Pagination will be automatically visible at the bottom of the page
{!! $invoices->render() !!}

How to make a pagination into a (show more)button in cakephp2?

I'm new to cakephp2 and I need some help here.
The problem is that I have a view that displays a list of items from the database and there is a pagination button below. Instead of moving to the 2nd page by using the pagination, I want to have a single button that will allow you to display(append)10 more data list. But I'm not sure how it is called and how to implement it. Sorry for my bad explanation.
I think, you can play with limit parameter.
Somethink like this:
// in controller
<?php
function items() {
$limit = array_key_exists('n', $this->request->query) ? (int) $this->request->query['n'] : 10;
// some security check you need to add
$this->paginate = array(
'limit' => $limit
);
$items = $this->paginate($this->YourModel);
// some other code
$this->set(array(
'items' => $items,
'next_limit' => $limit + 10
));
}
?>
<?php
// in view file
// items output
foreach($items as $item) ...
// more button
echo "<a href='".$this->here."?n=".$next_limit."'>More</a>";
?>

XPages - Is it possible to make view column header fixed?

Just checking to see if there is a very simple way to make the view header fixed so that as you page down in the view in XPages, the header stays where it is. position=Fixed is not a property of xp:viewColumnHeader.
If you want to add the attribute of position to xp:viewColumnHeader you can use the attrs property to do that (works on 8.5.3). You code would look something like this:
<xp:viewColumnHeader ......>
<xp:this.attrs>
<xp:attr name="position" value="fixed"></xp:attr>
</xp:this.attrs>
</xp:viewColumnHeader>
But I don't think that alone would do the trick. Some time back I created a CSS snippet to make floating Banner, Title Bar and Place Bar in Application Layout control of Extension Library. You can get some ideas from that.
yes, it is possible, but requires some JavaScript coding.
I solved it for a customer recently using with the following code. The basic idea is to geht the width of the columns out of the first line of TDs, then apply this with to the THs ad set the THs to fixed afterwards.
You need to run this function after a partial update, too. Good luck.
var fixTableHeaders = function() {
var thead = dojo.query("thead")[0];
if (!thead) return;
thead.style.position = "static";
var THs = dojo.query('.xspDataTable th');
var firstTDs = dojo.query('.xspDataTable tr:first-child td');
var secondTDs = null;
if (firstTDs.length < 2) {
// categorized view, first line is a category with only one cell
// -> we need the second line
secondTDs = dojo.query('.xspDataTable tr:nth-child(2) td');
}
var w = 0;
for (var i = 0; i < THs.length; i++) {
w = dojo.coords(THs[i], true).w;
// console.log(i+" w="+w);
THs[i].style.width = (w)+"px";
if (firstTDs[i]) {
//if (secondTDs && secondTDs[i]) secondTDs[i].style.width = w+"px";
//else firstTDs[i].style.width = w+"px";
firstTDs[i].style.paddingTop = "3em";
}
}
thead.style.position = "fixed";
}
dojo.addOnLoad(fixTableHeaders);
I saw some jQuery code the other day that could make a Table Header fixed. Don't remember where it was but something that can help you should be out there.

Concrete5: Can I use $_GET variable for query string on Regular Page?

How does $_GET Variable works with Concrete5? Can I use that on regular page?
I know I can do this with single page via url segment, I'm just wondering if it is possible with regular page.
Example is :http://www.domain_name.com/about-us/?name=test...
Get-parameters are available via the controllers. In the view of a page or block use:
$this->controller->get("parameterName");
A cleaner way for custom parameters would be to define them in the function view() of the page controller. If at http://www.domain_name.com/about-us is your page and you define the view function of it's pagetype controller like this:
function view($name) {
$this->set("name", $name);
}
... and call the URL http://www.domain_name.com/about-us/test – then "test" will be passed under $name to your page view.
Note that controllers for page types must be in controllers/page_types/ and called BlablaPageTypeController ... with "PageType" literally being in there.
You can use it in a template. For instance, you can grab a variable...
$sort_by = $_GET['sort'];
And then use that variable in a PageList lookup, similar to:
$pl = new PageList();
$ctHandle = "teaching";
// Available Filters
$pl->filterByCollectionTypeHandle($ctHandle); //Filters by page type handles.
// Sorting Options
if ($sort_by == "name") {
$pl->sortByName();
} else {
$pl->sortBy('teaching_date', 'desc'); // Order by a page attribute
}
// Get the page List Results
$pages = $pl->getPage(); //Get all pages that match filter/sort criteria.
$pages = $pl->get($itemsToGet = 100, $offset = 0);
Then you can iterate over that array to print stuff out...eg
if ($pages) {
foreach ($pages as $page){
echo ''.$page->getCollectionName() . '<br />';
}
}
Props to the C5 Cheatsheet for the PageList code.

Kohana 3 ORM find_all() returns all rows regardless of where clause

I have one simple users table, and I want to find all users where email_notifications = 1.
Logic dictates that the following should work:
class Controller_Test extends Controller {
public function action_index()
{
$user = ORM::factory('user');
$user = $user->where('email_notifications', '=', 1);
$total = $user->count_all();
$users = $user->find_all();
echo $total." records found.<br/>";
foreach ($users as $v)
{
echo $v->id;
echo $v->first_name;
echo $v->last_name;
echo $v->email;
}
}
}
However, what's happening is that I am getting ALL of my users back from the DB, not just the ones with email_notifications turned on. The funny thing is, the $total value returned is the accurate number result of this query.
I am so stumped, I have no idea what the problem is here. If anyone could shed some light, I'd really appreciate it.
Thanks,
Brian
Calling count_all() will reset your model conditions. Try to use reset(FALSE) to avoid this:
$user = ORM::factory('user');
$user = $user->where('email_notifications', '=', 1);
$user->reset(FALSE);
$total = $user->count_all();
$users = $user->find_all();

Resources