Re: Troubleshooting First Script

From: Date: Thu, 29 Jun 2000 15:59:19 +0000
Subject: Re: Troubleshooting First Script
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-3822@lists.php.net to get a copy of this message
At 12:46 PM 6/29/2000 +0100, you wrote:
Basically, your trouble is that you are trying to access variables are out of scope. In your functions you should have: function Find_a_Horse () {
    global $color, $gender, ......etc
I see you're right! However, I must have yet *another* problem in the script, because it *still* doesn't work. Furthermore, I'm being shot down right out of the gate! Not even the *else* default kicks in! So I'm really befuddled... I've included the code if anyone cares to try and help this newbie. The first function Main_Menu() asks the user to choose various values, thus setting variables. The second function Find_a_Horse() is called upon submission of the form. It's *supposed* to consult the table in the database named *horse* , call certain variables, compare them to the user's input and echo back the ones that meet the user's criteria. TIA, BenO <HTML> <HEAD>
        <TITLE>Find a Horse!</TITLE>
</HEAD> <BODY> <?
/* Script Name:    Antigo_search.php3 */
/* First let's set some variables. */ require ("/usr/www/thewebso/SSL/Common/common.req"); /* Establish our connection to the DB. */ mysql_pconnect("$hostname","$user","$password") or die("Unable to connect to SQL server"); mysql_select_db("thewebso") or die("Unable to select database"); /* Now to define functions. */ /* The main screen. Printed when $state is empty. */ /* Rem */ function Main_Menu () { global$colorz, $genderz, $agez; echo "<H1><CENTER> Find A Horse! </CENTER></H1>"; echo "Please fill out as much information as you'd like, then hit the &quot;Find a Horse!&quot; button.<br>&nbsp;<br>"; echo "<FORM METHOD = \"POST\" ACTION=\"Antigo_search.php3\">"; echo "Color<br>"; echo "<select name=\"colorz\">"; /* SHOULD I BE PUTTING *NAME* IN BOTH SELECT AND OPTION? */ echo "<option selected name=\"colorz\"
value=\"9\">select</option>";         /* This is simply to set a value so far out that it'll never be filled. */
echo "<option name=\"colorz\" value=\"3\">Palomino</option>"; echo "<option name=\"colorz\" value=\"1\">Chestnut</option>"; echo "<option name=\"colorz\" value=\"0\">Bay</option>"; echo "<option name=\"colorz\" value=\"4\">Golden</option>"; echo "<option name=\"colorz\" value=\"2\">Buckskin</option>"; echo "</select><br>"; echo "Gender<br>"; echo "<select name=\"genderz\">"; echo "<option selected name=\"genderz\" value=\"9\">select</option>"; echo "<option name=\"genderz\" value=\"0\">Mare</option>"; echo "<option name=\"genderz\" value=\"1\">Gelding</option>"; echo "<option name=\"genderz\" value=\"2\">Stallion</option>"; echo "</select><br>"; echo "Age<br>"; echo "<select name=\"agez\">"; echo "<option selected name=\"agez\" value=\"9\">select</option>"; echo "<option name=\"agez\" value=\"0\">Yearling</option>"; echo "<option name=\"agez\" value=\"1\">2-4 years</option>"; echo "<option name=\"agez\" value=\"2\">4-7 years</option>"; echo "<option name=\"agez\" value=\"3\">7+ years</option>"; echo "</select><br>&nbsp;<br>"; echo "<INPUT TYPE=\"submit\" name=\"state\" VALUE=\"FindHorse\">"; echo "</FORM>"; }; function Find_a_Horse () { global $color, $gender, $name; echo "<CENTER><H2>Here's Your Horses!</CENTER></H2><P>";
        if ($colorz != 9 && $genderz !=9 && $agez != 9) {  // *9* is default value
                $query = "select name, color, gender, birth from horse order by color";
/* CAN I ORDER BY MORE THAN ONE? */
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
/* DO I NEED TO SET VARIABLES? */
                $time = getdate (time());
/* DO I NEED TO DO *GETDATE* FOR EACH PIECE? */
                $dateyr = $time[year];  /* as YYYY */
                $datemo = $time[mon];           /* as MM (number) */
                $dateda = $time[mday];  /* as DD (number) */
                $birthyr = substr($row[birth],0,4);     /* calc year of birth */
                $birthmo = substr($row[birth],5,2);     /* calc mo. of birth */
                $birthda = substr($row[birth],8,2);     /* calc day of birth */
                $years = $dateyr - $birthyr;
                $months = $datemo - $birthmo;
                $days = $dateda - $birthda;
                if ($days < 0 && $months < 0) { /* still haven't reached birthdate this year */
                        $years = $years - 1;    /* calc how many years old this horse is */
                        }
                if ($years <= 2) {
                        $agezh = 0;
                        } elseif ($years <= 4) {
                        $agezh = 1;
                        } elseif ($years <= 7) {
                        $agez = 2;
                        } else {
                        $agezh = 3;
                        }
                                if ($row["color"] == $colorz && $row["gender"] == $genderz && $agez == $agezh) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($genderz !=9 && $agez != 9) {
                $query = "select name, gender, birth from horse order by gender";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
/* REMEMBER TO SET VARIABLES */
                $time = getdate (time());
/* DO I NEED TO DO *GETDATE* FOR EACH PIECE? */
                $dateyr = $time[year];  /* as YYYY */
                $datemo = $time[mon];           /* as MM (number) */
                $dateda = $time[mday];  /* as DD (number) */
                $birthyr = substr($row[birth],0,4);     /* calc year of birth */
                $birthmo = substr($row[birth],5,2);     /* calc mo. of birth */
                $birthda = substr($row[birth],8,2);     /* calc day of birth */
                $years = $dateyr - $birthyr;
                $months = $datemo - $birthmo;
                $days = $dateda - $birthda;
                if ($days < 0 && $months < 0) { /* still haven't reached birthdate this year */
                        $years = $years - 1;
                        }
                if ($years <= 2) {      /* calc how many years old this horse is */
                        $agezh = 0;
                        } elseif ($years <= 4) {
                        $agezh = 1;
                        } elseif ($years <= 7) {
                        $agez = 2;
                        } else {
                        $agezh = 3;
                        }
                                if ($row["gender"] == $genderz && $agez == $agezh) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($colorz != 9 && $agez != 9) {
                $query = "select name, color, birth from horse order by color";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
/* REMEMBER TO SET VARIABLES */
                $time = getdate (time());
/* DO I NEED TO DO *GETDATE* FOR EACH PIECE? */
                $dateyr = $time[year];  /* as YYYY */
                $datemo = $time[mon];           /* as MM (number) */
                $dateda = $time[mday];  /* as DD (number) */
                $birthyr = substr($row[birth],0,4);     /* calc year of birth */
                $birthmo = substr($row[birth],5,2);     /* calc mo. of birth */
                $birthda = substr($row[birth],8,2);     /* calc day of birth */
                $years = $dateyr - $birthyr;
                $months = $datemo - $birthmo;
                $days = $dateda - $birthda;
                if ($days < 0 && $months < 0) { /* still haven't reached birthdate this year */
                        $years = $years - 1;
                        }
                if ($years <= 2) {      /* calc how many years old this horse is */
                        $agezh = 0;
                        } elseif ($years <= 4) {
                        $agezh = 1;
                        } elseif ($years <= 7) {
                        $agez = 2;
                        } else {
                        $agezh = 3;
                        }
                                if ($row["color"] == $colorz && $agez == $agezh) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($colorz != 9 && $genderz !=9) {
                $query = "select name, color, gender from horse order by color";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
                                if ($row["color"] == $colorz && $row["gender"] == $genderz) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($agez != 9) {
                $query = "select name, birth from horse order by birth";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
/* REMEMBER TO SET VARIABLES */
                $time = getdate (time());
/* DO I NEED TO DO *GETDATE* FOR EACH PIECE? */
                $dateyr = $time[year];  /* as YYYY */
                $datemo = $time[mon];           /* as MM (number) */
                $dateda = $time[mday];  /* as DD (number) */
                $birthyr = substr($row[birth],0,4);     /* calc year of birth */
                $birthmo = substr($row[birth],5,2);     /* calc mo. of birth */
                $birthda = substr($row[birth],8,2);     /* calc day of birth */
                $years = $dateyr - $birthyr;
                $months = $datemo - $birthmo;
                $days = $dateda - $birthda;
                if ($days < 0 && $months < 0) { /* still haven't reached birthdate this year */
                        $years = $years - 1;
                        }
                if ($years <= 2) {      /* calc how many years old this horse is */
                        $agezh = 0;
                        } elseif ($years <= 4) {
                        $agezh = 1;
                        } elseif ($years <= 7) {
                        $agez = 2;
                        } else {
                        $agezh = 3;
                        }
                                if ($agez == $agezh) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($genderz !=9) {
                $query = "select name, gender from horse order by gender";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
                                if ($row["gender"] == $genderz) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        elseif ($colorz != 9) {
                $query = "select name, color from horse order by color";
                $result = mysql_query ($query)
                        or die ("Query failed");
                while ($row = mysql_fetch_row ($result))
                {
                        for ($i = 0; $i < mysql_num_fields ($result); $i++)
                        {
                                if ($row["color"] == $colorz) {
                                        echo "<a href=\"<? $name ?>.html\"><? $name ?></a><br>";
                                }
                        }
                }
        }
        else echo "We have over 70 horses! Please enter at least one criteria and click the &quot;Find a Horse!&quot; button.";
} /* The main loop. Call functions based on the value of $state, which gets set via a hidden INPUT TYPE. */ switch($state) { case "";
      Main_Menu();
break; case "FindHorse";
      Find_a_Horse();
      break;
} ?> </BODY> </HTML>

« previous php.general (#3822) next »