Salam. Aku mahu senaraikan subcategory bagi setiap kategori yang ada. Setelah agak lama mencuba, aku menemui jalan buntu kerana kekurangan ilmu. ada siapa boleh bantu? 
Target: Kenapa tak keluar gambar? aku letak dalam attachment je la 
Pencapaian: List kategori sahaja. List subcategori masih buntu.
Problem:
Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\ci\system\application\controllers\site.php on line 30
site_model.php (model)
<?php
/**
* @author
* @copyright 2010
*/
class Site_model extends Model
{
function get_category()
{
$query = $this->db->get('category');
return $query->result();
}
function get_subcategory2($idcat)
{
$query = $this->db->get_where('subcategory', array('idCategory'=>$idcat));
return $query->result();
}
}
?>site.php (controller)
<?php
/**
* @author
* @copyright 2010
*/
class Site extends Controller{
function userarea()
{
$this->load->model('site_model');
$data['main_content'] = 'userarea';
$data['files'] = $this->site_model->get_category(); //untuk senarai kategori di userarea.php (view)
$cat = $this->site_model->get_category();
foreach($cat as $catrow)
{
$data['sub'] = $this->site_model->get_subcategory2($catrow['idCategory']);
}
$this->load->view('includes/template', $data);
}
}
?>userarea.php (view)
<div class="center_left">
<h1>Browse</h1>
<?php foreach($files as $file): ?> // senaraikan kategori
<div class="browse_box">
<div class="title_welcome"><?php echo $file->CategoryName; ?></div>
</div>
<br/>
<?php endforeach; ?>
</div>
cuba gantikan
$catrow['idCategory']
dengan ini
$catrow->idCategory
thanks error dah tiada tp masih belum berjaya paparkan subcategory. Ni code userarea.php (view) yang dah diubah.
<div class="center_left">
<h1>Browse</h1>
<?php foreach($files as $file): ?>
<div class="browse_box">
<div class="title_welcome"><?php echo $file->CategoryName; ?></div>
<div><?php echo $file->CategoryDescription; ?></div>
<?php foreach($sub as $subs): ?>
<div><?php echo $subs->SubCatName; ?></div>
<?php endforeach; ?>
</div>
<br/>
<?php endforeach; ?>
</div>
tukar
$data['sub']
kepada ini
$data['sub'][]
aku recommend bina multidimensional array untuk categories nih.
ni code untuk ko implement kat controller. mungkin ada kesilapan, aku tak berapa pasti.
foreach ($categories as $category)
{
$category = &$data['categories'][$category->CategoryName];
// boleh ganti dengan apa-apa data berkaitan category yang dikehendaki
$category['id'] = $category->idCategory;
$category['subcategories'] = $this->site_model->get_subcategory2($category->idCategory);
}kat view
foreach ($categories as $key => $value)
{
echo $key; // ini sepatutnya akan print nama category
echo $value['id']; // id category
// senaraikan semua subcategories
foreach ($value['subcategories'] as $sub)
{
echo $sub->SubCatName;
}
}apa yang ko try buat ni adalah organizing hierarchical data. setakat dua level macam ko try buat sekarang nih, tak ada masalah sangat, tapi kalau ko nak buat ade 3-tier atau seterusnye (subcategory kepada subcategory) akan timbul masalah dan kod akan jadi lebih komplex.
aku recommend bace lebih lanjut tentang teknik2 lain untuk membina data tree.
tq karass untuk link tu
sama2
thanks karass. ni aku sharekan link yg aku rasakan berguna untuk solvekan problem diatas:
http://sqllessons.com/categories.html
http://codeigniter.com/forums/viewthread/135575/#669253
I think your code is ok, its just that the sub-category is replacing each other.
try this. I have not tested it so it may have some syntax errors.
I've also modified your code to include empty array checking because if your database is empty it will break your code.
controller
<?php
/**
* @author
* @copyright 2010
*/
class Site extends Controller{
function userarea()
{
$this->load->model('site_model');
$data['main_content'] = 'userarea';
$data['files'] = $this->site_model->get_category(); //untuk senarai kategori di userarea.php (view)
if(!empty($data['files'])){
$cat = $data['files'];
foreach($cat as $catrow)
{
$subCat = $this->site_model->get_subcategory2($catrow->idCategory);
if(!empty($subCat)){
$data['sub'][$catrow->idCategory] = $subCat;
}
}
}
$this->load->view('includes/template', $data);
}
}
?>View
<div class="center_left">
<h1>Browse</h1>
<?php
if(isset($files)){
foreach($files as $file):
?>
<div class="browse_box">
<div class="title_welcome"><?php echo $file->CategoryName; ?></div>
<div><?php echo $file->CategoryDescription; ?></div>
<?php
if(isset($sub) && isset($sub[$file->idCategory])){
$catSub = $sub[$file->idCategory];
foreach($catSub as $catSubs):
?>
<div><?php echo $catSubs->SubCatName; ?></div>
<?php endforeach;
}
?>
</div>
<br/>
<?php endforeach;
}
?>
</div>
cuba gantikan
$catrow['idCategory']dengan ini
$catrow->idCategory