0

I'm still trying to get my head around MVC I have a straight php/mysql page that basically works by getting select * FROM table1 then looping through the result set and on each loop running a Join to find sub project information..

but I can't figure out how to split this up and do it in MVC..

code is as below any help or pointers in the right direction would be appreaciated!

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM building";
$result=mysql_query($query);

$num=mysql_numrows($result);

$i=0;
while ($i < $num) {
    //for loop make a table
    //this is the heading info

    $b_id=mysql_result($result,$i,"id");
    $address=mysql_result($result,$i,"address");
    $description=mysql_result($result,$i,"description");

?>
    <table width="772px" border="0" align="center" cellpadding="5" cellspacing="0">
     <tr bgcolor="#4682B4" height="50">
      <td width="5%"></td>
      <td><font size="4" face="tahoma" color="white"><strong><? echo $address; ?><a href="http://localhost:8888/project-add.php?b_id=<? echo $b_id; ?>">Add Project</a></strong></font></td>
      <td bgcolor="#4682B4" align="center" width="50%"><input id="lnk<? echo $i; ?>" type="button" value="[+] Expand" onclick="toggle_visibility('tbl<? echo $i; ?>','lnk<? echo $i; ?>');"></td>
     </tr>
     <tr>
      <td colspan="3">
       <table width="103%" border="1" cellpadding="5" cellspacing="0" id="tbl<? echo $i; ?>" class="tbl">

        <?
        $query="SELECT project.id AS p_id, project.name AS p_name, project.description AS p_des, project.building_id as p_b_id, building.id AS b_id, building.address AS b_name
            FROM project JOIN building
            ON project.building_id = building.id
            WHERE building_id='$b_id'";
        $proj_result=mysql_query($query);
        $proj_num=mysql_numrows($proj_result);

        $j=0;
        while ($j < $proj_num) { //while 1

            $p_id=mysql_result($proj_result,$j,"p_id");
            $p_name=mysql_result($proj_result,$j,"p_name");
            $p_des=mysql_result($proj_result,$j,"p_des");
            $b_name=mysql_result($proj_result,$j,"b_name");


        ?>
        <tr>
         <td width="5%"></td>
         <td width="45%"><? echo $p_name; ?></td>
         <td width="50%" align="center">XXXXXXX</td>
        </tr>

        <?
            $j++;
        } //end while 1
        ?>
      </table>
      </td>
     </tr>
    </table>
    <?
    $i++;
}



mysql_close();
?>
Keiiz
  • 45
  • 4
  • It honestly doesn't look like you've tried anything at all, there's nothing here that suggests you're using Codeigniter or any framework. Just for starters, read the user guide on how to connect to your database. Have you even created a controller yet? Read about how to create models? – Wesley Murch Jul 25 '12 at 03:54
  • If you are trying to understand MVC, then using CodeIgnater would be the last thing to do. – tereško Jul 25 '12 at 04:04
  • Hi Wesley - this isn't code igniter at all! sorry I should have been more clear - I was trying to convert it to code igniter but I couldn't figure out how to go about the model section! – Keiiz Jul 25 '12 at 04:21
  • Feel a bit silly that I didn't mention that before sorry! – Keiiz Jul 25 '12 at 04:22
  • The only way I could think of doing it is having the model return the join as a single results array then using php in the view section to split it up? But instinctively that doesn't feel quite right.. – Keiiz Jul 25 '12 at 04:27
  • In the meantime they also invented CSS, so please don't use those deprecated html attributes. – Damien Pirsy Jul 25 '12 at 04:53

2 Answers2

1

Adding a framework on top of existing codebase is never a good thing. Especially if you are using something so fundamentally broken as CodeIgniter.

Instead you should research OOP and the principles and law that come this this paradigm. Things like SOLID and LoD.

Another thing you should look into would be dropping the ancient mysql_* API. It is not maintained anymore. Community has even begun to push for deprecation, which is quite evident by the red box in documentation. It is recommended to start using either PDO or MySQLi. If you choose PDO, here is good tutorial.

Next thing is: separate HTML from the application logic. PHP itself is a good templating language. Take the advantage of it. This article might help with it.

Also, on the subject of model in MVC, you might find this comment be useful.

Community
  • 1
  • 1
tereško
  • 58,060
  • 25
  • 98
  • 150
  • I'm genuinely interested in hearing more about *"something so fundamentally broken as CodeIgniter"*. In what way is the framework itself (not developer's usage of it) "fundamentally broken"? – Wesley Murch Jul 25 '12 at 19:42
  • I had hoped you will just give up, because i hate to repeat same arguments again and again, and again .. and again. Here are few of the issues with CI: global state everywhere, static calls, use of PHP4 code, mis-implementation of MVC (they actually have ORM-Template-Adapter thing instead of MVC) and complete ignorance in regards to LoD and SOLID principles. – tereško Jul 26 '12 at 18:10
  • Didn't mean to make you rehash things you've said to others. I did read and upvoted your manifesto but didn't follow all the links yet. Global state... yes, this and having only one instance of each class has been a pain in the ass for me with CI. I'll be sure to continue reading and watching the videos you linked to to learn more about the other stuff. Out of curiosity (because I respect your views), which, if any, PHP framework do you like personally? – Wesley Murch Jul 26 '12 at 19:58
  • OK, it seems this is a question you are always dodging or dismissing with some negative quip. I'll leave you be about it. – Wesley Murch Jul 27 '12 at 04:08
0

Ok don't know if people are going to scream at my solution but here's how I ended up doing it: thanks for all the replies

public function index()
{

    $data['building'] = $this->building_model->get_building();

    $this->load->view('templates/header', $data);

    foreach ($data['building'] as $building_item)
    {
            $data['address']  = $building_item['address'];
            $data['id']  = $building_item['id'];
            $this->load->view('building/tbl1', $data);
            $data['project'] = $this->building_model->get_project($building_item['id']);
            $this->load->view('building/tbl2', $data);
            $this->load->view('building/tbl3');             

    }

    $this->load->view('templates/footer');

}
Keiiz
  • 45
  • 4