Reputation: 11
I am a beginner and wrote a web page based on the tutorial JREAM (MVC).
I've encountered a problem in displaying articles.
How can I send a variable $from (index.php) from the controller to the model?
Of course, I can connect to the database in the index.php but whether that is good?
index.php
$limit = LIMIT; // article per page
$article = $this->Ile[0]['num']; // number of articles
if (isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$allpage = round($article/$limit);
$from = $limit * ($page - 1);
// $from send value to model and model return array with article
foreach($this->Article as $key => $value)
{
echo '<div style="float:left;width:660px;"><hr/><br/><p><span class="strong" style="color:#CC0000;">';
echo $value['title'];
echo '</span></p><br/><p>';
echo $value['content'];
echo '</p><div style="height:130px;">';
echo $value['photo'];
echo '</div></div>';
}
echo '<hr/>';
echo '<div style="width: 660px; position: relative; pointer-events: none; text-align: center; top: 14px;">'.$page.' z '.$allpage.'</div>';
if ($page == 1 && $page < $allpage)
{
echo '<a href="news?page='.($page+1).'" style="float: right;">STARSZE >></a>';
}
elseif ($page >= $allpage)
{
echo '<a href="news?page='.($page-1).'" style="float: left;"><< NOWSZE</a>';
} else {
echo '<a href="news?page='.($page-1).'" style="float: left;"><< NOWSZE</a>';
echo '<a href="news?page='.($page+1).'" style="float: right;">STARSZE >></a>';
}
?>
news.php
class News extends Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->view->title = 'News';
$this->view->Ile = $this->model->Ile();
$this->view->Article = $this->model->Article();
// I can put some value in ...->Article(0); and it works, but how connect in index.php
$this->view->render('header');
$this->view->render('news/index');
$this->view->render('footer');
}
}
news_model.php
<?php
class News_Model extends Model
{
public function __construct()
{
parent::__construct();
}
public function Ile()
{
return $this->db->select('SELECT COUNT(*) as num FROM `articles`');
}
public function Article($from)
{
return $this->db->select('SELECT * FROM `articles` ORDER BY `newsid` DESC LIMIT '.$from.', '.LIMIT.'');
}
}
Regards,
Thomas
Upvotes: 0
Views: 70
Reputation: 146
You should do this in your controller not in view :
Controller :
$limit = LIMIT; // article per page
$article = $this->view->Ile[0]['num']; // number of articles
if (isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$allpage = round($article/$limit);
$from = $limit * ($page - 1);
$this->view->Article = $this->model->Article($form); // This is how you can pass $form to model
And then you can assign This variable to your view like :
$this->view->page = $page
$this->view->allpage = $allpage
Now in view you can use $this->page instead of $page, same for $allpage
Hope it helps
Upvotes: 1