CodeIgniter Forums
Using Former functionality with new project - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Using Former functionality with new project (/showthread.php?tid=50631)

Pages: 1 2


Using Former functionality with new project - El Forum - 04-02-2012

[eluser]xtremer360[/eluser]
This was from a former project that I'm not trying to add to my new project and I'm trying to figure out what I should do to still be able to keep its functionality with a combination of my controller, library, and model.

Code:
<?php
            if ($access_level_id == 2 || $access_level_id == 3) {
       $query = "SELECT
         characters.id
        FROM
         characters";
       $result = mysqli_query ($dbc,$query);
       $total_num_characters = mysqli_num_rows($result);
    
       $query = "SELECT
         user_characters.id
        FROM
         user_characters
        INNER JOIN user_accounts
            ON user_accounts.id = user_characters.user_id";
      } else {
       $query = "SELECT
         user_characters.id
        FROM
         user_characters
        INNER JOIN user_accounts
            ON user_accounts.id = user_characters.user_id
        WHERE
         user_accounts.id = '".$user_id."'";
      }
      $result = mysqli_query($dbc,$query);
            echo

      $num_available_characters = mysqli_num_rows($result);
            if (($num_available_characters > "1") || (($access_level_id == 2 || $access_level_id == 3) && (isset($total_num_characters)) && ($total_num_characters > "0"))) {
            ?>
       <form method="POST" id="change_default_character">
                <select class="dropdown" name="new_default_character_id" id="new_default_character_id" title="Select Character">                
            &lt;?php
    
       if ($default_character_id > "0") {
        print "<option value=".$default_character_id.">".$default_character_name;
       } else {
        print "<option value=0>- Select -";
       }
                if ($access_level_id == 2 || $access_level_id == 3) {
        $query = "SELECT
          characters.id,
          characters.character_name
         FROM
          characters
         WHERE
          characters.id <> '".$default_character_id."' AND
          characters.status_id = '1'
         ORDER BY
          characters.character_name";
       } else {
        $query = "SELECT
          characters.id,
          characters.character_name
         FROM
          characters
         INNER JOIN
             user_characters
             ON characters.id = user_characters.character_id      
         INNER JOIN
          user_accounts
             ON user_accounts.id = user_characters.user_id
         WHERE
          user_accounts.id = '".$user_id."' AND
          user_characters.character_id <> '".$default_character_id."' AND
          characters.status_id = '1'
         ORDER BY
          characters.character_name";
       }
       $result = mysqli_query ($dbc,$query);
       $num_rows = mysqli_num_rows ($result);
       if ($num_rows > 0) {
        if ($access_level_id == 2 || $access_level_id == 3) {
         print "<optgroup label=\"** Active Characters **\">";
        }
                    while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
         print "<option value=\"".$row['id']."\">".$row['character_name']."</option>\r";
        }
       }
                if ($access_level_id == 2 || $access_level_id == 3) {
        $query = "SELECT
          characters.id,
          characters.character_name
         FROM
          characters
         WHERE
          characters.id <> '".$default_character_id."' AND
          characters.status_id = '2'
         ORDER BY
          characters.character_name";
       } else {
        $query = "SELECT
          characters.id,
          characters.character_name
         FROM
          characters
         LEFT JOIN
          user_characters
             ON characters.id = user_characters.character_id      
         LEFT JOIN
          user_accounts
             ON user_accounts.id = user_characters.user_id
                        WHERE
          user_accounts.id = '".$user_id."' AND
          user_characters.character_id <> '".$default_character_id."' AND
          characters.status_id = '2'
         ORDER BY
          characters.character_name";
       }
       $result = mysqli_query ($dbc,$query);
       $num_nows = mysqli_num_rows($result);
       if ($num_rows > "0") {
        print "<optgroup label=\"** Inactive Characters **\">";
        while ( $row = mysqli_fetch_array ( $result, MYSQL_ASSOC ) ) {
            print "<option value=\"".$row['id']."\">".$row['character_name']."</option>\r";    
        }
       }
                ?&gt;
       </select>
                &lt;/form&gt;
            &lt;?php
      } else {
       print "<h1>".$default_character_name."</h1>\n";
      }
            ?&gt;



Using Former functionality with new project - El Forum - 04-02-2012

[eluser]CroNiX[/eluser]
Whats the point of using a framework like CI if you are just going to manually access the database via traditional php/mysql and not use CI's functionality? You don't even use anything CI related at all in any of that code.


Using Former functionality with new project - El Forum - 04-02-2012

[eluser]xtremer360[/eluser]
It was an old script on a NON CI page. I'm wanting to import the functionality into a CI functionality script. That's what I meant by transfering it to it.


Using Former functionality with new project - El Forum - 04-02-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Queries -> Models (don't validate data here, please)
Check and manipulate data -> Controllers
Display data -> Views



Using Former functionality with new project - El Forum - 04-02-2012

[eluser]xtremer360[/eluser]
Theres just so much and the amount of if statements


Using Former functionality with new project - El Forum - 04-03-2012

[eluser]xtremer360[/eluser]
I have 7 different possible queries and all are basically the same except for a few different things at different points. So I'm trying to figure out how I can make one query with all of all this.


Using Former functionality with new project - El Forum - 04-03-2012

[eluser]Mauricio de Abreu Antunes[/eluser]
Put this query in your model. You can use if statements for build other queries into these methods. Don't worry, it's more simple than your original code.


Using Former functionality with new project - El Forum - 04-03-2012

[eluser]xtremer360[/eluser]
Put what query in my model I have 7 of them and I trout I shouldn't put f statements fr that in a model.


Using Former functionality with new project - El Forum - 04-03-2012

[eluser]xtremer360[/eluser]
Any ideas?


Using Former functionality with new project - El Forum - 04-04-2012

[eluser]xtremer360[/eluser]
*bump*