Welcome Guest, Not a member yet? Register   Sign In
Issue with arrays? Any experts?
#1

[eluser]Devon Lambert[/eluser]
Ok Codeigniters here's the deal:

gTech helped me figure out some issues I was having with getting data regarding my categories for an app I am building BUT now I need to call on the items that relate to those categories and display it ALL in a view. Basically the layout should look like this:

Category 1
- Item 1
- Item 2
- Item 3
- Item 4
etc...

Category 2
- Item 1
- Item 2
- Item 3
- Item 4
etc...


SO far I have this:

MODEL:

Code:
// Original function for pulling the categories I need from the DB (Thanks gTech!)
function get_categories() {  
    $query = "SELECT name, type FROM categories ORDER BY categories.order ASC";
    $result = $this->db->query($query);
    return $result->result_array();
}

// Function that will give me 5 of the items belonging to the category that was passed via the //cat_data array.

function get_catitems() {
    $query = "SELECT id, items.desc, name, nameid, rating  FROM items WHERE cat = '".$categories['name']."' ORDER BY RAND() LIMIT 0, 5";
    $result = $this->db->query($query);
    return $result->result_array();
}

CONTROLLER:

Code:
// Create the cat_data array
$cat_data = array();
$cat_data['categories'] = $this->mdl_home->get_categories();
$cat_data['cat_items'] = $this->mdl_home->get_catitems($cat_data);

// Load the view
$this->load->view('home_bottom', $cat_data);

VIEW:

Code:
<?php foreach($categories as $category):?>
  <li>&lt;?php echo $category['name'];?&gt;</li>
      <ul>
      &lt;?php foreach($cat_items as $cat_item):?&gt;
      <li>&lt;?php echo $cat_item['name']; ?&gt;</li>    
      &lt;?php endforeach;?&gt;
      </ul>
&lt;?php endforeach;?&gt;

Any help? :-)
#2

[eluser]Devon Lambert[/eluser]
Ok,

Narrowed this one down and figured out that it purely my lack of knowledge with arrays that is hurting me here. I'm close but still don't quite have a solution, any help is greatly appreciated.

Here is what I have so far:

MODEL:

Code:
function get_categories() {  
    $query = "SELECT name, type FROM categories ORDER BY categories.order ASC";
    $result = $this->db->query($query);
    return $result->result_array();
}
    
function get_catgames($cat_data) {
    foreach ($cat_data['categories'] as $category):
    $query = "SELECT id, items.desc, name, nameid, rating  FROM items WHERE cat = '".$category['name']."' ORDER BY RAND() LIMIT 0, 5";            
        $result = $this->db->query($query);
    return $result->result_array();
    endforeach;
}

CONTROLLER:

Code:
// Create the cat_data array
    $cat_data = array();
        
    $cat_data['categories'] = $this->mdl_home->get_categories();
    $cat_data['items'] = $this->mdl_home->get_catitems($cat_data);
        
    //Printing out my array for testing purposes
    print_r($cat_data);
        
    $this->load->view('home_bottom', $cat_data);

VIEW:

Code:
&lt;?php foreach($categories as $category):?&gt;
  <li>&lt;?php echo "<br />".$category['name'];?&gt;</li>
      <ul>
      &lt;?php foreach($items as $item):?&gt;
      <li>&lt;?php echo $item['name']; ?&gt;</li>    
      &lt;?php endforeach;?&gt;
      </ul>
&lt;?php endforeach;?&gt;

This code is currently outputting the same "items" for every category that I print out. Such as:

#


Category 1

* BLUE Item 1
* BLUE Item 2
* BLUE Item 3
* BLUE Item 4
* BLUE Item 5

Different category but same items?

Category 2

* BLUE Item 1
* BLUE Item 2
* BLUE Item 3
* BLUE Item 4
* BLUE Item 5

etc...
#3

[eluser]oddman[/eluser]
The reason for this dnyce, is because you have not created any relationship in the code between items and categories.

