Jump to content

Archived

This topic is now archived and is closed to further replies.

complex05

Complicated Logic

Recommended Posts

I'm building an application for people to book guest suites in a hotel.

This is the script i'm using

[code]

    if ($action == "input_details")
    {

        $query = mysql_query("SELECT suites,suitenos,maxnights,nightprice,deposit FROM gssettings WHERE id=1");
        $data = mysql_fetch_array($query);
        $maxnights = $data["maxnights"];
        $suites = $data["suites"];
        $suitenos = $data["suitenos"];
        $nightprice = $data["nightprice"];
        $deposit = $data["deposit"];
        $suite_numbers = split(",",$suitenos);
        $query = mysql_query("SELECT suiteno FROM gsdays WHERE date='$arrival'");
        $check = mysql_num_rows($query);

        if($check>=$suites)
        {
            $validate = 1;
            $error = "There are no suites available on $arrival";
        }

        while($data = mysql_fetch_array($query))
        {
            if ($a==1) break;
            for($i=0;$i<$suites;$i++)
            {
                $suiteno = $data["suiteno"];
                if ($a==1) break;
                if ($check==0)
                {
                    $use_suite = $suite_numbers[0];
                } else {
                    if ($suiteno != $suite_numbers[$i])
                    {
                        $use_suite = $suite_numbers[$i];
                        $a = 1;
                    } else {
                        $validate = 1;
                        $error .= "There are no suites available on $arrival";
                    }    
                }
            }
        }


        $departure = "$yearcheck-$monthcheck-$daycheck";
        $query = mysql_query("SELECT DATEDIFF('$departure','$arrival') AS datediff");
        $data = mysql_fetch_array($query);
        $datedifference = $data["datediff"];
        if ($datedifference > $maxnights)
        {
            $validate = 1;
            $error .= "<li>You are only able to reserve the guest suite for $maxnights days";
        }

        for($i=0;$i<$datedifference;$i++)
        {
            list($year,$month,$day) = split("-",$arrival);
            $this_date = date("Y-m-d",mktime(0,0,0,$month,$day+$i,$year));
            $query = mysql_query("SELECT * FROM gsdays WHERE date='$this_date'");
            $check = mysql_num_rows($query);
            if ($check >= $suites)
            {
                $validate = 1;
                $error .= "<li>There are no guest suites available on $this_date";
            }
        }

        if ($validate == 1)
        {
            echo $error;
            echo("<p><a href=\"guestsuite.php\" class=\"menu\">Click here to try again</a>");
        } else {

            $invoice = (($datedifference*$nightprice) + $deposit);
            $invoice_show = number_format((($datedifference*$nightprice) + $deposit),2);
            mysql_query("INSERT INTO gsdata (username,approved,suiteno,arrival,departure,invoice) VALUES ('$username',0,'$use_suite','$arrival','$departure','$invoice')");

            for($i=0;$i<$datedifference;$i++)
            {
                list($year,$month,$day) = split("-",$arrival);
                $this_date = date("Y-m-d",mktime(0,0,0,$month,$day+$i,$year));
                $query = mysql_query("SELECT id FROM gsdata ORDER BY id DESC LIMIT 1");
                $data = mysql_fetch_array($query);
                $dataid = $data["id"];
                mysql_query("INSERT INTO gsdays (suiteno,date,user,dataid) VALUES ('$use_suite','$this_date','$username','$dataid')");
            }

            echo<<<endhtml
                <b><center>Thank you for making your reservation</b><p>
                Your invoice total is $$invoice_show</center>
endhtml;
        }
    }
}

[/code]
The problem is $use_suite returns blank if there are no reservations, if there is one reservation it will use $suite_numbers[0]. Anyone see the problem?

Share this post


Link to post
Share on other sites
Your code is a bit messy and hard to read, it's taking a little bit to decipher it... give a moment!

Share this post


Link to post
Share on other sites
Perhaps if you described the data (tables and columns) and told us what you were trying to achieve it would save us a couple of hours trying to reverse-engineer a script that isn't even working and therefore only tells us what you don't want to do.

Share this post


Link to post
Share on other sites
It's alright, I figured it out. Problem was I was getting data from the while() loop... however it wouldnt' run if the query returned no results. And 90% of the time, it SHOULD return 0... silly error.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.