Help with models

edited June 2012 in News & Announcements
Hi,

I have a site where I need to create listings of businesses.

I have 4 tables:
categories
sub_categories
categories_to_members
members

Some of the categories will have sub categories and some not.
I have a side menu built from the categories and sub categories tables.

So my menu looks like this:
Accommodation
B&B
Guesthouse
Self-Catring

Outdoors

Health & Beauty

When I click on Guesthouse, it goes to: mysite/members/accommodation/guesthouse.

In the below code I marked the lines that I changed or added with ***

My members.php file looks like this:
$category = $CI->uri->segment(2); $subCategory = $CI->uri->segment(3);*** $CI->load->model('images_to_members_model'); if(!empty($category)): $CI->load->model('categories_model'); $CI->load->model('sub_categories_model');*** // remember, all categories that have a published value of 'no' will automatically be excluded $category = $CI->categories_model->find_one_by_name($category); $members = $category->members($subCategory);*** else: $CI->load->model('members_model'); // remember, all members that have a published value of 'no' will automatically be excluded $members = $CI->members_model->find_all(); endif; ?> <h1><?php if (!empty($category)) : ?> <?=$category->name?> <?php endif; ?></h1> <?php if(!empty($subCategory)) echo '<h2>' . $subCategory . '</h2>'; ?> <div style="margin-top: 10px;"> <?php foreach($members as $member): ?> <div class="member"> <?php $memberId = $member->id; ?> <img src="<?=img_path() . 'members/' . $member->logo?>" /> <h2 class="member"><?=fuel_edit($member->id, 'Edit: '.$member->title, 'members')?><?=$member->title?></h2> <p>Address: <?=$member->address?></p> <p>Phone: <?=$member->phone?></p> <p>Web site: <a href="http://<?=$member->site?>" target="_blank"><?=$member->site?></a></p> <p><?=$member->content?></p> <p> <ul class="gallery"> <?php $CI->load->model('member_images_model'); $images = $CI->member_images_model->find_all(array('member_id'=>$memberId), 'id asc'); foreach($images as $img): $image = img_path() . 'members/' .$img->image; echo '<li> <a href="' . $image .'" rel="' . $memberId . '"><img src="' . $image . '" height="100" /></a> </li>'; endforeach; ?> </ul> <div class="clear"></div> </p> </div><!-- End member--> <?php endforeach; ?>

My cateogries model:
class Categories_model extends Base_module_model { public $record_class = 'Category'; public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('categories'); } } class Category_model extends Base_module_record { function get_members($subCategory) { $this->_CI->load->model('categories_to_members_model'); $where = array('category_id' => $this->id, 'sub_cat_id' => $subCategory, 'members.published' => 'yes');*** $members = $this->_CI->categories_to_members_model->find_all($where); return $members; } }

Categories_to_members model:
class Categories_to_members_model extends MY_Model { public $record_class = 'Category_to_member'; //public $foreign_keys = array('category_id' => 'categories_model', 'member_id' => 'members_model'); public $foreign_keys = array('category_id' => 'categories_model', 'sub_cat_id' => 'sub_categories_model', 'member_id' => 'members_model'); public $parsed_fields = array('content', 'content_formatted'); function __construct() { parent::__construct('categories_to_members'); } function _common_query() { $this->db->select('categories_to_members.*, members.title, members.id, members.content, members.address, members.phone, members.site, members.logo, categories.name AS category_name, categories.published'); $this->db->join('members', 'categories_to_members.member_id = members.id', 'left'); $this->db->join('categories', 'categories_to_members.category_id = categories.id', 'left'); $this->db->join('sub_categories', 'categories_to_members.sub_cat_id = sub_categories.id', 'left');*** } } class Category_to_member_model extends Data_record { public $category_name = ''; public $title = ''; function get_excerpt_formatted($char_limit = NULL, $readmore = '') { $this->_CI->load->helper('typography'); $this->_CI->load->helper('text'); $excerpt = $this->content; if (!empty($char_limit)) { // must strip tags to get accruate character count $excerpt = strip_tags($excerpt); $excerpt = character_limiter($excerpt, $char_limit); } $excerpt = auto_typography($excerpt); $excerpt = $this->_parse($excerpt); if (!empty($readmore)) { $excerpt .= ' '.anchor($this->get_url(), $readmore, 'class="readmore"'); } return $excerpt; } }

When I go to mysite/members/accommodation/guesthouse I get this error:

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: fuel/Loader.php(298) : eval()'d code

Line Number: 26

Not sure how to deal with that issue.

How will I make sure that if there is a sub cateogry, it gives me all the members that belong to this sub category and if there isn't one, it will give me all the members that belong to the main category, and why do I get the php error?
Thank you

Comments

  • edited 8:12AM
    Try using the following in your members.php file:
    // use get_members instead of just members for the method name $members = $category->get_members($subCategory);
    You have to use the method name in this case because you are passing a parameter.
  • edited 8:12AM
    Thank you, that helped
Sign In or Register to comment.