In order to do this, you need to update your get_catgames() function, as the logic is incorrect (its returning after one call, even though you're looping.

Try this:

Code:
function get_catgames($cat_data) {
    $cat_items = array();
    foreach ($cat_data['categories'] as $category) {
        $items = array();
        $query = 'SELECT id, items.desc, name, nameid, rating, cat FROM items WHERE cat = \''.$category['name'].'\' ORDER BY RAND() LIMIT 0, 5';
        $result = $this->db->query($query);
        
        foreach ($query->result_array() as $row)
        {
            $items[] = $row;
        }
        
        $cat_items[$row['cat']] = $items;
    }
}

What we've done here is created a relationship between the categories and items, and stored each result in it's own array.

Code:
&lt;?php foreach($categories as $category):?&gt;
<li>&lt;?php echo "<br />".$category['name'];?&gt;</li>
<ul>
    &lt;?php foreach($items[$category['name']] as $item):?&gt;
      <li>&lt;?php echo $item['name']; ?&gt;</li>    
      &lt;?php endforeach;?&gt;
</ul>
&lt;?php endforeach;?&gt;

This is the code you want in your view. As you can see, we're referring to each $items array that contains the $category['name'] value. Also, just a bit of friendly advice - you're better off using IDs to link your data between tables, rather than strings (the category name), reason being is IDs are far easier for keys, and they're much more efficient for searching.

Hope that helps!
#4

[eluser]Devon Lambert[/eluser]
Thanks Oddman.

The CI community is always great for advice even general php related.

I'll try this when I get back from eating.
#5

[eluser]oddman[/eluser]
Oh, one thing I forgot. Don't forget to return $cat_items in the function Tongue

PS: I used to love developing browser-based games. I still do them when I get the urge Smile
#6

[eluser]gtech[/eluser]
hello I like the design of the website btw..

anyway your code overwrites its self as you return inside the loop. infact you will only ever get one itteration through the loop in the get_catgames function, so heres a fix:

Code:
function get_catgames($cat_data) {
    $itemsarr = array();
    foreach ($cat_data['categories'] as $category) {
        $query = "SELECT id, items.desc, name, nameid, rating  FROM items WHERE cat = '".$category['name']."'  ORDER BY RAND() LIMIT 0, 5";            
        $result = $this->db->query($query);
        $itemsarr[$category['name']] = $result->result_array();
    }
    return $itemsarr;
}

then your view
Code:
&lt;?php foreach($categories as $category):?&gt;
  <li>&lt;?php echo "<br />".$category['name'];?&gt;</li>
      <ul>
      &lt;?php foreach($items[$category['name']] as $item):?&gt;
      <li>&lt;?php echo $item['name']; ?&gt;</li>    
      &lt;?php endforeach;?&gt;
      </ul>
&lt;?php endforeach;?&gt;

I havn't tested it, but you get the idea hopefully.. in the view the items loop key changes for each iteration of the categories loop.

also I am hoping that the category name is unique? if not you should be using an ID.. there are cleaner methods of achieving what you want but lets do one step at a time.
#7

[eluser]gtech[/eluser]
dang! beaten to it, but there you go two similar solutions Smile
#8

[eluser]Devon Lambert[/eluser]
haha Thanks gtech.

No worries. I can use ALL the help I can get. I get the idea.

I'm glad that you guys both like games and the design of my site. I am actually in the process of converting to our beloved CI backend and a brand spanking new design for the front end.

Hopefully I can make another splash in the browser based games world with the help of CI. Of course, there's DOOF (using CI) not to mention the other ruby on rails counterparts like Kongregate and I believe Gamegum.
#9

[eluser]gtech[/eluser]
@oddman

just to point out a minor thing:
Code:
foreach ($query->result_array() as $row)
        {
            $items[] = $row;
        }

is the same as

Code:
$items = $query->result_array();
#10

[eluser]oddman[/eluser]
Cheers gtech Smile




Theme © iAndrew 2016 - Forum software by © MyBB