CodeIgniter just show one result from my table - codeigniter

SELECT * FROM image;
I am using that query to get data from image table, when i checked it using this query directly into mysql commandline it showed 3 result like this
3 rows in set (0.00 sec);
but, when i try to fetch it from codeigniter using query or active_record, it just show me 1 result. This is my code
class Produk_model extends CI_Model{
function __construct(){
parent::__construct();
}
function get_phome(){
$this->db->select('nama_produk, nama_seo, nama_kategori, seo_kategori, nominal_harga');
$this->db->join('kategori','kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q;
}
}
function get_by_kategori($kategori_name=''){
$this->db->select('nama_kategori, seo_kategori, nama_produk, nama_seo, nominal_harga, url_img');
$this->db->join('kategori', 'kategori.id_kategori=produk.id_kategori');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->join('image','image.id_produk=produk.id_produk');
$this->db->where('seo_kategori',$kategori_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_by_produk($produk_name=''){
$this->db->select('produk.id_produk, nama_produk, nama_seo, short_desc, min_beli, nominal_harga');
$this->db->join('produk_detil','produk_detil.id_produk=produk.id_produk');
$this->db->join('harga','harga.id_produk=produk.id_produk');
$this->db->where('nama_seo', $produk_name);
$q=$this->db->get('produk');
if($q->num_rows()){
return $q;
}
}
function get_img_by_produk($produk_id=''){
$this->db->select('url_img');
$this->db->where('id_produk', $produk_id);
$q=$this->db->get('image');
if($q->num_rows()){
return $q;
}
}
}
i am trying to use get_all_pimage() with the function below from my controller
function home(){
$this->load->model('produk_model');
$data['konten']=$this->produk_model->get_phome()->result();
$data['img']=$this->produk_model->get_all_pimage()->row_array();
$this->load->view('frontend/home', $data);
}
but it just showed me 1 result, like this :
Array
(
[id_image] => 2
[id_produk] => PRD002
[seo_image] => pastel-1
[url_img] => pastel-1.jpg
)
i am confused, why it can be happened. because i don't know where is the mistake . Thank for your helping

Change function to..
function get_all_pimage(){
$q=$this->db->get('image');
if($q->num_rows()>0){
return $q->result_array();
}
}
Take row->Array() off your call...like so..
$data['img']=$this->produk_model->get_all_pimage();
Then foreach...
foreach($data['img'] as $val){
//display you data
}

Related

Laravel API delete row from db

While trying to delete rows by id from db as below , I am getting the error- count(): Parameter must be an array or an object that implements Countable. I think no id is passed to the delete() function in my code and am not sure how to pass it. Please help me with ur suggestions.
Controller
public function delete($id){
$data = PersonalDetails::where('id',$id)->delete();
// dd($data);
if(count($data)){
return response()->json(['message'=>'Successfully Deleted']);
}
else{
return response()->json(['message'=>'Delete Failed']);
}
}
Route
Route::group([
'namespace'=>'App\Http\Controllers',
'middleware' => 'api',
], function ($router) {
Route::post('delete/{id}', 'PersonalDetailsAdmin#delete');
}
The ->delete() method already returns the count of the deleted rows, so the solution would be:
public function delete($id){
$count = PersonalDetails::where('id',$id)->delete();
// dd($data);
if($count > 0 ){
return response()->json(['message'=>'Successfully Deleted']);
}
else{
return response()->json(['message'=>'Delete Failed']);
}
}

Codeigniter 3: Is there any 'where' condition added to the Query Builder so far?

I'm using CodeIgniter 3. I just need to know if there is a 'where' condition added to the query builder until now.
I'm calling a 'delete' function that deleted rows from database And it's possible to add a where condition before calling that function. Something like this:
public function delete()
{
// Here I need to know if where condition added to the db class
$this->db
->where('field', 1)
->delete('my_table');
}
public function main()
{
$this->db->where('field2', 2);
$this->delete();
}
In controller
function delete_row()
{
$this->load->model('model_name');
// Pass the id or something else to the row_delete() method
$this->model_name->row_delete($id);
}
In Model
function row_delete($id)
{
$this->db->where('id', $id);
$this->db->delete('table_name');
}
According to your example:
public function delete_method($condition,$table_name )
{
$this->db->where($condition)
$this->db->delete($table_name);
}
public function main()
{
$condition = [
'field1'=>1,
'field2'=>2
];
$table_name = 'my_table';
$this->delete_method($condition,$table_name );
}
I found a solution.
The only thing I need to do is getting a select query and search for 'where' clause inside it:
public function delete()
{
// Here I need to know if where condition added to the db class
$sql = $this->db->get_compiled_select(NULL, FALSE);
$has_where = stripos($sql, 'where') !== FALSE;
// $has_where is TRUE if there is a where condition defined until now
$this->db
->where('field', 1)
->delete('my_table');
}
public function main()
{
$this->db->where('field2', 2);
$this->delete();
}

laravel many to many with chaining where clause

I have a given table :
tools toolparts parts part_details
----- --------- ----- ------------
id* id* id* id*
name tool_id name part_id
part_id total (int)
----- --------- ----- ------------
the relation between Tools and Parts is ManyToMany. and the relation between parts and part_details is one to many.
with Laravel model, how can I get tool with part that has the biggest part_details.total ??
//tool model
public function parts()
{
return $this->belongsToMany('App\Part', 'tool_part');
}
//part model
public function tools()
{
return $this->belongsToMany('App\Tool', 'tool_part')
}
public function details(){
return $this->hasMany('App\Part_detail');
}
//partDetail model
public function part(){
return $this->belongsTo('App\Part');
}
Controller
public function index()
{
$tools = Tool::with('parts', 'parts.details')->has('parts')->get();
return $tools;
}
what I expected is something like :
Controller
public function index()
{
$tool = Tool::with('SinglePartThatHasHigestTotalInPartDetail');
}
Any Idea ??
You can use Laravel aggregates for querying to get the desired result,
In your code use max() function,
public function index()
{
$tool = Tool::with(['parts.part_details' => function ($query) {
$max = $query->max('total');
$query->where('total',$max);
})->first();
}
I haven't tested this but you can do like this.
Comment if you will get any errors.
I Manage my problem with "hacky" ways. if someone have a better and more elegant solution, please tell me.
//tool model
public function partWithHighestTotalDelivery($trans_date = null){
if (is_null($trans_date)) {
$trans_date = date('Y-m-d');
}
$parts = $this->parts;
$highest_total_delivery = 0;
foreach ($parts as $key => $part) {
$part->detail;
$total_delivery = $part->first_value;
if (isset( $part->detail->total_delivery )) {
$total_delivery += $part->detail->total_delivery;
}
if ($highest_total_delivery < $total_delivery ) {
$highest_total_delivery = $total_delivery;
$part->total_delivery = $highest_total_delivery;
$result = $part;
}
}
if (!isset($result)) {
$result = null;
}
$this->part = $result;
}
In controller i have :
public function index(Request $request){
$tools = Tool::has('parts')
->get();
$tools->each(function($tool){
$tool->partWithHighestTotalDelivery();
});
return $tools;
}
with this, I need to run tool->partWithHighestTotalDelivery() tools.count times. which is take noticeable process if the tools is many.
and also, the code I post and the question I ask has a slightly difference.that's for a simplicity sake's
Use the the hasManyThrough Relationship to get the all part details related to tool and then you can check the one by one record and get the highest total of the tool part.
// Tool Model
public function partsdetails()
{
return $this->hasManyThrough('App\PartDetail', 'App\Part','tool_id','part_id');
}
In Your controller
$data = Tool::all();
$array = [];
if(isset($data) && !empty($data)) {
foreach ($data as $key => $value) {
$array[$value->id] = Tool::find($value->id)->partsdetails()->max('total');
}
}
if(is_array($array) && !empty($array)) {
$maxs = array_keys($array, max($array));
print_r($maxs);// This array return the max total of the tool related parts
}
else{
echo "No Data Available";
}
You can start with the part detail and get the tool(s) from there:
$maxPartDetail = Part_detail::orderByDesc('total')->first();
$tools = $maxPartDetail->part->tools;

Codeigniter - Trying to count articles view

Im working with codeigniter and have a controller and 3 model functions to read, insert and update a column of table.
i want to get count of view from db and update it per each view!
but after the update an extra row added to table! with zero value for content_id.
please check once!
this is my controller:
public function daily_report($id="")
{
$counter=$this->home_model->counter($id);
if($counter)
{
$view=$this->home_model->counter($id)->row()->view;
$views=$view+1;
$this->home_model->update_counter($views,$id);
}
else{
$views=1;
$this->home_model->set_counter($views,$id);
}
}
This is the model functions:
public function counter($id)
{
$code=$id;
$lang=$this->session->userdata('lang');
$data = $this->db
->select('view')
->from('tbl_views')
->where("content_id",$code)
->where("language",$lang)
->get();
if ($data->num_rows() > 0) {
return $data;
}else{
return false;
}
}
public function set_counter($views,$id)
{
$data = array(
'content_id' => $id ,
'view' => $views,
'language'=>$this->session->userdata('lang')
);
$this->db->insert('tbl_views', $data);
}
public function update_counter($views,$id)
{
$data = array(
'view' => $views,
);
$this->db->where('content_id', $id);
$this->db->update('tbl_views', $data);
}

codeigniter passing count_all_results to view

I'm using the count_all_results() function to return a user's number of languages spoken. But when I try to pass the number to the view, I keep getting a php undefined variable (for $lang_cnt). Below is my code:
Model
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Controller
function showLangCount() {
$data['lang_cnt'] = $this->language_model->countLanguages($id);
$this->load->view('lang_view', $data);
}
View
<p>This user speaks <?php echo $lang_cnt; ?> languages.</p>
One problem is that your model function takes two arguments:
function countLanguages($id, $cnt_languages)
But when you call it you are only passing one argument:
$this->language_model->countLanguages($cnt_languages);
And an even bigger problem, as Rocket points out, is that countLanguages doesn't return anything. Try this:
function countLanguages($id) {
$this->db->where('user_id', $id)->from('languages');
return $this->db->count_all_results();
}
Always check your model functions if they return value or not. Try this:
function showLangCount() {
if($this->language_model->countLanguages($id))
{
$data['lang_cnt'] = $this->language_model->countLanguages($id);
}
else
{
$data['lang_cnt'] = NULL;
}
$this->load->view('lang_view', $data);
}
Its better to use:
return $query->num_rows();
to return the number of rows effected...

Resources