Need Help Converting Database Query into a Model and Output to a View

Discussion in 'PHP' started by scottlpool2003, Oct 29, 2012.

  1. #1
    The query is:

    <?php
    //Set default number of columns
    //GET CATEGORIES
    
    $numCols = 2;
    $count = 0;
    
    $result = mysql_query("SELECT * FROM categories");
    
    //Count number of rows and divide into 2
    $numPerCol = ceil(mysql_num_rows($result) / $numCols);
    echo "<div style=\"width:369px;\">\n";
    for($col = 1; $col <= $numCols; $col++) {
        echo "<div style=\"width:46.7%;float:right;margin-left:5px;\">";
        for($row = 0; $row < $numPerCol; $row++) {
            $resultRow = mysql_fetch_assoc($result);
            if($resultRow == false) {
                break;
            }
    
    
    //Convert data
            $title = $resultRow['title'];
            $id = $resultRow['id'];
            $url = $resultRow['url'];
    
    //Output data
            echo "<h3>$title</h3>";
    
    //Get subcategories
            $resultb = mysql_query("SELECT * FROM cat_objects WHERE parent_id = '$id' LIMIT 5");
    
    //Check if no results
            if(mysql_num_rows($resultb)==0){
                $message = "There are no subcategories";
            }
    
    //There are results, get data
            else
            {
                $message = "<a href=\"categories/\" title=\"View All Categories\">...</a>";
            }
            while($rowb = mysql_fetch_array($resultb))
    
    //Add comma to all but last result
            {
                if ($count++ > 0) echo " ";
                $titleb = $rowb['title'];
                $urlb = $rowb['url'];
                $sid = $rowb['id'];
                echo "<a href=\"categories/cat.php?id=$sid\" title=\"$titleb\">$titleb</a>";
            }
            echo "$message";
        }
        echo "\n</div>\n";
    }
    echo "</div>\n";
    
    ?>
    PHP:
    I'm really struggling to put it into a model as I've no idea how to convert some of the functions such as this section:

    //Count number of rows and divide into 2
    $numPerCol = ceil(mysql_num_rows($result) / $numCols);
    echo "<div style=\"width:369px;\">\n";
    for($col = 1; $col <= $numCols; $col++) {
        echo "<div style=\"width:46.7%;float:right;margin-left:5px;\">";
        for($row = 0; $row < $numPerCol; $row++) {
            $resultRow = mysql_fetch_assoc($result);
            if($resultRow == false) {
                break;
            }
    PHP:
    If anybody could help me convert the whole query into a model and pass it through the controller and to the view that would be awesome! Thanks.
     
    scottlpool2003, Oct 29, 2012 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Are you using any particular framework or ORM?
     
    jestep, Oct 29, 2012 IP
  3. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #3
    I'm using CodeIgniter as the framework and writing my code in PHPStorm.
     
    scottlpool2003, Oct 29, 2012 IP