0

Im trying to run my program that i made in php but in this case i wanna run it using codeigniter.

i've been trying to fetch the tables without success, this is my old code:

    <?php include('connect.php'); 

?>

<html>
<head>

    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

</head>
    <body>

    <div class="container"> 
    <div class="row">
    <div class="col-md-12">



    <a href="estudiante.php"><button type="button" class="btn btn-success">AGREGAR</button></a><br /><br />
        <h2 align="center">TABLA:MATERIAS</h2>
        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>Carrera</th>
                    <th>Nombre</th>
                    <th>Descripcion</th>
                    <th>Carga horaria (hs)</th>
                    <th>Accion</th>
                </thead>

    <?php

                $sql=mysql_query("SELECT s.*, c.nombre AS carrera FROM materias s LEFT JOIN carreras c ON s.carrera_id=c.id");
                $i=1;
                    while($row=mysql_fetch_array($sql)){
                        echo "<tr>
                                <td>".$i."</td>
                                <td>".$row['carrera']."</td>
                                <td>".$row['nombre']."</td>
                                <td>".$row['descripcion']."</td>
                                <td>".$row['carga_horaria']."</td>
                                <td align='center'>
                                    <a href='editar.php?editar=1&iden=".$row['id']."'><button type='button' class='btn btn-primary'>EDITAR</button></a> |
                                    <a href='borrar.php?borrar=1&iden=".$row['id']."'><button type='button' class='btn btn-danger'>BORRAR</button></a>
                                </td>
                        </tr>";
                        $i++;

                    }
                ?>


            </table>    

        </div>
        </div>
        </div>

    </body>



</html>

Here is the "connect" file:

    <?php

    $con=mysql_connect('localhost','root','root')OR die('error : '.mysql_error());
    $db=mysql_select_db('desafio');

    if($db){
        echo '';

    }else{
        echo 'Error :' .mysql_error(); 
    }


?>

But i dont know how should i do it using Codeigniter, i cannot fetch data from tables "materias"and "carreras".I've thought something like this (it is just an example of my Crudmodel):

    <?php

    Class Crudmodel extends CI_Model{

        public function getRecords(){
            $query = $this->db->get('materias');

            if($query -> num_rows() > 0){

                return $query->row();
            }

        }

    }


?>

Then, here is my controller file:

    <?php

    class Home extends CI_Controller{

        public function index(){
            $records = $this->Crudmodel->getRecords();
            $this->load->view('home', ['records'=>$records]);

        }

    }


?>

Finally, here is my main file (which should have the information of both tables)

    <?php include('header.php'); ?>

<?php include('footer.php'); ?>


    <div class="container"> 
    <div class="row">
    <div class="col-md-12">

        <h2 align="center">TABLA:MATERIAS</h2>
        <input id="busqueda_tabla" type="text">
            <table class="table table-hover" align="center" border="1" cellspacing="0" cellpadding="0" width="700" id="tabla_busqueda">
                <thead>
                    <th>id</th>
                    <th>Carrera</th>
                    <th>Nombre</th>
                    <th>Descripcion</th>
                    <th>Carga horaria (hs)</th>
                    <th>Accion</th>
                </thead>


<?php

                $sql=mysql_query("SELECT s.*, c.nombre AS carrera FROM materias s LEFT JOIN carreras c ON s.carrera_id=c.id");
                $i=1;
                    while($row=mysql_fetch_array($sql)){
                        echo "<tr>
                                <td>".$i."</td>
                                <td>".$row['carrera']."</td>
                                <td>".$row['nombre']."</td>
                                <td>".$row['descripcion']."</td>
                                <td>".$row['carga_horaria']."</td>
                                <td align='center'>
                                    <a href='editar.php?editar=1&iden=".$row['id']."'><button type='button' class='btn btn-primary'>EDITAR</button></a> |
                                    <a href='borrar.php?borrar=1&iden=".$row['id']."'><button type='button' class='btn btn-danger'>BORRAR</button></a>
                                </td>
                        </tr>";
                        $i++;

                    }
                ?>

            </table>

        </div>
        </div>
        </div>

Hope you can help me :(

1

2 Answers 2

1

well done for choosing CI.

We need to know what errors you are having, on which files and lines, Which parts that don't work, and more details concerning your error.. However, I can give a checklist to troubleshoot.

  1. please make sure your connection string/DB configuration is correct.
  2. in your model class, have you called $this->load->database(); in the construct method?
  3. have you loaded your model in your controller. e.g.: calling $this->load->model('Crudmodel'); on your controller construct method?

the easiest step by step would be detailed on this page: http://www.codeigniter.com/user_guide/tutorial/news_section.html

Sign up to request clarification or add additional context in comments.

1 Comment

Dude, i just wanna have this code in my CodeIgniter program: ideone.com/CVAbV6 But, i gotta "call" the tables ("materias" and "carreras") before..inthis way: ideone.com/hfF8sG But i dont know how should i do that :S
0
  <?php

    Class Crudmodel extends CI_Model{

        public function getRecords(){

            $this->db->select('s.*, c.nombre AS carrera')
                     ->from('materias s')
                     ->join('carreras c', 's.carrera_id = c.id', 'left');
            $q = $this->db->get();

            if($q -> num_rows() > 0){

                return $q->result();
            }

            return false;

        }

    }


?>

this?
edit: included manual @ https://www.codeigniter.com/userguide3/database/query_builder.html

more edits below:

<?php

    class Home extends CI_Controller{

        public function index(){
            $this->load->model('Crudmodel');
            $data['records'] = $this->Crudmodel->getRecords();
            $this->load->view('home', $data['records']);

        }
    }
?>

view:

  <table>
    <?php
        $i=1;
        foreach($records as $record) {
            // build your table view here
            echo "<tr>
                      <td>".$i."</td>
                      <td>".$record->carrera."</td>
                      <td>".$record->nombre."</td>
                      <td>".$record->descripcion."</td>
                      <td>".$record->carga_horaria."</td>
                      <td align='center'>
                         <a href='editar.php?editar=1&iden=".$record->id."'><button type='button' class='btn btn-primary'>EDITAR</button></a> |
                         <a href='borrar.php?borrar=1&iden=".$record->id."'><button type='button' class='btn btn-danger'>BORRAR</button></a>
                      </td>
                  </tr>";
        }
        $i++;
    ?>
    </table>

13 Comments

Thank you for reply!,but take a look at this: stackoverflow.com/questions/43034938/…
I added the basic workings, up to you to generate the table in the view.
Thank you for reply again!, but i dont get it dude :(.Can help me to show the table like if it was the "old" code.. doing something like this in the view? ideone.com/QvvcXh
I edited the view a bit more. We use "->" because we are passing an object not an array.
sry forgot to load model in controller $this->load->model('Crudmodel');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.