troubleshooting Question

Codeigniter - my code seems very repetitive

Avatar of edvinson
edvinsonFlag for United States of America asked on
PHP
3 Comments1 Solution305 ViewsLast Modified:
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?

	$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    |                |
+--------------+--------------+------+-----+---------+----------------+
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros