Welcome Guest, Not a member yet? Register   Sign In
Getting ID from Array
#1

[eluser]Otemu[/eluser]
Hi,

Hope everyone well, hoping someone could help me with this.......

Basically I have a call that returning an multidimensional array from my database, which I use to display 4 featured stories, I then have more stories displayed in a list which shouldn't include what is already in the featured stories.

How can I extract only the articleid from this multidimensional array into a variable, so that I can then pass this variable into my next query?

Controller
Code:
$promoList4['promoList'] = $this->M_Modules->getPromoList4();

Model
Code:
function getPromoList4() {
        $data = array();
        $this->db->select('modules.moduleid, modules.articleid, modules.module_name, modules.display, articles.articleid, articles.heading, articles.intro_text, articles.page_url, articles.summary_text, articles.display, images.articleid, images.smallurl, images.largeurl, images.description');
        $this->db->from('modules','articles','images');
        $this->db->join('articles', 'modules.articleid = articles.articleid','left');
        $this->db->join('images', 'articles.articleid = images.articleid','left');
        $this->db->where('modules.display', 1);
        $this->db->where('articles.template', 'default_article');
        $this->db->limit(4);
        $Q = $this->db->get();
        $Q->num_rows();
        if ($Q-> num_rows() > 0){
            foreach ($Q-> result_array() as $row){
                $data[] = $row;
                }
            }
            
        $Q->free_result();
        return $data;
    }

All my attempts have failed so far :red:
I was going to create another db query just to rerun the ids but thought this wouldn't be good practice since I already have the data. Any tips or advice will be appreciated.

Thanks All
#2

[eluser]XMadMax[/eluser]
[quote author="Otemu" date="1313774369"]

list($promoList4['promoList'],$promoList4['promoIds']) = $this->M_Modules->getPromoList4();


.
.
.
getPomoList4
.
.
.
$ids = array();
if ($Q-> num_rows() > 0){
foreach ($Q-> result_array() as $row){
$data[] = $row;
$ids[] = $row->modules.articleid;
}
}

$Q->free_result();
return array($data,$ids);
}
[/quote]
#3

[eluser]Otemu[/eluser]
Thanks for your quick response,

Sorry if I have misunderstood your answer, but isn't your solution the same as declaring $promoListIds['promoListIds'] = $this->M_Modules->getPromoIds(); and running another db query to get the ids, how can i seperate the ids from $promoList4['promoList'] = $this->M_Modules->getPromoList4(); only without running another call.
#4

[eluser]CroNiX[/eluser]
No, he is looping through the query results and grabbing the IDs and sticking them in their own $ids array. So now there are 2 arrays, $data which contains all data (original result) and $ids which contain only the IDs. Both come from the original result set.
#5

[eluser]Otemu[/eluser]
Oh fantastic I understand guys. Thanks for your excellent solution XPerez and thanks for setting me straight CroNiX Smile




Theme © iAndrew 2016 - Forum software by © MyBB