[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;
}
}
}
?>
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.