Where to see out put of Print_r() in codeigniter - codeigniter-3

i am learning codeigniter and i use methods(functions) in model to get data from the database. i am not sure whether my method is fetching correct data i want to use print_r(); exit; to debug but do not know where and how to see the output. can any one help me
for example my method in a model is
public function get_price($check)
{
$this->db->select('*');
$this->db->where('name', $check);
$this->db->from('group_test');
$query = $this->db->get();
print_r($query); exit;
return $query->result();
}

Your function is fine. The only change you need to do is use print_r function as below
public function get_price($check)
{
$this->db->select('*');
$this->db->where('name', $check);
$this->db->from('group_test');
$query = $this->db->get();
print_r($query->result());
//return $query->result();
}
So the above code will print the result as an array of objects. Let me know if you want anything else.

Related

Sort Childobjects by specific value

im currently trying to sort all Childobjects i can get by their validity time.
my current code is not sorting anything:
$children = $this->productRepository->findByParent($product->getNumber());
foreach ($children as $child) {
//debug::debug($child->getvalidityPeriod());
$product->addChild($child);
}
As allready to see in the debug, $child->getvalidityPeriod() is there to get the needed info as integer. I thought, there must be a way to sort this object array by their validityPeriod, but how do i manage that? Do i need to create a new method and filter it there or can i use something BEFORE the ForEach Loop?
Pseudo like:
$children = $this->productRepository->findByParent($product->getNumber());
ObjectSortFunction($children, 'getvalidityPeriod');
...and then for each...
Would be awesome if somebody would help me!
Okay after several tries i have a workaround, that can be used as answer, as well ;)
I wrote this method in my repo:
public function findByParentSorted($parentId)
{
$query = $this->createQuery();
$query->matching($query->equals('parent', $parentId));
$query->setOrderings(
[
'validity_period' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
]
);
return $query->execute();
}

Zend 2 CSV Action

I have created the following action, which successfully returns a CSV. however it still returns the layout within the response. From what i have read the layout is not supposed to be returned. aAnyone know how to disable this?
public function csvAction() {
$content = 'test';
$response = $this->getResponse();
$response->getHeaders()
->addHeaderLine('Content-Type', 'text/csv')
->addHeaderLine('Content-Disposition', "attachment; filename=\"my_filen.csv\"")
->addHeaderLine('Accept-Ranges', 'bytes')
->addHeaderLine('Content-Length', strlen($content));
$response->setContent($content);
return $response;
}
Try adding these at the beginning of your csvAction function:
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();

It is possible to execute MySQLi prepared statement before the other one is closed?

Lets say I have a prepared statement. The query that it prepares doesn't matter. I fetch the result like above (I can't show actual code, as it is something I don't want to show off. Please concentrate on the problem, not the examples meaningless) and I get
Fatal error: Call to a member function bind_param() on a non-object in... error. The error caused in the called object.
<?php
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
class table2Info{
private $mysqli;
public function __construct($_mysqli){
$this->mysqli = $_mysqli;
}
public function getInfo($id)
{
$db = $this->mysqli->prepare('SELECT info FROM table2 WHERE id = ? LIMIT 1');
$db->bind_param('i',$db_id);
$db_id = $id;
$db->bind_result($info);
$db->execute();
$db->fetch();
$db->close();
return $info;
}
}
$t2I = new table2Info($mysqli);
$stmt->prepare('SELECT id FROM table1 WHERE name = ?');
$stmt->bind_param('s',$name);
$name = $_GET['name'];
$stmt->bind_result($id);
$stmt->execute();
while($stmt->fetch())
{
//This will cause the fatal-error
echo $t2I->getInfo($id);
}
$stmt->close();
?>
The question is: is there a way to do another prepared statement while another one is still open? It would simplify the code for me. I can't solve this with SQL JOIN or something like that, it must be this way. Now I collect the fetched data in an array and loop through it after $stmt->close(); but that just isn't good solution. Why should I do two loops when one is better?
From the error you're getting it appears that your statement preparation failed. mysqli::prepare returns a MySQLi_STMT object or false on failure.
Check for the return value from your statement preparation that is causing the error. If it is false you can see more details by looking at mysqli::error.

How to paginate search results?

I want to paginate search results using Zend_Paginator. So I pass my data to a paginator instance:
$paginator = new Zend_Paginator (
new Zend_Paginator_Adapter_DbSelect ( $data )
);
Data is returned this way
public function getData($idArray){
$db = Zend_Db_Table::getDefaultAdapter();
$selectProgramme = new Zend_Db_Select($db);
$selectProgramme->from('programme')
->order('id DESC')
->where('id IN(?)', $idArray);
return $selectProgramme;
}
$idArray is provided by my search implementations. This all works great and I get the correct data and pagination links displayed.
However I can't paginate the result because the pagination links are not valid. So normal pagination would have following link:
mysite.de/home/index/page/1
in search I now have
mysite.de/home/search/page/1
This does not work. Any suggestions how to implement search pagination?
EDIT: I have a HomeController with two actions, index and search action. IndexAction displays all data and I can paginate it.
public function indexAction(){
//...
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbSelect($data)
);
$paginator->setItemCountPerPage(16)
->setPageRange(20)
->setCurrentPageNumber($this->_getParam('page', 1));
$this->view->data = $paginator;
}
The searchActions handles the search process:
public function searchAction(){
$response = $solr->search($this->getRequest()->getParam('search', null));
//...if items found get the data exactly the same way as in the
// index action, using Zend_Paginator_Adapter_DbSelect
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbSelect($data)
);
$paginator->setItemCountPerPage(16)
->setPageRange(20)
->setCurrentPageNumber($this->_getParam('page', 1));
$this->view->data = $paginator;
}
So like you see in the search action there is a problem with the search process when I paginate. I need to decide somehow if to search or to paginate. Any suggestions on that?
Since search required the search parameter pagination will fail because when paginating the the search parameter is null.
$sreq = $this->getRequest()->getParam('search', null);
So we need to pass this parameter whenever we paginate our search. I solve this using Zend_Session:
//get search param
$sreq = $this->getRequest()->getParam('search', null);
//store search param in session for pagination
$search = new Zend_Session_Namespace('PSearch');
if($sreq != null){
$search->psearch = $sreq;
}else{
$sreq = $search->psearch;
}
I have this at the top of my searchAction and everything works.
Not sure I understand, but is your problem that the page parameter from the url is not making it's way to the Paginator - e.g. regardless of what page you are on, it is always showing the first 20 results?
If so, have you tried manually setting the page on the paginator:
$page = $this->_getParam('page', 1);
$paginator->setCurrentPageNumber($page);
public function search()
Are you sure that you didn't mistyped here? Should be
public function searchAction()
You put your search data into $response but create paginator instance using $data (which is null)

How to get zend_lucene and zend_paginator to work

I've been using Zend Framework for a few months now. So, my knowledge is pretty good but I'm not quite an expert yet. I am trying to use zend_lucene with zend_paginator and so far not successful. I am able to use zend_lucene and index data successfully by itself and able to do use zend_paginator when querying the database, but I can't seem to combine the two. Here is a sample of what I am doing:
try {
$searchresults = $index->find($lucenequery);
}
catch (Zend_Search_Lucene_Exception $e) {
echo "Unable {$e->getMessage()}";
}
$page = $this->_getParam('page',1);
$paginator = Zend_Paginator::factory($searchresults);
$paginator->setItemCountPerPage(20);
$paginator->setCurrentPageNumber($page);
$this->view->paginator = $paginator;
Is there a different step I need to do with lucene and zend_paginator? I am really uncertain. The result I get is that for the first page results display properly. But when I hit the second page or third my results are blank. So uncertain what might be wrong as I can't find docs or tutorials in using the two together. Any help would be greatly appreciated.
I think this may work with the iterator adapter:
public function searchAction() {
$index = Zend_Search_Lucene::open('/path/to/lucene');
$results = $index->find($this->_getParam('q'));
$paginator = Zend_Paginator::factory($results);
$paginator->setCurrentPageNumber($this->_getParam('page', 1));
$paginator->setItemCountPerPage(10);
$this->view->results = $paginator;
}
Perhaps the problem you are having is that $paginator doesn't know how many search results there are..
So you may need to do that manually:
$paginator->setDefaultPageRange($results->count());