How do i correct jqgrid footer information? - cakephp

I am using jqgrid in one of my application.
Right now i am facing a strange problem and do not know how to rectify it?
Actually in one of the reports (having multiple link) jqgrid is showing wrong footer information that is its showing Page 0 of 0 even if there are records in the table.
This is the code which runs:
if( isset($this->params['named']['ajax']) && $this->params['named']['ajax'] == '1' )
{
$this->autoRender = false;
// get how many rows we want to have into the grid - rowNum parameter in the grid
$limit = $this->params['url']['rows'];
// get index row - i.e. user click to sort. At first time sortname parameter -
// after that the index from colModel
$sidx = $this->params['url']['sidx'];
// sorting order - at first time sortorder
$sord = $this->params['url']['sord'];
$page = $this->params['url']['page'];
// if we not pass at first time index use the first column for the index or what you want
if( !$sidx ) $sidx = 1;
// calculate the number of rows for the query. We need this for paging the result
$findconditions = array();
if(!empty($this->params['named']['batch']))
array_push(&$findconditions, array('Batch.id' => $this->params['named']['batch'] ));
$row = $this->Placement->find('count',array(
'link' => array(
'Student' => array(
'Batch'
)
),
'conditions'=> $findconditions
));
$count = $row;
// calculate the total pages for the query
if( $count > 0 )
{
$total_pages = ceil($count / $limit);
}
else
{
$total_pages = 0;
}
// if for some reasons the requested page is greater than the total
// set the requested page to total page
if( $page > $total_pages ) $page = $total_pages;
// calculate the starting position of the rows
$start = $limit * $page - $limit;
// if for some reasons start position is negative set it to 0
// typical case is that the user type 0 for the requested page
if( $start < 0 ) $start = 0;
// the actual query for the grid data
$limit_range = $start . "," . $limit;
$sort_range = $this->modelClass . '.' . $sidx . " " . $sord;
$this->Placement->recursive = -1;
$where='';
if( $this->params['url']['_search'] == 'true' )
{
//pr($this->params);
$searchconditions = array();
if( isset($this->params['named']['batch']) && !empty($this->params['named']['batch']) )
{
$where.= " Batch.id =".$this->params['named']['batch'];
}
if( isset($this->params['url']['isstillworking']) && !empty($this->params['url']['isstillworking']) )
{
$where.= " AND Placement.isstillworking ='".$this->params['url']['isstillworking']."'";
}
if( isset($this->params['url']['studentname']) && !empty($this->params['url']['studentname']) )
{
$where.=" AND Student.fullname LIKE '" .$this->params['url']['studentname'] . "%'";
}
if( isset($this->params['url']['companyname']) && !empty($this->params['url']['companyname']) )
{
$where.=" AND Company.name LIKE '" .$this->params['url']['companyname'] . "%'";
}
if( isset($this->params['url']['salary']) && !empty($this->params['url']['salary']) )
{
$where.= " AND Placement.salary =".$this->params['url']['salary'];
}
if( isset($this->params['url']['contactnumber1']) && !empty($this->params['url']['contactnumber1']) )
{
$where.= " AND Student.contactnumber1 =".$this->params['url']['contactnumber1'];
}
if( isset($this->params['url']['batchname']) && !empty($this->params['url']['batchname']) )
{
$where.=" AND Batch.name LIKE '" .$this->params['url']['batchname'] . "%'";
}
$sql="SELECT Student.fullname,
Placement.isstillworking,
Company.id,
Company.name,
Placement.id,
Placement.salary,
Placement.created,
Student.id,
Student.contactnumber1,
Batch.id,
Batch.name
FROM placements Placement
INNER JOIN (
SELECT student_id, isstillworking, max( created ) AS other_col
FROM placements
GROUP BY student_id
) AS b ON Placement.student_id = b.student_id
AND Placement.created = b.other_col
INNER JOIN students Student ON ( Student.id = Placement.student_id )
INNER JOIN batches Batch ON ( Student.batch_id = Batch.id )
INNER JOIN companies Company ON ( Company.id = Placement.company_id )
WHERE ".$where.
" AND Student.type='student'
AND Student.trainingcompleted=1
ORDER BY ".$sort_range."
LIMIT ".$limit_range
;
$result=$this->Placement->query($sql);
}
else
{
$sql="SELECT Student.fullname,
Placement.isstillworking,
Company.id,
Company.name,
Placement.id,
Placement.salary,
Placement.created,
Student.id,
Student.contactnumber1,
Batch.id,
Batch.name
FROM placements Placement
INNER JOIN (
SELECT student_id, isstillworking, max( created ) AS other_col
FROM placements
GROUP BY student_id
) AS b ON Placement.student_id = b.student_id
AND Placement.created = b.other_col
INNER JOIN students Student ON ( Student.id = Placement.student_id )
INNER JOIN batches Batch ON ( Student.batch_id = Batch.id )
INNER JOIN companies Company ON ( Company.id = Placement.company_id )
WHERE Batch.id =
".$this->params['named']['batch'].
" AND Student.type='student'
AND Student.trainingcompleted=1
ORDER BY ".$sort_range."
LIMIT ".$limit_range
;
$result=$this->Placement->query($sql);
}
$i = 0;
$response->page = $page;
$response->total = $total_pages;
$response->records = $count;
//pr($result);
foreach($result as $result)
{
$response->rows[$i]['id'] = $result['Placement']['id'];
$student = "<a href='" . APP_URL . "placements/report18/studentid:" . $result['Student']['id']."'>" . $result['Student']['fullname'] . "</a>";
$company = "<a href='" . APP_URL . "companies/view/" . $result['Company']['id'] . "'>" . $result['Company']['name'] . "</a>";
$batch = "<a href='" . APP_URL . "batches/view/" . $result['Batch']['id'] . "'>" . $result['Batch']['name'] . "</a>";
$contactnumber1 =$result['Student']['contactnumber1'];
$response->rows[$i]['cell'] = array($student, $result['Placement']['isstillworking'], $result['Company']['name'], $result['Placement']['salary'], $contactnumber1, $batch);
$i++;
}
echo json_encode($response);
}
I am also attaching the screen shot for reference.
Please help me on this.
Regards,
Pankaj

