Codeigniter search with pagination error - codeigniter-2

Hello I have a problem with my search in CI. In the first page the results are displayed ok, but on second page it shows a 1064 SQL error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND cpt_echip.cpt_echip_nr_inventar LIKE '%%' LIMIT 10 , 10' at line 1
Here is the code (functions that are used):
MODEL:
function search($start, $limit){
$match = $this->input->post('search');
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$q = $this->db->query("SELECT * FROM (cpt_echip)LEFT OUTER JOIN cpt_utl ON cpt_utl.cpt_utl_marca = cpt_echip.cpt_utl_marca WHERE cpt_echip.cpt_tip_echip_id = $tip_echip AND cpt_echip.cpt_echip_nr_inventar LIKE '%$match%' LIMIT $start , $limit");
$rezultat = $q->result();
return $rezultat;
}
function num_filter(){
$tip_echip = $this->input->post('cpt_tip_echip_nume');
$this->db->where('cpt_tip_echip_id', $tip_echip);
$q = $this->db->get('cpt_echip');
$number = $q->num_rows();
return $number;
}
CONTROLLER:
function search(){
$data = $this->helpdesk_model->general();
$start_row = $this->uri->segment(3);
$per_page = 10;
if(trim($start_row) == ""){
$start_row = 0;
}
$config['base_url'] = base_url() . '/index.php/helpdesk/search/';
$config['total_rows'] = $this->helpdesk_model->num_filter();
$config['per_page'] = $per_page;
$config['num_links'] = 10;
$config['first_link'] = 'Prima pagina';
$config['last_link'] = 'Ultima pagina';
$this->pagination->initialize($config);
$data['pagini'] = $this->pagination->create_links();
$data['query'] = $this->helpdesk_model->search($start_row, $per_page);
$this->load->view('rezultat', $data);
}
VIEW:
<table border="1">
<tr>
<th>Nr. Inventar</th>
<th>Nume</th>
<th>Utilizator</th>
</tr>
<?php foreach($query as $row): ?>
<tr>
<td><?php echo anchor('helpdesk/detalii_echipament/'.$row->cpt_echip_nr_inventar, $row->cpt_echip_nr_inventar, array('data-fancybox-type'=>'iframe', 'class'=>'fancybox')); ?></td>
<td><?php echo $row->cpt_echip_nume; ?></td>
<td><?php echo $row->cpt_utl_nume; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $pagini; ?>
Without the search and filter, the pagination works fine.

It means the $_POST array is empty. There will not be anything posted when you navigate using links to the second page. You can pass the search data by url or store in session at first submission and use it.
Just check using print_r($_POST) to see what is there $_POST array.

Related

Get the total/sum of commission per user and display it

