Welcome Guest, Not a member yet? Register   Sign In
problem in listing category and subcategory in the same page
#1

[eluser]Bigil Michael[/eluser]
model
Quote:function select_all_classifieds()
{
$this->db->select('id,heading');
$this->db->order_by('heading');
$this->db->where('parent','0');
$result_product = $this->db->get('classifieds_categories');
return $result_product->result_array();
}

function list_all_classifieds($cid)
{
$this->db->where('parent',$cid);
$result_product = $this->db->get('classifieds_categories');
return $result_product->result_array();
}
function count_classifieds($parent=0)
{
$count=0;
$result_total = $this->db->query("SELECT COUNT(id) AS total FROM classifieds WHERE categoryid='$parent' AND status=1");
if($result_total->num_rows()>0){
$row = $result_total->row();
$count = $row->total;
}
return $count;
}

controller
Quote:$build_array = array();
$fleets = $this->Classifieds_model->select_all_classifieds();

foreach($fleets as $row){
$build_array[] = array (
'fleets_array' => $row,
'listefleets_array' => $this->Classifieds_model->list_all_classifieds($row['id']),
'sub_listings_count' => $this->Classifieds_model->count_classifieds($row['id'])
);
}
$this->data['meow'] = $build_array;
view
Quote:<ul class="main">
&lt;?php foreach($meow as $crow){?&gt;
<li>
<span class="title"><a href="&lt;?php echo site_url();?&gt;/classifieds/index/&lt;?php echo $crow['fleets_array']['id'];?&gt;">&lt;?php echo $crow['fleets_array']['heading'];?&gt;</a>(&lt;?php echo $crow['sub_listings_count'];?&gtWink</span>

&lt;?php
foreach ($crow['listefleets_array'] as $lrow)
{
?&gt;
<ul class="sub">
<li><a href="&lt;?php echo site_url();?&gt;/classifieds/index/&lt;?php echo $lrow['id'];?&gt;">&lt;?php echo $lrow['heading'];?&gt;</a>
</li>

</ul>
&lt;?php } ?&gt;
</li>
&lt;?php } ?&gt;

</ul>

now it working well.

now i want to print like this
category
subcategory
count(subcategory present in another table)

now i reached up to this level
category
subcategory


can any one help me???
thanks in advance.....
#2

[eluser]InsiteFX[/eluser]
143 Posts and you still do not know how to use code tags?

InsiteFX
#3

[eluser]Bigil Michael[/eluser]
Code:
$build_array = array();
      $fleets = $this->Classifieds_model->select_all_classifieds();
    
      foreach($fleets as $row){
        $build_array[] = array (
          ‘fleets_array’ => $row,
          ‘listefleets_array’ => $this->Classifieds_model->list_all_classifieds($row[‘id’]),
          ‘sub_listings_count’ => $this->Classifieds_model->count_classifieds($row[‘id’])
        );
      }
      $this->data[‘meow’] = $build_array;
#4

[eluser]Nick_MyShuitings[/eluser]
[quote author="InsiteFX" date="1304783693"]143 Posts and you still do not know how to use code tags?

InsiteFX[/quote]

I think I have a forum mancrush on InsiteFX... he reminds me of Mr Oh Hai.
#5

[eluser]Nick_MyShuitings[/eluser]
But in all seriousness, you're saying that the model and build_array have all the info that you want, but that you aren't able to figure out the view file? or is the model and build_array still not including all the data that you need?
#6

[eluser]Bigil Michael[/eluser]
Code:
$build_array = array();
      $fleets = $this->Classifieds_model->select_all_classifieds();
    
      foreach($fleets as $row){
        $build_array[] = array (
          ‘fleets_array’ => $row,
          ‘listefleets_array’ =>           $this->Classifieds_model->list_all_classifieds($row[‘id’]),
          
        );
      }
      $this->data[‘meow’] = $build_array;

this is the controller me used to print
category
subcategory

now i want

category
subcategory
sub-subcategory

for this i have to write again a for loop inside the build array
can anyone help me???
thanks in advance.......
#7

[eluser]Bigil Michael[/eluser]
can anyone help me?????????
urgent
#8

[eluser]Nick_MyShuitings[/eluser]
Its prolly a bit out of your php level, but what you need here is a fancy recursive function to work its way down N-levels as needed and give you a fancy little array. Then a similar magic function to print the sucker out. Otherwise: yes... you need to just add another foreach.

If you're serious about it... then check out these two functions of mine. These suckers work with the whole id, parent_id paradigm, where a table makes reference to previous rows to provide for N level deep nesting:

Code:
/**
   * Get Head
   *
   * @return array
   * Use: takes a parent_id as an input and outputs that item
   */
  public function _get_head($id, $table = 0) {
    $table = ($table === 0)?$this->_table:$table;
    $this->db->where($table.'_id', $id);
    if ($this->condition_field) {
      $this->db->where($this->condition_field, '0');
    }
    $this->db->order_by($this->order_field, "asc");
    $query = $this->db->get($table);
    return $query->result_array();
  }
  /**
   * Get Items
   *
   * @return array
   * Use: takes a parent_id as an input and outputs all the children
   */
  public function & _get_items($id = 0, $table = 0) {
    $table = ($table === 0)?$this->_table:$table;
    $itemOverview =& $this->_get_head($id, $table);
    foreach ($itemOverview as $key => $val) {
      $item = $itemOverview[$key];
      if ($item['id'] != 0) {
        $childItems =& $this->_get_items($item['id'], $table);
      }
      if (!empty($childItems)) {
        $item['children'] =& $childItems;
      }
      $items[] = $item;
    }
    return $items;
  }

and I am way to tired to try and explain how that works right now... either read up on how recursive functions work then study that code, or just slap another foreach into your code and you can go another level deeper.

PS... urgent is your problem, nobody here on the forum feels your urgency... and the fact that you posted again an entire day and a half later without any "hey I tried all of this and still no luck" is even more of a telltale...
#9

[eluser]Bigil Michael[/eluser]
hey I tried all of this and still no luck
#10

[eluser]Bigil Michael[/eluser]
i have changed my code
Code:
$build_array = array();
        $fleets = $this->Classifieds_model->select_all_classifieds();
        
        foreach($fleets as $row){
            $build_array[] = array (
                'fleets_array' => $row,
                'listefleets_array' => $this->Classifieds_model->list_all_classifieds($row['id']),
                //'sub_listings_count' => $this->Classifieds_model->count_classifieds($row['id'])
            );
        }
        $meow = $build_array;
        $this->data['meow'] = $meow;
        foreach($meow as $crow)
        {
            foreach ($crow['listefleets_array'] as $lrow)
            {
                echo '&nbsp;' .$lrow['id'].' &nbsp; '. $lrow['heading'].' &nbsp; ';
                $data['sub_listings_count'] = $this->Classifieds_model->count_classifieds($lrow['id']);
                print_r($data['sub_listings_count']).' <br/>';
            }
        }

now i use echo the values in controller
it is working well. but like this it is not possible to print the values in view page

i tried a lot to write nested foreach
every time i failed..
can any one solve my problem..




Theme © iAndrew 2016 - Forum software by © MyBB