• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stuck with this php class

#1
[eluser]jvittetoe[/eluser]
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.
Code:
<?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 == 0 )
            {
                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 == 0 )
            {
                mysql_query( "INSERT INTO merchants (id, user_id, name) VALUES ('', '$this->user_id', $str)" );
                return mysql_insert_id();
            }
            else
            {
                return $row->id;
            }
        }
    }

?&gt;

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.

#2
[eluser]wiredesignz[/eluser]
This is not a CI library so problems could be many. What checking have you done so far to find the error?

#3
[eluser]jvittetoe[/eluser]
i guess im just more worried about my syntax. if i do not pass those variables through the methods,
Code:
$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.

#4
[eluser]wiredesignz[/eluser]
To see output or errors call the script from the browser address bar, using the same URL the javascript uses.

#5
[eluser]jvittetoe[/eluser]
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.

#6
[eluser]wiredesignz[/eluser]
You're very welcome Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.