PHP Recursive database loop - php

I want to get the main parent of chosen subcategory. I made function which recursively loop the databasse and it even get the ID but I can only echo it. I cannot return it into variable. I need to work further with the returned ID. Here is my code.
public function check_parent($parent)
{
$q = $this->db->get_where('ci_categories', array('cat_id'=>$parent));
$r = $q->row();
if ($r->cat_child > 0)
{
$this->check_parent($r->cat_child);
} else {
echo $parent;
}
}
When I use return $parent in else I get null. Any ideas ?

if you want to return the value you should return it on both places
public function check_parent($parent)
{
$q = $this->db->get_where('ci_categories', array('cat_id'=>$parent));
$r = $q->row();
if ($r->cat_child > 0)
{
return $this->check_parent($r->cat_child);
} else {
return $parent;
}

Related

CODEIGNITER function can echo but can't return

I have this code in my model file
function exist_kode($kode = 1)
{
$get = $this->db->query("SELECT * FROM perkiraan WHERE kode_perk='$kode'");
if($get->num_rows() == 0)
{
return $kode;
}
else
{
$this->exist_kode((int)$kode+1);
}
}
This function is not return some values when I use. But if I change return to echo, it will write the value I hope.
How can i solve this?
Return in your else statement as well:
return $this->exist_kode((int)$kode+1);

RETURN values in PHP

I have 2 functions in PHP and i want to return a value to one function to another. this is my functions
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
}
function payment_log_exists($icasl_no, $exam_session) {
$this->db->where('icasl_no', $icasl_no);
$this->db->where('exam_session', $exam_session);
$query = $this->db->get('exm_paymentlog');
if ($query->num_rows() > 0) {
$pl = $query->row();
$pay_id = $pl->paylog_id;
return $pay_id;
} else {
return false;
}
}
I want to return $pay_id to the save_payment_log_data() function but in here $pay_id didn't return to that function. I think it's return from the function payment_log_exists()
so how can I return $pay_id to the save_payment_log_data() function
See this example it's working fine:
function save_payment_log_data() {
$paylog_av = payment_log_exists();
# print the return value
echo $paylog_av;
}
function payment_log_exists() {
return "Hello";
}
save_payment_log_data();
You can try to remove public and this form save_payment_log_data(),
and call the save_payment_log_data() function where you want.
your are only assigning a value to the $paylog_av your "save_payment_log_data" function should be:
public function save_payment_log_data($icasl_number, $exam_session) {
$paylog_av = $this->payment_log_exists($icasl_number, $exam_session);
return $paylog_av;
}

Trying to get property of non-object using laravel

I have try to make my function but when id is empty the function return a empty array and got "Trying to get property of non-object" ... using laravel 5.2
public static function NameFunction($id) {
if(!empty($id)) {
$Table = Table::find($id);
if(count($Table) > 0)
{
return $Table;
}
} else {
$return = array('name' => 'NULL');
return $return;
}
}
How I use the function
{{ ClassName::NameFunction($idCat)->name }}
I know is not an object but how can I fix this ?
I don't know what's the idiomatic way to solve this in Laravel, but I suppose that if some item is not found something else should be done. So maybe you need to check something like:
public static function NameFunction($id) {
if(!empty($id)) {
$Table = Table::find($id);
if(count($Table) > 0)
{
return $Table;
}
}
return false;
}
$name = ClassName::NameFunction($idCat);
if ($name && !empty($name->name)) {
echo $name->name;
} else {
echo 'Not found'
}
Or maybe you can use findOrFail instead of find which will raise exception, which you can catch.
I think this one should work
public static function NameFunction($id) {
$default = array('name' => null);
if(!empty($id)) {
$Table = Table::find($id);
if(count($Table) > 0)
{
return $Table;
}
else
{
return $default;
}
} else {
return $default;
}
}

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 do I get the next item in a Laravel collection?

Let's say I have a child of a parent collection and I want to know who the next sibling is. My parent collection is ordered differently than internal id so I can't use the method described here:
Laravel previous and next records
Which would work except I'm sorting by name and time, not internal id. I'm hoping that there's a way to just get the parent collection, find this child's position within it, and then look forward or back within that collection to get next or previous.
Edit:
So, I made this, which works, but seems clunky. Is there a more efficient way to do this?
public function next()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $previous->id == $this->id))
{
// Yay! Our current record is the 'next' record.
return $media->id;
}
$previous = $media;
}
return null;
}
public function previous()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $media->id == $this->id))
{
// Yay! Our previous record is the 'previous' record.
return $previous;
}
$previous = $media->id;
}
return null;
}
You should never load the entire table to loop through it to find the next/previous item, instead do it this way:
$next = $this->album->media()->orderBy('id')->where('id', '>', $this->id)->first();
Here is the simple line of code
// next
function next($product_id)
{
$next = Product::where('id', '>', $product_id)->first();
return $next;
}
// previous
function previous($product_id)
{
$previous = Product::where('id', '<', $product_id)->first();
return $previous;
}
This did the trick:
public function next()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $previous->id == $this->id))
{
// Yay! Our current record is the 'next' record.
return $media->id;
}
$previous = $media;
}
return null;
}
public function previous()
{
$previous = null;
foreach ($this->album->media as $media)
{
if(!empty($previous && $media->id == $this->id))
{
// Yay! Our previous record is the 'previous' record.
return $previous;
}
$previous = $media->id;
}
return null;
}

Categories