问题
I have blog which is related to a cashback in codeigniter franework. and i have database of offers. i want to fetch all offers from database to index.php(view). this is my default.
The problem is how to fetch data in index.php. index.php is my main page of my website.
what i have tried
user.php (controller)
public function testing()
{
$this->load->view('user/index.php');
$this->load->model('showoffers');
$offers = $this->showoffers->showoffersmodel();
$this->load->view('showoffers',['offers'=>$offers]);
}
showoffers.php(model)
<?php
class showoffers extends CI_Model
{
public function showoffersmodel()
{
$q = $this->db->select('*')
->from('offers')
->get();
return $q->result();
}
}
?>
index.php (view)
<?php if(count($offers)): ?>
<?php foreach ($offers as $emp): ?>
<div class="text-center">
<br>
<img style="margin-left: -80%;" border="0" height="100" width="120" src="<?php echo base_url('image/amazon.png'); ?>" class="rounded" alt="...">
<h2><?= $emp->title; ?></h2>
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6 details">
<?= $emp->details; ?>
<br>
<br>
<a style="font-size: 15px;" href="<?= $emp->link; ?>" class="badge badge-warning">Click Here to collect Cashback</a>
</div>
</div>
<button type="button" class="btn btn-success btn78">
<i class="fa fa-check" aria-hidden="true"></i>
Verified
</button>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif; ?>
the error i have got
Unable to load the requested file: showoffers.php
please help!!!
回答1:
Controller:
public function testing()
{
$data['offers'] = $this->showoffers->showoffersmodel();
$this->load->view('user/index', $data);
}
Model:
public function showoffersmodel()
{
$query = $this->db->get('offers');
return $query->result();
}
View:
<?php if(count($offers)): ?>
<?php foreach ($offers as $emp): ?>
<div class="text-center">
<br>
<img style="margin-left: -80%;" border="0" height="100" width="120" src="<?php echo base_url('image/amazon.png'); ?>" class="rounded" alt="...">
<h2><?= $emp->title; ?></h2>
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6 details">
<?= $emp->details; ?>
<br>
<br>
<a style="font-size: 15px;" href="<?= $emp->link; ?>" class="badge badge-warning">Click Here to collect Cashback</a>
</div>
</div>
<button type="button" class="btn btn-success btn78">
<i class="fa fa-check" aria-hidden="true"></i>
Verified
</button>
</div>
<?php endforeach; ?>
<?php endif; ?>
Hope this helps!
来源:https://stackoverflow.com/questions/62729781/fetch-data-from-database-in-index-php-in-codeigniter