My target is to display the total commission per user. I tried it already but the only thing I'm getting is commission of myself which is 503,582.26. I want to display the commission dynamically. For example: ID 1 = 500, ID 2 = 300, ID = 600. I have provided screenshot below for more explanation. Any help will be appreciated. Thank you
Views:
<?php
foreach($result as $rows) { $uuid = $rows->uuid;
$userID = $rows->userID; ?>
<?php if($rows->uuid===$_SESSION['uid']): ?>
<tr>
<td><?php echo $rows->firstname; ?></td>
<td><?php echo $rows->mobile; ?></td>
<td><?php echo $rows->account_type; ?></td>
<td><?php echo $rows->currentPoints; ?></td>
<td> <?php $sum = number_format($commBalance -$commWithdrawn,2); echo ($sum);?></td>
<td>
Controller:
public function agents()
{
$data['activeNav'] = "";
$data['subnav'] = "networks";
$this->header($data);
$this->nav();
$data['result'] = $this->networks->getAllData();
$data['commBalance']=$this->load->networks->gettotalcommi();
$data['commWithdrawn']=$this->load->networks->gettotalWithdrawn();
$data['meronResult'] = $this->networks->meronBets();
$data['walaResult'] = $this->networks->walaBets();
$data['tagent']=$this->load->networks->gettotalagent();
$this->load->view('agents', $data);
$this->footer();
}
Model:
function gettotalcommi(){
$reqcommi= $this->session->userdata('uid');
$a = $this->input->post('userID');
$this->db->select_sum('amount');
$this->db->where('commTo',$reqcommi);
$bind = array('commType', 'in');
$this->db->where_in('commType', $bind);
$result = $this->db->get('agent_commission_history')->row();
return $result->amount;
}
function gettotalWithdrawn(){
$a = $this->input->post('userID');
$reqcommi= $this->session->userdata('uid');
$this->db->select_sum('amount');
$this->db->where('commTo',$reqcommi);
$bind = array('commType', 'out');
$this->db->where_in('commType', $bind);
$result = $this->db->get('agent_commission_history')->row();
return $result->amount;
}
If I understand correctly, you want retrieve data by post value USERID. And also show the individual 'contributions'.
What i'm seeing in your implementation, is that your summing all contributions by USERID 1 for example, but not grouping the values by individual contributors or retrieving the individual contributor values.
Group by agent example:
function gettotalcommi(){
return $this->db->select_sum('amount')
->from('agent_commission_history')
->where('commTo', $this->session->userdata('uid'))
->where_in('commType', ['commType', 'in'])
->group_by('commFrom')
->get()->row()->amount;
}
Individual get:
function gettotalcommi(){
return $this->db->select_sum('amount')
->from('agent_commission_history')
->where('commTo', $this->session->userdata('uid'))
// individual
->where('commFrom', $this->input->post('userID'))
->where_in('commType', ['commType', 'in'])
->get()->row()->amount;
}
I hope this helps. If i'm misinterpreting your question, lemme know.

How to get url of a category in magento

am trying to display all the second level categories and my code is
<?php $storeId = Mage::app()->getStore()->getId();
//Get category model
$_category = Mage::getModel('catalog/category')->setStoreId($storeId);
$_categoryCollection = $_category->getCollection();
$_categoryCollectionIds = $_categoryCollection->getAllIds();
//Remove root category from array
unset($_categoryCollectionIds[0], $_categoryCollectionIds[1]);
?>
<div id="accordian_hover">
<?php
$o = null;
$o .= '<ul>';
foreach ($_categoryCollectionIds as $catId) {
$_category = $_category->load($catId);
if($_category->getLevel() == 2) {
$catChildren = $_category->getChildren();
if(!empty($catChildren)) {
$o .= '<li> '.$_category->getName().'';
$o .= '<ul>';
$categoryChildren = explode(",", $catChildren);
foreach ($categoryChildren as $categoryChildId) {
/* #var $_childCategory Mage_Catalog_Model_Category */
$_childCategory = $_category = Mage::getModel('catalog/category')->setStoreId($storeId)->load($categoryChildId);
$o .= '<li>'.$_childCategory->getName().'</li>';
// If you wish to display the total number of products for each category, uncomment this line
// $o .= '<span class="totalNumberOfProducts">'.$_childCategory->getProductCollection()->count().'</span>';
}
$o .= '</ul></li>';
}
}
} $o .='</ul>';
echo $o;
?>
</div>
But the top menu url is wrong all other are displaying correct but the main category url is wrong(2nd category) Please help me , and i have to display the third level menu also when the user hover on category...
Live Link http://toolskaart.com/
Hope the below code will solve your problem.
Mage::getModel('catalog/category')->load($_category->getId())->getUrl()
It works for me.

codeigniter grabbing array from database

I'm new to codeigniter and followed the guides but seem to be missing something. I have a simple database with customer records in it. My first goal in codeigniter is to simple list all my customers.
here is my controller:
public function index()
{
$this->load->model('HomeModel'); //loads the HomeModel model
$data = $this->HomeModel->function1(); //loads the function (function1) from the model
$this->load->view('index', $data);
}
Here is my model:
public function function1()
{
$query = $this->db->get('work_orders');
return $query->result();
}
here is my view:
<table class="table table-bordered table-striped table-highlight" id="invoice-details">
<thead>
<tr>
<th>Work Order ID</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<?php
foreach ($data->result() as $row);?>
<tr class="even gradeC">
<td><a href="/WorkOrders/viewWo/<?php echo $row['id']; ?>">
<?php echo $row['id']; ?></a></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo Logic\System\Lib\Helper::trunc(htmlentities($row['description']), 8); ?></td>
</tr>
<?php endforeach; ?>
</table>
Change on model :
public function function1()
{
$query = $this->db->get('work_orders');
//return $query->result();
return $query->result_array();
}
Change on controller :
public function index()
{
$this->load->model('HomeModel');
//$data = $this->HomeModel->function1();
$data['result'] = $this->HomeModel->function1();
$this->load->view('index', $data);
}
Change on view :
//foreach ($data->result() as $row);
if(is_array($result)&&!empty($result))
foreach ($result as $row);
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Views : Codeigniter User Guide
And Since you are using array on your view like $row['id'], you got to use result_array on model to return result set array:
This function returns the query result as a pure array, or an empty array when no result is produced. Typically you'll use this in a foreach loop, like this:
$query = $this->db->query("YOUR QUERY");
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
Generating Query Results : Codeigniter
You can use $this->db->join();
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
Produces:
SELECT * FROM blogs
JOIN comments ON comments.id = blogs.id
Go through active record class guide .
Active Record Class
In ur controller,u must pass the values as subarray of data array$data['result'].After which u can simply call it in view as $result .
Controller
$data['result'] = $this->HomeModel->function1();
And In view,
foreach ($result as $row){
<?php echo $row->id; ?>
..
}

Invalid image when using force download in codeigniter

I use download helper in codeigneter,, when i download file jpeg and open it,, it becomes invalid...
What should i do..
Thank you...
Controller
function downloadSuratElektronik($file)
{
$this->load->helper('download');
$file_data = file_get_contents(base_url()."data/".$file);
$file_name = $file;
force_download($file_name, $file_data);
}
Model
function getSuratKeluar(){
$data = $this->db->get('surat_keluar');
if ($data->num_rows() > 0){
foreach ($data->result() as $row){
$hasil[]=$row;
}
return $hasil;
}
}
View
<table>
<tr>
<td>File Upload</td>
</tr>
<?php
foreach ($hasil as $data) :
?>
<tr>
<td>/<?=$data->link_upload?></td>
</tr>
<?php endforeach; ?>
</table>
I got the same problem, i fixed it using other method... if u still want it
$file_name = "picture.jpg"
$file_path = "your path"
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$file_name");
ob_clean(); flush();
readfile($file_path);

in one foreach call second foreach! codeigniter

hier is the problem:
in codeigniter in controller I have next code:
$query_not_voted = "SELECT * FROM photos WHERE p_id NOT IN (SELECT distinct p_id FROM p_votes where u_id = ".$this->session->userdata('u_id').") LIMIT ".$this->db->escape_str($segment_url).", ".$config['per_page'];
$q = $this->db->query($query_not_voted);
$data['myphotos'] = $q->result_array();
foreach($data['myphotos'] as $key=>$val)
{
$query_g = "SELECT * FROM users WHERE u_id = (SELECT u_id FROM p_votes WHERE p_id = ".$val['p_id'].")";
$q_2 = $this->db->query($query_g);
$data['allvotess'] = $q_2->result_array();
$query_u = "SELECT * FROM users WHERE u_id = ".$val['u_id']." LIMIT 0, 5";
$q_1 = $this->db->query($query_u);
$data['author'] = $q_1->result_array();
}
So now I have $data['myphotos'] and this is can be outputted in view with next codes:
<?php foreach ($myphotos as $keys => $myphoto){ ?>
<div id="voteblock">
<div id="voteleft">
<img src="<?php echo base_url().$myphoto['p_thumb']; ?>" />
</div>
<?php } ?>
but how can I output $data['allvotess'] in last foreach loop?
I tried to do same, but didn't work out.
Then I tried to push the results inside $data['myphotos'] and no good result!
So What I'm Doing???
Just need someone to help me with foreachloop insite foreachloop!!!
For a week I just can't do, I can even pay for solution right now!!!
I posted also at codeingiter forum, but they say it's easy hier is link
Generally in views I use the more semantic way to write foreach and if statements, and I'll also illustrate a tiered foreach loop...
$array1 = array(1,2,3,4);
$array2 = array(1,2,3,4);
$array3 = array(1,2,3,4);
$second_array[] = $array1;
$second_array[] = $array2;
$second_array[] = $array3;
so now we have $second_array that is a multi-dimensional array...
$data->second_array = $second_array;
$this->load->view('your/view',$data);
in a view file...
<? foreach($second_array as $array): ?> //<--start first foreach loop
<div>do some stuff</div>
<? foreach($array as $array_item): ?> //<--start second foreach loop, inside the first
<div>do some other stuff for every item in $array</div>
<? endforeach; ?> //<--end first foreach
<? endforeach; ?> //<--end second foreach

Resources