Part of the EllisLab Network
   
 
stuck with this php class
Posted: 04 March 2008 07:27 PM   [ Ignore ]  
Grad Student
Rank
Total Posts:  39
Joined  03-18-2007

Hi, im sorta new to writing php 5 classes, would someone take a minute to look over this code and see if they can spot the problem. im my app, i am submitting a form via ajax to a php file which uses this php class.

<?php

    
class Items{
        
        
var $user_id;
        var 
$date_of;
        var 
$date_cleared;
        var 
$itemtype;
        var 
$merchant;
        var 
$amount;
        var 
$tags;
        var 
$num_of_items;
        
        function 
__construct()
        
{    
            $this
->user_id $_SESSION['userid'];
        
}
        
        
public function add_initial_balance($amount)
        
{
            $this
->date_of date('Y-m-d');
            
$this->date_cleared null;
            
$this->itemtype 0;
            
$this->merchant 0;
            
$this->amount $amount;
            
$this->tags null;
            
            
$sql "INSERT INTO items (id, user_id, merchant_id, itemtype_id, amount, date_of, date_cleared, tags) VALUES('', '$this->user_id', '$this->merchant', '$this->itemtype', '$this->amount', '$this->date_of', '$this->date_cleared', '$this->tags')";
            
$result mysql_query$sql );
            
            if( ! 
mysql_error() )
            
{
                
return true;
            
}
            
else
            
{
                
return false;
            
}
        }
        
        
public function build_items_list()
        
{
            $sql 
"SELECT * FROM items WHERE user_id = " $this->user_id "";
            
$result mysql_query$sql );
            
$row mysql_fetch_object$result );
            
$rows mysql_num_rows$result );
            
            
$date_of $row->date_of;
            
$date_cleared $row->date_cleared;
            
$itemtype $row->itemtype_id;
            
$merchant $row->merchant_id;
            
$amount $row->amount;
            
            for(
$i 0$i <= $rows$i++)
            
{
                $return 
.= "<ul class='item'>";
                
$return .= "<li class='dof'><a href=''>$row->date_of</a></li>";
                
$return .= "<li class='clr'><a href=''>$row->date_cleared</a></li>";
                
$return .= "<li class='typ'><a href=''>$row->itemtype_id</a></li>";
                
$return .= "<li class='mer'><a href=''>$row->merchant_id</a></li>";
                
$return .= "<li class='amt'><a href=''>$row->amount</a></li>";
                
$return .= "<li class='bal'><a href=''>balance</a></li>";
                
$return .= "</ul>";
            
}
            
            
return $return;
        
}
        
public function add_item($date_of$itemtype$merchant$amount)
        
{
            $this
->date_of $date_of;
            
$this->date_cleared null;
            
$this->itemtype $this->find_item($itemtype);
            
$this->merchant $this->find_merchant($merchant);
            
$this->amount $amount;
            
$this->tags null;
            
            
$sql "INSERT INTO items (id, user_id, merchant_id, itemtype_id, amount, date_of, date_cleared, tags) VALUES('', '$this->user_id', '$this->merchant', '$this->itemtype', '$this->amount', '$this->date_of', '$this->date_cleared', '$this->tags') ";
            
$result mysql_query$sql );
            
            if( ! 
mysql_error() )
            
{
                
return true;
            
}
            
else
            
{
                
return false;
            
}
        }
        
        
private function find_item($str)
        
{
            $sql 
"SELECT * FROM itemtype WHERE name = '$str' AND user_id = '$this->user_id'";
            
$result mysql_query($sql);
            
$numrows mysql_num_rows$result );
            
$row mysql_fetch_object$result );
            
            if( 
$numrows == )
            
{
                mysql_query
"INSERT INTO itemtype (id, user_id, name) VALUES ('', '$this->user_id', $str)" );
                return 
mysql_insert_id();
            
}
            
else
            
{
                
return $row->id;
            
}
        }
        
        
private function find_merchant($str)
        
{
            $sql 
"SELECT * FROM merchants WHERE name = '$str' AND user_id = '$this->user_id'";
            
$result mysql_query($sql);
            
$numrows mysql_num_rows$result );
            
$row mysql_fetch_object$result );
            
            if( 
$numrows == )
            
{
                mysql_query
"INSERT INTO merchants (id, user_id, name) VALUES ('', '$this->user_id', $str)" );
                return 
mysql_insert_id();
            
}
            
else
            
{
                
return $row->id;
            
}
        }
    }

?> 

this issue arises when i call the add_item method and send the merchant and itemtype through the find_item and find_merchant methods. i cant figure out why those methods refuse to return anything or insert a new row when i expect them to. thanks.

Profile
 
 
Posted: 04 March 2008 08:03 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

This is not a CI library so problems could be many. What checking have you done so far to find the error?

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 04 March 2008 08:09 PM   [ Ignore ]   [ # 2 ]  
Grad Student
Rank
Total Posts:  39
Joined  03-18-2007

i guess im just more worried about my syntax. if i do not pass those variables through the methods,

$this->itemtype $itemtype;
            
$this->merchant $merchant

everything works. i just cant figure out why those methods are failing. and echos wont show so i haven’t been able to debug anything.

Profile
 
 
Posted: 04 March 2008 08:29 PM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

To see output or errors call the script from the browser address bar, using the same URL the javascript uses.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile
 
 
Posted: 04 March 2008 08:56 PM   [ Ignore ]   [ # 4 ]  
Grad Student
Rank
Total Posts:  39
Joined  03-18-2007

it was a syntax error just as i thought. thanks for showing my how to debug though. luv. i wasnt wrapping my variables in ’ ’ in my inserts.

Profile
 
 
Posted: 04 March 2008 09:14 PM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3198
Joined  06-10-2007

You’re very welcome smile

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

Profile