Why on earth would you not use Cake's ORM layer and Pagination class? If you use Model->query() you have to escape input yourself!

Related

Combined two arrays and the result returned has an empty key and value

I have an excel sheet which is read and converted to an array.
Here is the code :
$filename = $_FILES['importProjects']['tmp_name'];
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$i = 0;
$all_sheet = $objPHPExcel->getSheetCount(); //total number of sheets
$arrayInsertProject = array();
$arrayUpdateProject = array();
while ($i < $all_sheet){
$objPHPExcel->setActiveSheetIndex($i);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of rows from the sheet
$column = 'A';
$project_cnt=1;
$dataIns = array();
for ($row = 1; $row <= $arrayCount; $row++) { //loop through rows
if($row ==1){
$highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
$rowDataHeader = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$rowDataHeader = array_reduce($rowDataHeader, 'array_merge', array());
}else {
$highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
$rowData = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$dataIns = array_combine($rowDataHeader, $rowData);
}
}
}
When I print this array $dataIns, I get something like this :
Array
(
[intProjectSlNo] => 1069
[chvProjectNameEng] => New
[slug] => this-is -test
[nchvSecType] => 1754349
[] =>
)
How to remove the empty key => value pair ?
Add this after while loop of your code
foreach($dataIns as $key=>$value)
{
if(is_null($value) || $value == '')
unset($dataIns[$key]);
}
print_r($dataIns);

mediawiki query NOT IN syntax

I have a database query and I would like to add the condition:
$not_these_ids = array(22, 34, 55, 66)
'page_id' != $not_these_ids;
How do I write it in the mediawiki syntax?
`
$dbr = wfGetDB( DB_SLAVE ); ...
$conds = array( 'rev_id = page_latest',
'page_id' != $not_these_ids
); `
but it doesn't work.
You can just pass an array of strings, one for each conditions, to the third parameter of select().
Example from maintenance/Maintenance.php:
$dbw = wfGetDB( DB_MASTER );
$dbw->begin( __METHOD__ );
[...]
if ( count( $latestRevs ) > 0 ) {
$revConds[] = 'rev_id NOT IN (' . $dbw->makeList( $latestRevs ) . ')';
}
$res = $dbw->select( 'revision', 'rev_id', $revConds, __METHOD__ );
$oldRevs = array();
foreach ( $res as $row ) {
$oldRevs[] = $row->rev_id;
}
$this->output( "done.\n" );

yii database query inside loop

I didn't get the correct filter out for the following action. Here $val is an array of checked values. But the query executes only for the last element of the array even though I used the foreach loop. Please, can anyone help me?
if( isset($_POST['state']) && count($_POST['state']) > 0 ){
foreach($_POST['state'] as $row => $val ){
$criteria = new CDbCriteria;
$criteria->select='*';
$criteria->condition='pjt_user_id=:pjt_user_id && pjt_pdt_status=:pjt_pdt_status';
$criteria->params=array(':pjt_user_id'=> $user_id, ':pjt_pdt_status'=> $val);
$criteria -> order = 'pjt_id';
$projects= ProjectModel::model() -> findAll($criteria);
}
$this->render('index', array('projects'=>$projects));
Thanks Martin Komara....
I got the solution ...
I just use OR condition in SQL.
if(isset($_POST['state']) && count($_POST['state']) > 0 ){
$i = 0;
foreach($_POST['state'] as $data){
$condit[":$data"] = $data;
if($i!=0){
$var .= ' || ';
}
$var .= " pjt_pdt_status=:$data";
$i++;
}
$var = ' AND (' .$var.' )';
}
$criteria = new CDbCriteria;
$criteria->select='*';
$criteria->condition='pjt_user_id=:pjt_user_id' .$var ;
$criteria->params=$condit;
$criteria -> order = 'pjt_id';
$projects= ProjectModel::model() -> findAll($criteria);
This would be better - much more readable:
if (isset($_POST['state']) && count($_POST['state']) > 0 ) {
$criteria = new CDbCriteria;
$criteria->addInCondition('pjt_pdt_status', $_POST['state']);
$criteria->addColumnCondition(array('pjt_user_id' => $user_id));
$criteria->order = 'pjt_id';
$projects = ProjectModel::model()->findAll($criteria);
}

How to return value of 2 data in foreach which each data has 5 rows in model of codeigniter to view

I have a similar problem that the function only return the first loop data.
The result should be 2 loops and each loop has rows data.
For example, The first loop is A and has 5 rows data and second loop is B and has 5 rows.
The return value only A with 5 rows, it sould be 10 rows. 5 rows A + 5 rows B.
How is the code should be?
This function in model is bellow
function getDataForm($data_search)
{
$i=0;
if(!empty($data_search['arr_idacl']))
{
foreach($data_search['arr_idacl'] as $idx => $val)
{
if(!empty($operatingHour))
{
$sql = "SELECT be_acl_resources.description as description,DATE_FORMAT(tbl_camera_detail.start_time,'%d %M %Y ') as datadate, SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'IN',tbl_camera_detail.enters*(-1),0))+SUM(IF(tbl_consolid_detail.operation = 'plus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits,0))+SUM(IF(tbl_consolid_detail.operation = 'minus' AND tbl_consolid_detail.direction = 'OUT',tbl_camera_detail.exits*(-1),0)) as total_enter FROM tbl_consolid JOIN tbl_consolid_detail ON tbl_consolid.id = tbl_consolid_detail.consolid_id JOIN tbl_config_data ON tbl_consolid_detail.config_data_id = tbl_config_data.id JOIN tbl_camera ON tbl_config_data.id_acl = tbl_camera.id_acl JOIN tbl_camera_detail ON tbl_camera.id = tbl_camera_detail.camera_id JOIN be_acl_resources ON tbl_consolid.id_acl = be_acl_resources.id WHERE tbl_camera.date >= '".$data_search['start_date']."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') >= '".$startOpratingHour."' AND date_format(tbl_camera_detail.start_time, '%H, %i %S') <= '".$operatingHour['finish_time']."' AND tbl_camera.date <= '".$data_search['end_date']."' AND tbl_consolid.id_acl = '".$val."' GROUP BY tbl_camera.date";
echo $sql;
$query = $this->db->query($sql);
$result_data[] = $query->result();
//return $result;
}
$i++;
}
return $result_data;
}
}
Declare $result_data = array(); just before if(!empty($data_search['arr_idacl'])) condition.
Replace $result_data[] = $query->result(); with $result_data[$i] = $query->result();
Update:
Just close to the answer. :) Try this:
Replace $result_data[] = $query->result(); with $result_data[$i][] = $query->result();

DBI: How to find the right data-types for unknown data?

With of these two approaches would you prefer: trying to find the right data-types or simply using always varchar?
# ...
use HTML::TableExtract;
my $te = HTML::TableExtract->new( headers => [ 'some headers', 'one', 'two' ], keep_headers => 1 );
$te->parse( $html_string );
die $te->tables if $te->tables != 1;
( my $grid ) = $te->tables;
use DBI;
my $dbh = DBI->connect( ... ) or die $DBI::errstr;
my $table = 'my_test_table';
my #rows = $grid->rows;
my $header_row = shift #rows;
##### version 1 ####
use Data::Types qw(:all);
my #create_row;
for my $col ( 0 .. $#$header_row ) {
my ( $count, $int, $float ) = ( 0, 0, 0 );
my $longest = 0;
for my $row ( #rows ) {
$longest = length $row->[$col] if length $row->[$col] > $longest;
$int++ if is_int( $row->[$col] );
$float++ if is_float( $row->[$col] );
$count++;
}
if ( $int == $count ) {
$create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' int';
}
elsif ( $float == $count ) {
$create_row[$col] = $dbh->quote( $header_row->[$col] ) . ' float';
}
else {
$create_row[$col] = $dbh->quote( $header_row->[$col] ) . " char($longest)";
}
}
$sql = sprintf "CREATE TABLE $table ( %s )",
join( ', ', #create_row );
$dbh->do( $sql );
$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )",
join( ',', map { $dbh->quote( $_ ) } #$header_row ), join( ',', ('?') x #$header_row );
my $sth = $dbh->prepare( $sql );
#### version 2 ####
# always varchar
$sql = sprintf "CREATE TABLE $table ( %s )",
join( ', ', map { "'$_' varchar(60)" } #$header_row );
$dbh->do( $sql );
$sql = sprintf "INSERT INTO $table ( %s ) VALUES( %s )",
join( ',', map { $dbh->quote( $_ ) } #$header_row ), join( ',', ('?') x #$header_row );
my $sth = $dbh->prepare( $sql );
If the table you're processing will not change, and if the column will only be used for that single table's data, then it is safe to guess a data type that seems to fit (version 1).
However, if you plan to add any more data to that column then you'd need to keep everything as varchars in case there's some data of a different type in future (version 2).

Resources