2017-11-06 6 views
0

Kann mir jemand helfen, diese Ursache herauszufinden, ich weiß nicht, wo das Problem ist. Ich habe viele Male versucht und selbst nach vielen Tutorials zu suchen konnte immer noch nicht funktionieren. Bitte helfen Sie mir, meine Probleme zu lösen, vielen Dank.404 Seite nicht gefunden Die gewünschte Seite wurde nicht gefunden

Das ist mein Controller als home.php gespeichert

<?php 
class Home extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('HomeModel'); 
     // Your own constructor code 
    } 

} 

    function index(){ 

    $query = $this->HomeModel->getEmployees(); 
    $data['EMPLOYEES'] = null; 
    if($query){ 
    $data['EMPLOYEES'] = $query; 
    } 

    $this->load->view('index.php', $data); 
} 
?> 


Dieses Modell als HomeModel.php gespeichert

<?php 
class HomeModel extends Model { 

function HomeModel(){ 
    parent::Model(); 
} 

function getEmployees(){ 
    $this->db->select("jantina,bangsa,agama"); 
    $this->db->from('pesakit'); 
    $query = $this->db->get(); 
    return $query->result(); 
} 
} 
?> 


Dies ist Ansichten gespeichert als index.php

<?php 

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Display Records From Database Using Codeigniter</title> 
    <link href="<?= base_url();?>css/bootstrap.css" rel="stylesheet"> 
</head> 
<body> 
    <div class="row"> 
    <div style="width:500px;margin:50px;"> 
    <h4>Display Records From Database Using Codeigniter</h4> 
    <table class="table table-striped table-bordered"> 
    <tr><td><strong>Jantina</strong></td><td><strong>Bangsa</strong></td><td><strong>Agama</strong></td></tr> 
    <?php foreach($EMPLOYEES as $employee){?> 
    <tr><td><?=$employee->jantina;?></td><td><?=$employee->bangsa;?></td><td><?=$employee->agam;?></td></tr>  
     <?php }?> 
    </table> 
    </div> 
    </div> 
</body> 
</html> 


config.php

$config['base_url'] = 'http://localhost/project/'; 
$config['index_page'] = 'index.php'; 
$config['uri_protocol'] = 'AUTO'; 


routes.php

$route['default_controller'] = 'home'; 
$route['404_override'] = ''; 
$route['translate_uri_dashes'] = FALSE; 


database.php

$active_group = 'main'; 
$active_record = TRUE; 
$query_builder = TRUE; 

$db['default'] = array(
    'dsn' => '', 
    'hostname' => 'localhost', 
    'username' => 'root', 
    'password' => '', 
    'database' => '', 
    'dbdriver' => 'mysqli', 
    'dbprefix' => '', 
    'pconnect' => FALSE, 
    'db_debug' => (ENVIRONMENT !== 'production'), 
    'cache_on' => FALSE, 
    'cachedir' => '', 
    'char_set' => 'utf8', 
    'dbcollat' => 'utf8_general_ci', 
    'swap_pre' => '', 
    'encrypt' => FALSE, 
    'compress' => FALSE, 
    'stricton' => FALSE, 
    'failover' => array(), 
    'save_queries' => TRUE 

$db['main']['hostname']='xx.x.xxx.xx'; 
$db['main']['username']='this server's username'; 
$db['main']['password'] ='this server's password'; 
$db['main']['database'] = 'database name'; 
$db['main']['dbdriver'] = 'mysql'; 
$db['main']['dbprefix'] = ''; 
$db['main']['pconnect'] = TRUE; 
$db['main']['db_debug'] = TRUE; 
$db['main']['cache_on'] = FALSE; 
$db['main']['cachedir'] = ''; 
$db['main']['char_set'] = 'utf8'; 
$db['main']['dbcollat'] = 'utf8_general_ci'; 
$db['main']['swap_pre'] = ''; 
$db['main']['autoinit'] = TRUE; 
$db['main']['stricton'] = FALSE; 
); 


Helfen Sie mir bitte, ich bin total ein Neuling für CI. Danke vielmals.

+0

Ist Ihr andere Controller arbeiten cool? Oder aber .htaccess zeigen –

+0

\t Erfordern alle verweigert \t von allen Deny flac

Antwort

0

Wenn es sich nicht um einen Tippfehler im Controller handelt, haben Sie eine zusätzliche schließende Klammer über der Funktion index().

<?php 
class Home extends CI_Controller { 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('HomeModel'); 
     // Your own constructor code 
    } 


} <--- THIS IS CLOSING YOUR CONTROLLER CLASS. 


    function index(){ 

    $query = $this->HomeModel->getEmployees(); 
    $data['EMPLOYEES'] = null; 
    if($query){ 
    $data['EMPLOYEES'] = $query; 
    } 

    $this->load->view('index.php', $data); 
} 

} <--- IT NEEDS TO BE DOWN HERE. 

?> 
-1

Replace (Wenn der Index Ihrer Sicht Seite Name ist)

$this->load->view('index.php', $data); 

Um

$this->load->view('index', $data); 
0

