Welcome Guest, Not a member yet? Register   Sign In
[HELP] Database and foreach
#1

[eluser]Marcus Marden[/eluser]
Hello friends,

I started to use CI and I am still learning. I am trying to do video script via CI and I need help for the queries below:

/controllers/category.php
Code:
$data['categories'] = $this->db->order_by('name', 'ASC')->get('category')->result_array();
  foreach($data['categories'] as $category)
  {
   $data['videos'] = $this->db->select('seo, name, thumb, view, vote, score')->from('video')->where('category_id', $category['id'])->order_by('id', 'DESC')->limit(3)->get()->result_array();
// this code returns always category_id = 3 but I want to use it for all category ids.
  }
  $this->load->view('category_list_view', $data);

view/category_list_view.php
Code:
<?php foreach($categories as $category) { ?>
         <h2>&lt;?php echo anchor('category/show/' . $category['seo'], $category['name'], array('title' => $category['name'])); ?&gt;</h2>
         &lt;?php foreach($videos as $video) { ?&gt;
          <h1>&lt;?php echo anchor('video/show/' . $video['seo'], mb_substr($video['name'], 0, 25, "utf-8"), array('title' => $video['name'])); ?&gt;</h1>
         &lt;?php } } ?&gt;

I want to list all categories and 3 video files in categories.

My next question is, I am always using below code on view layer for list items.

Code:
foreach($query as $row) {
echo $row->id; // or if array $row['id']
}

Is it any solution to use it on controller layer and forward data to view layer?

Let me explain via codes.

Code:
$categories = $this->db->order_by('name', 'ASC')->get('category')->result_array();
  foreach($categories as $category)
  {
$data['title] = $category['title'];
}
$this->load->view('category_view', $data);

view layer

Code:
Category: &lt;?php echo $title; ?&gt; <br />

Returning ONLY "Category: Games" but should be:

Category: Games
Category: Popular
Category: Music
etc etc...

When I add foreach on view layer everything normal.

Hope that I explain my questions and thanks now for your helps and advice.
#2

[eluser]Marcus Marden[/eluser]
Anyone help me please Sad
#3

[eluser]ns8814[/eluser]
Hey I seen you had a similar question as per mine because you posted to it. Here goes an attempt to help. It seems as though you may have more experience in php then so I can only explain it so much, but figured its worth a shot the code snippets below work for a situation like you have. In the below scenario I used it to populate the values of a dropdown menu. this way i didnt have to use the foreach loop inside the view.


anyway


Code:
THE MODEL

function get_referralMain()
{
  $this->db->group_by('CatName');
  $query= $this->db->get('deck_referrallist');
  $options='';
  foreach($query->result() as $row)
  {
   $options .= '<option value="'.$row-&gt;CatID.$row-&gt;sourceTable.'">'.$row->CatName.'</option>'."\n";
  }
  return $options;
}
Here is the controller
Code:
The Controller

$data['Referrals']=$this->estimates_model->get_referralMain();
$this->load->view("your_view",$data);

This way when you get to the view there isnt much php code in there.

Code:
IN THE VIEW

<select size="1" name="ReferralMain" id="ReferralSource" >
  
  &lt;?php echo $Referrals;?&gt;
            
</select>
This way is much neater and if you change your database around all you have to is modify the model. Not sure if this is along the lines of what you were looking for or not.
#4

[eluser]Marcus Marden[/eluser]
Hey thank you so much ns8814, I can use your codes. I always used standalone PHP with my own classes, functions etc so learning new FW is taking time.

You added your strings in HTML format using $options but if you want to use only $row->CatID or $row->CatName this caused maybe problem on view layer. So maybe this can work.

Code:
// model
function get_referralMain()
{
  $this->db->group_by('CatName');
  $query= $this->db->get('deck_referrallist');
  $options='';
  foreach($query->result() as $row)
  {
   $options['catid'] = $row->CatID;
   $options['catname'] = $row->CatName;
  }
  return $options;
}

Code:
// view
<select size="1" name="ReferralMain" id="ReferralSource" >
  
  &lt;?php echo $Referrals['catid'] . ' ' . $Referrals['catname'];?&gt;
            
</select>
#5

[eluser]CroNiX[/eluser]
Or you could use the form_helper...

Model
Code:
function get_referralMain()
{
  $this->db->group_by('CatName');
  $query= $this->db->get('deck_referrallist');
  $options=array;
  foreach($query->result() as $row)
  {
   $options[$row->CatID] = $row->CatName;
  }
  return $options;
}

Controller:
Code:
$this->load->helper('form');
$data['options'] = $this->your_model->get_referralMain();
$this->load->view('your_view', $data);

View:
Code:
echo form_dropdown('ReferralMain', $options, set_value('ReferralMain'), 'id="ReferralSource" size="1"');
#6

[eluser]Marcus Marden[/eluser]
Cronix any idea for this?

Code:
$data['categories'] = $this->db->order_by('name', 'ASC')->get('category')->result_array();
  foreach($data['categories'] as $category)
  {
   $data['videos'] = $this->db->select('seo, name, thumb, view, vote, score')->from('video')->where('category_id', $category['id'])->order_by('id', 'DESC')->limit(3)->get()->result_array();
// this code returns always category_id = 3 but I want to use it for all category ids.
  }
  $this->load->view('category_list_view', $data);
#7

[eluser]Marcus Marden[/eluser]
I am still waiting help...




Theme © iAndrew 2016 - Forum software by © MyBB