multiple delete with checkbox in ci - php

I know it has already been asked, but I'm not able to solve this. Below is my control module
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
for ($i = 0; $i < count($did); $i++)
{
$multi = $did[$i];
print_r($multi);
$this->model->delall("user", $multi);
// redirect("control/ahome");
}
}
}
and this is my model module
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in('$wh')");
}
now problem is that it's only delete single row not multiple rows

In your query string, try removing the quotes around the $wh variable.
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in ($wh)");
}
That should get it to work; however, you should be using CodeIgniter's query bindings. Something like this:
public function delall($tb,$wh)
{
$this->db->query("delete from $tb where uid in (?)", array($wh));
}
Refer to this answer for more details.

Try this,
controller,
public function ahome()
{
$this->load->model('model');
$data['user'] = $this->model->selall('user');
$this->load->view('ahome', $data);
if ($this->input->post('del'))
{
$did = $this->input->post('chk');
// print_r($did);
foreach ($did as $d_id)
{
$this->model->delall("user", $d_id);
// redirect("control/ahome");
}
}
}
Model:
public function delall($tb,$wh)
{
$this->db->where('id',$wh);
$this->db->delete($tb);
return true;
}

Related

SQL / PHP - only display the first value of my database

All is in the title. why I only get the first line of my table and not all the fields.
Function :
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT NAME FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
function application :
$oMessage = new Services_Label();
$toto = $oMessage->getlibelle();
var_dump($toto);
answer :
string(15) "Prise en charge"
table :
Going by the name of the getOneResult(), your resultset is probably being limited to one result.
Can you post the whole function to confirm?
I have no idea from your question what your table columns are called, but if you want all of the columns use SELECT *
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT * FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
It is most often better to actually name only the columns you actually want returned so you would do that like this
class Services_Label extends Services_Abstract
{
public function getlibelle()
{
$sRequest = 'SELECT NAME,COL2,COL3 FROM menu WHERE id_application = 2';
$this->executeQueries($sRequest);
$aResult = $this->getOneResult();
$this->freeStatement();
return $aResult;
}
}
this is the function getoneresult()
public function getOneResult()
{
$row = $this->getNextRow(OCI_BOTH | OCI_RETURN_NULLS);
if (is_array($row)) {
return $row[0];
} else {
return false;
}
}
You need to collect the rows using a while loop. So change your function 'getOneResult' to something like this.
public function getResults()
{
$array = array();
while ($row = $this->getNextRow(OCI_BOTH | OCI_RETURN_NULLS)) {
$array[] = $row;
}
return $array;
}

Filter models by multiples conditions in Laravel / Eloquent