Es gibt eine Menge hier falsch. Beginnen wir mit Ihrem Heim-Controller.

  • Legen Sie keine ?> am Ende Ihrer Dateien.
  • Verschieben Sie Ihre Indexfunktion als Methode in die Home-Controller-Klasse.
  • Ich vereinfachte den Code ein wenig, wenn die getEmployees() Methode null sowieso bei Fehler zurückgibt.
  • Ich würde die compact($employees) Funktion hier verwenden. Es ist das gleiche wie Schreiben ['employees' => $employees].
  • Seien Sie konsistent mit der Benennung, verwenden Sie nicht alle Großbuchstaben. Alle Großbuchstaben für globale Konstanten speichern
  • Sie benötigen den .php im ersten Parameter $this->load->view() nicht.

...

<?php 

class Home extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('HomeModel'); 
    } 

    public function index() 
    { 
     $employees = $this->HomeModel->getEmployees(); 
     $this->load->view('index', compact('employees')); 
    } 
} 

Viele der gleichen für die Home Modell.

<?php 

class HomeModel extends CI_Model 
{ 
    public function getEmployees() 
    { 
     $this->db->select('jantina, bangsa, agama'); 
     $this->db->from('pesakit'); 
     return $this->db->get()->result(); 
    } 
} 

Für die index.php Seite.

  • Entfernen Sie das öffnende Tag <?php aus der ersten Zeile Ihrer Datei.
  • Ich würde kurz-Tags wie <?= nicht empfehlen.
  • In View-Dateien, ist es besser, mit Ihrem foreach, if, while, etc Blöcke explizit durch Verwendung foreach (...) : endforeach;

...

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <title>Display Records From Database Using Codeigniter</title> 
    <link href="<?php echo base_url();?>css/bootstrap.css" rel="stylesheet"> 
</head> 
<body> 
    <div class="row"> 
    <div style="width:500px;margin:50px;"> 
     <h4>Display Records From Database Using Codeigniter</h4> 
     <table class="table table-striped table-bordered"> 
     <tr><td><strong>Jantina</strong></td><td><strong>Bangsa</strong></td><td><strong>Agama</strong></td></tr> 
      <?php foreach ($employees as $employee) : ?> 
      <tr><td><?php echo $employee->jantina; ?></td><td><?php echo $employee->bangsa; ?></td><td><?php echo $employee->agam; ?></td></tr>  
      <?php endforeach; ?> 
     </table> 
     </div> 
    </div> 
    </body> 
</html> 

denke ich config.php und routes.php Blick in Ordnung.

Sie haben einen Syntaxfehler in Ihrer database.php Array Formatierung bekommen. Ich würde es einfach im selben Format behalten wie es ursprünglich war.

<?php 

    $active_group = 'main'; 
    $active_record = true; 
    $query_builder = true; 

    $db['default']['hostname'] = 'localhost'; 
    $db['default']['username'] = 'root'; 
    $db['default']['password'] = ''; 
    $db['default']['database'] = ''; 
    $db['default']['dbdriver'] = 'mysqli'; 
    $db['default']['dbprefix'] = ''; 
    $db['default']['pconnect'] = false; 
    $db['default']['db_debug'] = ENVIRONMENT !== 'production'; 
    $db['default']['cache_on'] = false; 
    $db['default']['cachedir'] = ''; 
    $db['default']['char_set'] = 'utf8'; 
    $db['default']['dbcollat'] = 'utf8_general_ci'; 
    $db['default']['swap_pre'] = ''; 
    $db['default']['autoinit'] = true; 
    $db['default']['stricton'] = false; 

    $db['main']['hostname'] = 'xx.x.xxx.xx'; 
    $db['main']['username'] = 'this server\'s username'; 
    $db['main']['password'] = 'this server\'s password'; 
    $db['main']['database'] = 'database name'; 
    $db['main']['dbdriver'] = 'mysqli'; 
    $db['main']['dbprefix'] = ''; 
    $db['main']['pconnect'] = true; 
    $db['main']['db_debug'] = true; 
    $db['main']['cache_on'] = false; 
    $db['main']['cachedir'] = ''; 
    $db['main']['char_set'] = 'utf8'; 
    $db['main']['dbcollat'] = 'utf8_general_ci'; 
    $db['main']['swap_pre'] = ''; 
    $db['main']['autoinit'] = true; 
    $db['main']['stricton'] = false; 

Als Randbemerkung, lesen Sie weiter PSR-1 und PSR-2 Coding-Standards auf. Auch nehmen Sie bitte einen Blick auf PHP The Right Way.

+0

ich diese Fehlermeldung bekam: Klasse 'Model' nicht für HomeModel.php gefunden – flac

+0

Ah , das sollte CI_Model sein. – Brad

+0

Oh, du hast Recht, es sollte CI_Model sein. Nachdem ich zu CI_Model geändert habe, zeige es mir diese Nachricht wieder 'Fatal error: Erlaubte Speichergröße von 134217728 Bytes erschöpft (versucht, 41 Bytes zuzuordnen) in C: \ xampp \ htdocs \ Projekt \ system \ database \ drivers \ mysqli \ mysqli_result. php on line 237' – flac