Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 29th Oct 2012, 08:26 AM   #1
Warrior Member
 
Join Date: 2009
Posts: 22
Thanks: 0
Thanked 2 Times in 2 Posts
Default
Need Help Converting Database Query into a Model and Output to a View
Share on: 
fb share twitter share gplus share more share

The query is:


PHP Code:
<?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";

?>
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:

PHP Code:
//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;
        } 
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 is offline  
Unread 29th Oct 2012, 08:46 AM   #2
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: Need Help Converting Database Query into a Model and Output to a View
Share on: 
fb share twitter share gplus share more share

Seems your function has its own output. Data are selected from DB (by categories), then for each category (by subcategories) and echoed to the screen.

What is your purpose? Are you trying to fit under your website design or you need some custom output?
viescripts is offline  
Unread 29th Oct 2012, 09:13 AM   #3
Warrior Member
 
Join Date: 2009
Posts: 22
Thanks: 0
Thanked 2 Times in 2 Posts
Default
Re: Need Help Converting Database Query into a Model and Output to a View
Share on: 
fb share twitter share gplus share more share

Originally Posted by viescripts View Post

Seems your function has its own output. Data are selected from DB (by categories), then for each category (by subcategories) and echoed to the screen.

What is your purpose? Are you trying to fit under your website design or you need some custom output?
The website was originally coded procedural, I've since started to learn OOP and want to integrate the website into the CodeIgniter framework.

The code I posted was the old version, I now need to convert this to object orientated passing it through the MVC framework.

I need somebody to help me put the actual database code into the model and pull it back out in the view.
scottlpool2003 is offline  
Unread 29th Oct 2012, 09:28 AM   #4
Active Warrior
 
Join Date: 2012
Posts: 97
Thanks: 12
Thanked 12 Times in 11 Posts
Default
Re: Need Help Converting Database Query into a Model and Output to a View
Share on: 
fb share twitter share gplus share more share

codeigniter is a very well documented framework, you will have to modify your code to their model.

If you are trying to learn OOP, you'd rather consider setup your own classes. For CodeIgniter you will have to learn their framework before you move any further.
viescripts is offline  
Unread 29th Oct 2012, 10:13 AM   #5
Warrior Member
 
Join Date: 2009
Posts: 22
Thanks: 0
Thanked 2 Times in 2 Posts
Default
Re: Need Help Converting Database Query into a Model and Output to a View
Share on: 
fb share twitter share gplus share more share

Originally Posted by viescripts View Post

codeigniter is a very well documented framework, you will have to modify your code to their model.

If you are trying to learn OOP, you'd rather consider setup your own classes. For CodeIgniter you will have to learn their framework before you move any further.
That is what my OP was asking, help to convert my query into the CodeIgniter framework...
scottlpool2003 is offline  
Closed Thread


Bookmarks

Tags
converting, database, model, query


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 01:27 PM.