Reputation: 193
what I'm trying to do seems very easy but for the life of me, I can't figure out what I'm doing wrong. What I want to achieve is to call a function which contains a loop with a return statement in another function in my model. For some reason beyond me, the loop function only returns the first row in the db table, even when there are more than 1.
Here is the loop function:
public function download_contents($id) {
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
$count = 1;
foreach ($the_contents->result() as $y) {
$file_name = $y->file_name;
$download_path = base_url('assets/client/contents/'.$file_name);
return '<p>' .$count++. '. ' .$file_name. '
<a class="btn btn-primary btn-xs" href="' .$download_path. '" title="Download Content">Download</a></p>';
}
}
Now I want to echo the results of the function above in the function below:
public function modal_content_download($id) {
$y = $this->db->get_where('website_requests', array('id' => $id))->row();
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
if ( $the_contents->num_rows() > 0 ) {
return '<button class="btn btn-primary btn-sm modal-toggle-btn" data-toggle="modal" data-target="#contents' .$id. '" title="Download Contents"> <i class="fa fa-download"></i></button>
<div class="modal fade" id="contents' .$id. '" role="dialog">
<div class="modal-dialog">
<div class="modal-content modal-width">
<div class="modal-header">
<div class="pull-right">
<button class="btn btn-danger btn-xs" data-dismiss="modal" title="Close dialog"> Close </button>
</div>
<h5 class="modal-title"> <i class="fa fa-download"></i> Download Contents: ' .$y->project_name. '</h5>
</div><!--/.modal-header-->
<div class="modal-body">'
.$this->download_contents($id). //calling the first function
'</div>
</div>
</div>
</div>';
} else {
return '<b style="color: red">No content uploaded yet.</b>';
}
}
The second function is passed to the controller and loaded to the view (with some other info). Now I know about code igniter's query logic and result() should return more than 1 row. What am I missing?
Upvotes: 0
Views: 434
Reputation: 398
Try this
public function download_contents($id) {
$the_contents = $this->db->get_where('contents', array('request_id' => $id));
$count = 1;
$loop_result="";
foreach ($the_contents->result() as $y) {
$file_name = $y->file_name;
$download_path = base_url('assets/client/contents/'.$file_name);
$loop_result .= '<p>' .$count++. '. ' .$file_name. '
<a class="btn btn-primary btn-xs" href="' .$download_path. '" title="Download Content">Download</a></p>';
}
return $loop_result;
}
Upvotes: 1