edvinson
asked on
Codeigniter - my code seems very repetitive
In my site, I have FIVE columns. Each column is represented by column: tab_id
Each column can have N number of links.
In my controller, I want to get all the links and send it to my view.
Here is the way I currently have it coded, and it SEEMS TOTALLY REDUNDANT!
Is there a more efficient way?
Each column can have N number of links.
In my controller, I want to get all the links and send it to my view.
Here is the way I currently have it coded, and it SEEMS TOTALLY REDUNDANT!
Is there a more efficient way?
$query = $this->db->query("select * from links where tab_id='1' ");
$data['link1'] = "";
foreach ($query->result_array() as $row)
{
$data['link1'] = $data['link1'] . "<li><a href='".$row['link_url']."'>".$row['link_title']."</a></li>";
}
$query = $this->db->query("select * from links where tab_id='1' ");
$data['link2'] = "";
foreach ($query->result_array() as $row)
{
$data['link2'] = $data['link2'] . "<li><a href='".$row['link_url']."'>".$row['link_title']."</a></li>";
}
$query = $this->db->query("select * from links where tab_id='2' ");
$data['link3'] = "";
foreach ($query->result_array() as $row)
{
$data['link3'] = $data['link3'] . "<li><a href='".$row['link_url']."'>".$row['link_title']."</a></li>";
}
$query = $this->db->query("select * from links where tab_id='3' ");
$data['link4'] = "";
foreach ($query->result_array() as $row)
{
$data['link4'] = $data['link4'] . "<li><a href='".$row['link_url']."'>".$row['link_title']."</a></li>";
}
$query = $this->db->query("select * from links where tab_id='4' ");
$data['link5'] = "";
foreach ($query->result_array() as $row)
{
$data['link5'] = $data['link5'] . "<li><a href='".$row['link_url']."'>".$row['link_title']."</a></li>";
}
$this->load->view('homepage',$data);
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_date | datetime | YES | | NULL | |
| link_title | varchar(255) | YES | | NULL | |
| link_url | varchar(255) | YES | | NULL | |
| tab_id | int(255) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you so much. Not only did I get my answer from you two, but I learned a little more about CI!! Thanks again!