Jump to content


Photo

Complicated Logic


  • Please log in to reply
5 replies to this topic

#1 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 13 March 2006 - 04:34 PM

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

This is the script i'm using


    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;
        }
    }
}

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?

#2 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 13 March 2006 - 05:49 PM

bump.

#3 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 13 March 2006 - 06:39 PM

nobody wants to takea stap at this one?

#4 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 13 March 2006 - 06:43 PM

Your code is a bit messy and hard to read, it's taking a little bit to decipher it... give a moment!

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 13 March 2006 - 09:09 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 13 March 2006 - 09:24 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users