Welcome Guest, Not a member yet? Register   Sign In
DB related code - make it simple and fast
#1

[eluser]Yash[/eluser]
Code:
//GetCats
$query = $this->db->get('categories');
             $i=0;
            foreach ($query->result() as $row)
            {
                $categories[$i]= array(
                                "CatID"=>$row->CatID,
                                "Cat"=>$row->Cat,
                                "RSS"=>$row->RSS,
                                "CatPrivate"=>$row->CatPrivate,
                                "ParentID"=>$row->ParentID
                                );    
                                
                $i++;

retrun $categories;

Code:
$cats=self::GetCats();

$categories=$posts['CatID']; //contain only numeric catids like 2,3,5 or 3 only
        $singlecategories=explode(",",$categories);
        //echo print_r($singlecategories);
        $mycat="";
        for($i=0;$i<=count($cats)-1;$i++)
        {
            for($j=0;$j<=count($singlecategories)-1;$j++)
            {
                if($singlecategories[$j]==$cats[$i]['CatID'])
                {
                    $mycat.=$cats[$i]['Cat'].",";
                }
            }
        }
        echo $mycat;

Please make it fast or simple code Smile
#2

[eluser]parrots[/eluser]
Hopefully this will simplify things:

Code:
$query = $this->db->get('categories');
$categories = $query->result_array();
return $categories;

Code:
$cats=self::GetCats();

$categories=$posts['CatID']; //contain only numeric catids like 2,3,5 or 3 only
$singlecategories=explode(",",$categories);
$mycat="";
foreach ($cats as $category) {
    if (in_array($category['CatID'], $singlecategories)) {
        $mycat.=$category['Cat'].",";
    }
}
echo $mycat;

In getCats result_array() returns an array of rows where each row is an array instead of an object ($row['column'] vs $row->column). In your other code you don't need to use that second nested for loop. You can make use of the in_array to see if one object is in another array. You can also use foreach to loop through arrays without having to keep track of the index. That's a personal preference, either way will work, I just find it easier to read and it saves you a counter variable.
#3

[eluser]Yash[/eluser]
that was awesome!!

dude that is really good job.

Thank you so much
#4

[eluser]xwero[/eluser]
Wouldn't it be simpler to do
Code:
$cats = '';
$temp = array();
$query = $this->db->select('Cat')->from('categories')->where_in($posts['CatID']);
foreach($query->result() as $row)
{
   $temp[] = $row->Cat;
}

$cats = implode(',',$temp);
If you only need one column why should you query the database for more?
#5

[eluser]Yash[/eluser]
Strange Error ...

Fatal error: Call to a member function result() on a non-object in near

problem is in this section

foreach($query->result() as $row)
{
$temp[] = $row->Cat;
}
#6

[eluser]Armchair Samurai[/eluser]
xwero's code is missing the actual query:
Code:
$this->db->select('Cat')->from('categories')->where_in($posts['CatID']);
$query = $this->db->get();
#7

[eluser]Yash[/eluser]
This time works perfectly...

and this code saves a lot time and much better approach.

Thank you xwero and Armchair Samurai




Theme © iAndrew 2016 - Forum software by © MyBB