PHPGEEK
PHPGEEK

Reputation: 55

getting function from class not working

I am trying to get a function from class to another file but when I run the code it says " Fatal error: Call to a member function start_order() on a non-object in C:\AppServ\www\store\start.php on line 4"

This is the calling code:

    <?php
    @session_start();
    require_once ( 'include/functions.php' );
    $users->start_order();
    ?>

And this is the class and the function code:

    <meta charset='utf-8' />
    <?php
    require_once ( 'include/config.php' );
    class users {

    public function start_order () {
    if($_SESSION['id']) {
    $services=mysql_query("SELECT * FROM services");
    print ("
    <form action='<?php echo $PHP_SELF; ?>' method='post'>
    <input name='orderlink' type='text'></input>
    <input name='orderquantity' type='text'></input>
    <input name='ordersubmit' type='text'></input>
    </form>
    ");
    $order_submit=$_POST ['ordersubmit'];
    $order_link=$_POST ['orderlink'];
    $order_quantity=$_POST ['orderquantity'];
    if($order_submit AND !empty($order_link) AND !empty($order_quantity)){
    while ($fetch_services=mysql_fetch_object($services)) {
    print ("<select>");
    print ("<option value='$fetch_services->id'>$fetch_services->service_name ($fetch_services->service_price)</option>");
    print ("</select>"); }
    } else { echo "خطاء";}
    $s_id=$_POST ['service'];
    $g_s_i=mysql_query("SELECT * FROM services WHERE id='$s_id'");
    $fetch_service=mysql_fetch_object($g_s_i);
    $s_price=$fetch_service->servic_price;
    $service_n=$fetch_service->service_name;
    $charge=$s_price/$order_quantity*$order_quantity;
    $date=date ("Y-m-d");
    $userbalance=mysql_query("SELECT * FROM users WHERE id='$_SESSION[id]'");
    $fetch_userbalance=mysql_fetch_object($userbalance);
    $u_balance=$fetch_userbalance->balance;
    if ($u_balance >= $charge) {
    $insert_order_values=mysql_query("INSERT INTO orders(id,u_id,u_name,date,link,charge,quantity,service_name,status) VALUES ('','$_SESSION[username]','$date','$order_link','$charge','$order_quantity','$service_n','0')"); 
    $update_balance=mysql_query("UPDATE users SET balance=$u_balance-$charge WHERE id='$_SESSION[id]'");
    }
    else { echo "not enough fund";}
    }}}

    ?>

Upvotes: 1

Views: 42

Answers (2)

cOle2
cOle2

Reputation: 4784

You need to create an instance of the object before using it:

<?php
@session_start();
require_once ( 'include/functions.php' );
$users = new users();
$users->start_order();
?> 

Upvotes: 1

mainstreetmark
mainstreetmark

Reputation: 703

You haven't made $users yet.

 $users = new users();
 $users->start_order();

Also, it's good practice to capitalize your class names, so they don't look so much like your instance.

 $users = new Users();
 $users->start_order();

Upvotes: 1

Related Questions