I want to filter a list of products. The API is accessible in the route /q, I pass the filtering parameters like this /q?location=1,2&category=1,2,3 (if no arguments are passed, it will fetch all locations/categories.
public function getProductsByQuery()
{
$commaSeparatedLocations = Input::get('location');
$commaSeparatedCategories = Input::get('category');
if ($commaSeparatedLocations == null){
$numberOfLocations = Location::count();
for ($i=0;$i<=$numberOfLocations;$i++)
{
$locationsArray[$i] = $i;
}
} else {
$locationsArray = $this->toArray($commaSeparatedLocations);
}
if ($commaSeparatedCategories == null){
$numberOfCategories = Category::count();
for ($i=0;$i<=$numberOfCategories;$i++)
{
$categoriesArray[$i] = $i;
}
} else {
$categoriesArray = $this->toArray($commaSeparatedCategories);
}
return $products = Product::whereIn('category_id', $categoriesArray)->whereIn('location_id', $locationsArray)->paginate(config('app.products_per_page'));
}
public function toArray($commaSeparatedString)
{
return explode(",",$commaSeparatedString);
}
And well, it works. But I was wondering how can I improve my code. There must be a smarter way where not that many SQL queries are needed.
This looks much more readable to me:
public function getProductsByQuery()
{
$q = Product::query();
if (request('location')) {
$q->whereIn('location_id', $this->toArray(request('location')));
}
if (request('category')) {
$q->whereIn('category_id', $this->toArray(request('category')));
}
return $q->paginate(config('app.products_per_page'));
}
If you want to have a more complex query / with more parameters, you should have a look at https://github.com/Tucker-Eric/EloquentFilter
This is best way to filter sql query in laravel.
$q = Product::query();
$q->when(request('location'), function ($q) {
return $q->where('location', request('location'));
});
$q->when(request('category'), function ($q) {
return $q->where('category', request('category'));
});
$data['product_list'] = $q->paginate(5);
//OR
$data['product_list'] = $q->get(5);

return multiple values inside function method in codeigniter

This is my query counting rows in tblapplication
public function countallrecord() {
$query = $this->db->get('tblapplication');
return $query->num_rows();
}
and this is the function to get all the data
public function getdata() {
$query = $this->db->get('tblapplication');
return $query->result();
}
Is there any way I can make this code on one function
I'm trying to pass it here:
public function Countandviewallrecord() {
// returns both rows and count
}
Just return it as an array. Include the results and count in their respective indices:
public function get_records()
{
$result = $this->db->get('tblapplication');
$data['results'] = $result->result();
$data['count'] = $result->num_rows;
return $data;
}
When accessing the model method in your controller, the usual:
$data = $this->model_name->get_records();
echo $data['count']; // whatever number this is
if($data['count'] > 0) {
foreach($data['results'] as $row) {
echo $row->column_name; // etc blah blah ..
}
}
this Countandviewallrecord will display data as well count total records
public function Countandviewallrecord(){
$TotalRecords= $this->countallrecord();
$totalData= $this->getdata();
}

how to store database query result in a variable in view

I want to store my database query result in my view section in a variable.I am trying but not work.
My View code
$cd=''.base_url().'/video/series/';
$count = count($cd);
for ($i = 0; $i < $count; $i++)
{
print'"'.$cd[$i][1].'",';
}
My Controller code
public function series() {
$result= $this->video_model->series_list();
return $result;
}
My Model Code
function series_list()
{
$string = trim($this->input->get_post('term'));
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
$cd=''.base_url().'/video/series/'; not get any array data ,only get blank data
You can get post variable value in controller only. But you trying to get it in model. That's wrong.
Controller:
public function series() {
$string = trim($this->input->get_post('term'));
$data['result']= $this->video_model->series_list($string);
$this->load->view('folder/filename', $data);
// in your case i think folder= video and filename = series
// in this way you can pass value from controller to view
}
Model:
function series_list($string = null)
{
if($string != ''){
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
return $query->row_array();
}
else
return false;
}
View:
<?php
var_dump($data);
?>
you can get your resultset in view and can play with it as you want.
You need to pass variable inside some array(in view method) so that all variables inside common array would be accessible on view page
Controller code would be
public function series()
{
$inputData =trim(strip_tags($this->input->get_post('term')));
$data['result']= $this->video_model->series_list($inputData);
$this->load->view('directory/viewpage',$data); //$data is array with all variables inside it
}
Model code would be
function series_list($data)
{
$string = $data;
$query = $this->db->query("SELECT name FROM `series` WHERE name LIKE '%".$string."%'");
if( $query->num_rows>0)
{
return $query->row_array();
}
else
{
return false;
}
}
View code would be
<?php
if(isset($result)&&($result!=''))
{
echo "<pre/>";
print_r($result); // Or var_dump($result);
}
?>

PHP OOP: How to get database rows as objects?

I can do this when selecting a single row fine but cant quite get my head around doing this for multiple rows of data.
For the single row I simply instantiate a new object that does a number of operations behind the scenes that bascially produces a row from the database as our object.
Example:
$object = new Classname($param);
foreach($object->row as $key=>$value) {
echo $key.":".$value."\n";
}
//output
id:1
firstname:steve
lastname:took
etc...
Any clever people here able to point me in the right direction please?
NOTE: just want to be able to create an object for each row rather than the one object with nested arrays
EDIT: sorry $object->row is a member of the class that stores selected row from the database
If I got you the answer is pretty simple mysql_fetch_object
Example:
while ($row = mysql_fetch_object($result)) {
echo $row->user_id;
echo $row->fullname;
}
Why don't you consider to use an ORM (Object Relational Mapper), like Doctrine or Propel?
I prefer Doctrine: http://www.doctrine-project.org/
Enjoy! :)
Here is an example
class users extends db {
public $users_id = 0;
public $users_group = 0;
public $users_firstname = 0;
public $users_lastname = 0;
public function open($id) {
if (is_numeric($id)) {
$sql = "SELECT * FROM users WHERE users_id = '$id'";
$res = parent::DB_SELECT($sql);
if (mysql_num_rows($res) <> 1) {
return 0;
}
} else {
$sql = "SELECT * FROM users " . $id;
$res = parent::DB_SELECT($sql);
if (mysql_num_rows($res) <= 0) {
return 0;
}
}
$data = array();
while ($row = mysql_fetch_array($res)) {
$this->users_id = $row['users_id'];
$this->users_group = $row['users_group'];
$this->users_firstname = $row['users_firstname'];
$this->users_lastname = $row['users_lastname'];
$data[] = (array) $this;
}
return $data;
}
public function setUsersId($users_id) { $this->users_id = addslashes($users_id); }
public function getUsersId() { return stripslashes($this->users_id); }
public function setUsersGroup($users_group) { $this->users_group = addslashes($users_group); }
public function getUsersGroup() { return stripslashes($this->users_group); }
public function setUsersFirstname($users_firstname) { $this->users_firstname = addslashes($users_firstname); }
public function getUsersFirstname() { return stripslashes($this->users_firstname); }
public function setUsersLastname($users_lastname) { $this->users_lastname = addslashes($users_lastname); }
public function getUsersLastname() { return stripslashes($this->users_lastname); }
}
You could use MySQLi.
// Connect
$db = new mysqli('host', 'user', 'password', 'db');
// Query
$users = $db->query('SELECT * from users');
// Loop
while($user = $users->fetch_object()) {
echo $users->field;
}
// Close
$users->close();
$db->close();
More info here: http://php.net/manual/en/book.mysqli.php

Categories