0

i'm watching a tutorials about CMS with OOP - PHP & RainTPL

on control page : (articles.php)

<?php
require_once('globals.php');
require_once(CONTROLLERS.'ArticlesController.php');

$articlesmodel = new ArticlesModel() ; 

$catsmodel     = new ArticlesCatsModel();

$controller    = new ArticlesController($articlesmodel,$catsmodel);

$controller->show();

?>

globals.php:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

define('ROOT',dirname(__FILE__));
define('INC',ROOT.'/includes/');
define('CORE',INC.'/core/');
define('MODELS',INC.'/models/');
define('CONTROLLERS',INC.'/controllers/');
define('LIBS',INC.'/libs/');

/*
core files
*/
require_once(CORE.'config.php');
require_once(CORE.'mysql.class.php');
require_once(CORE.'raintpl.class.php');
require_once(CORE.'system.php');

System::Store('db',new mysql());
System::Store('tpl',new RainTPL()); //class RainTPL
?>

ArticlesController.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


require_once(MODELS.'ArticlesModel.php');
require_once(MODELS.'ArticlesCatsModel.php');


class ArticlesController
{

    private $articlesModel; //Articles Model Object
    private $articlesCatsModel; //Articles Cat Model Object
                               //object of ArticlesModel class
    public function __construct(ArticlesModel $articlesmodel,ArticlesCatsModel $catsmodel)
    {

        $this->articlesModel     = $articlesmodel ;
        $this->articlesCatsModel = $catsmodel ; 
    }


    public function Show()
    {
        // array of articles from model :D 
        /*
           he takes object from Articlesmodel.php
           like private articles model 
           then he  call the Get function 
           from  ArticlesModel Class 
        */

        $articles = $this->articlesModel->Get();
        $cats     = $this->articlesCatsModel->Get();


        //put them inside the template after getting them
        System::Get('tpl')->assign('articles',$articles)    ;   
        System::Get('tpl')->assign('cats',$cats)    ;   

        // show them in the templatee

        System::Get('tpl')->draw('blog');
    }

}

Blog Where articles should be shown

but there's no problem in Blog

the problem is that $articles = $this->articlesModel->Get(); returns blank array doesn't contain the information from DB

The ArticlesCatsModel

ArticlesCatsModel includes/model

The ArticleModel

ِArticlesModel

3: http://pastebin.com/z2dzcBVc it returns blank array even there are rows in DB


This is Template i should see the articles that i get from DB

but the result is No Articles

to check where exactly the problem

i typed

in Articles.Controller.php

    $articles = $this->articlesModel->Get();
    var_dump($articles).'<br/>'; 
    $cats     = $this->articlesCatsModel->Get();
    print_r($cats); 

and if you noticed in the picture it success when i call $cats but i return blank array of $articles

Template

smile
  • 117
  • 3
  • 16

1 Answers1

1

Try to replace

SELETE * FROM 

with

SELECT * FROM 

in your Get() method in ArticlesModel.php file.

Jo Smo
  • 6,923
  • 9
  • 47
  • 67
  • a small question :D :D you think public function __construct(ArticlesModel $articlesmodel,ArticlesCatsModel $catsmodel) { $this->articlesModel = $articlesmodel; //$this->articlesModel refere to private etc the object $this->articlesCatsModel = $catsmodel; } better than making object like new ArticlesModel() in ArticlesController..construct is better !? – smile Aug 10 '15 at 00:45
  • It depends, if all the parameters in your constructor are needed in order for the object to work, then the constructor with parameters is helpful, but if you should need to create an `ArticlesModel()` in a case were not all parameters would be needed, then you would have to create another constructor for it. https://en.wikipedia.org/wiki/Function_overloading – Jo Smo Aug 10 '15 at 05:40
  • 1
    i was going to get crazy :D i edited everything again because of an error with no reason i think then i get the same error as the dead page :D i copy code from original project to check i get same error then i restarted 3 times hahaah but i paste code from tmp folder if you remember then it worked i think i was the same code !! the page is crazy ! :D :D :D – smile Aug 15 '15 at 23:06
  • http://stackoverflow.com/questions/32075143/view-page-error-php?noredirect=1#comment52047277_32075143 look :D :D :D – smile Aug 18 '15 at 15:43
  • sorry but if you have time look at this code it makes me crazy http://pastebin.com/0SQ8jZGA if(isset($_GET['id']) && (int)$_GET['id']>0) will be executed & else too how in same time !!!!! – smile Aug 20 '15 at 00:14
  • You call it recursively here: `if($this->usersModel->Update($id,$data))`, that's why both can get executed. – Jo Smo Aug 20 '15 at 00:53
  • if($this->usersModel->Update($id,$data)) already inside else ! & if(isset($_GET['id']) && (int)$_GET['id']>0) will be executed first so else shouldn't be executed – smile Aug 20 '15 at 00:59
  • If i would have to guess... It looks like else gets executed first and then the Update() method is called again recursively from inside the else block in the Update() method. – Jo Smo Aug 20 '15 at 01:42
  • ok but because the id already exist so if(isset($_GET['id']) && (int)$_GET['id']>0) will be executed so else shouldn't work – smile Aug 20 '15 at 12:06
  • if($this->usersModel->Update($id,$data)) another function from UsersModel object :D i feel like crazy :D – smile Aug 20 '15 at 12:09
  • i think i know it now :D 1- updateuser.php?id=5 if will be executed then updateuser.php will be shown so it will call the function again and if will not work because there's no id in the url ! then it will update and draw the page (success) – smile Aug 20 '15 at 18:33
  • I'm glad to hear that you have figured it out. :) – Jo Smo Aug 20 '15 at 20:44
  • i think yes i understand it :D ,,, – smile Aug 21 '15 at 11:53
  • i finished the tutorials and the task :)) and trying to make small project that takes name and message then show them on the same page,, but i need to know while($row = mysqli_fetch_assoc($querycheck)) why we always do that way to fetch the array ?? i don't undertstand it ? – smile Aug 25 '15 at 14:42
  • it will be while($row = first row ) then ($row = second row ) etc .. ? this is the way to fetch the to fetch all in assoc array – smile Aug 25 '15 at 14:43
  • Without the while, you will get only 1 row from your db. – Jo Smo Aug 27 '15 at 10:38
  • but strange it's something like while ($row = 1) i mean we didn't type while($row = mysqli_fetch_assoc($querycheck)) {$row = $row + 1 ;} how it already takes every row :D – smile Aug 27 '15 at 12:19
  • maybe :D :D :D it's special for while($row = mysqli_fetch_assoc($querycheck)) – smile Aug 27 '15 at 12:21
  • `mysqli_fetch_assoc()` is a `PHP` method that gets an array of all the rows which match the query. Then you loop through the array with `while()` or just select the first one without `while()`. I hope that will help. If not, try to read the PHP documentation, it's more in detail there. – Jo Smo Aug 29 '15 at 23:10
  • 1
    Thanks ...it was holiday :D :D D: ..i hope you are fine ^_^ – smile Sep 05 '15 at 18:59
  • http://stackoverflow.com/questions/32486139/php-login-and-logout-for-website :D if you have time cheack please :D – smile Sep 09 '15 at 18:13