SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Form with PHP Variables

    I'm trying to write a PHP query with several variables controlled by a form. I got some help on the php forum, but I can't get it to work, largely because I'm new to forms.

    Let me first give you an overview. My query displays a list of animal names from a database table named gzanimals. I want to give visitors four options, as follows:

    1) Type of Name (e.g. scientific name, common name or common name with commas - from fields Name, NameCommon and NameCommonComma, respectively)

    2) Type of Animal (e.g. mammal, bird, etc. - from field MyCla, where "mam" = mammals, "bir" = birds, etc.)

    3) First Letter - For example, list only names beginning with A, B or C.

    4) Place - For example, list only animals native to Africa or the United States. This information will be in a different table, so I'll have to make a table join.

    Visitors will be required to choose from the first category - scientific or common names. But I want to design it so that visitors can choose from any combination of the other categories. If they make no choices at all, they'll get a list of names for the entire animal kingdom by default. Or they could choose from the last two categories, like animals whose names start with C and live in Africa. Or they could choose from all categories, like birds whose names start with A and live in South America.

    If I can get just one of these variables to work, then, hopefully, I can figure out the others. The script below doesn't include the form. It works, displaying only names of MAMMAL (MyCla = 'mam') SPECIES (Rank = '65') that begin with the letter A (NameCommonD LIKE 'A%').

    PHP Code:
    <?php
    $result 
    mysql_query('select count(*) from gzanimals');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {
     
    $taxon mysql_query ("SELECT * FROM gzanimals AS GZA
    WHERE MyCla = 'mam' AND Rank = '65' AND NameCommonD LIKE 'A%'"
    );
    echo 
    '<div class="' $_GET['$MyGet'] . '">';
    }
    ?>

    <?php
    echo '<table>';
    while (
    $row mysql_fetch_array($taxonMYSQL_ASSOC))

    {
    echo <<<EOD
       <tr>
         <td>
    {$row["NameCommonD"]}</td>
       </tr>
    EOD;
    }
    echo 
    '</table>';
    ?>
    Now I need to replace one element with a variable and connect it to a form. This next script is my attempt to convert taxonomic hierarchies or rank to a form variable:

    PHP Code:
    <?php
    $submit 
    $_POST['submit'];
    if(
    $submit){
    $Rank $_POST['Rank'];

    // $Rank = '35';
    // if type contains content then print out list
    $result mysql_query('select count(*) from gzanimals');
    if ((
    $result) && (mysql_result ($result 0) > 0)) {
    } else {
    die(
    'Invalid query: ' mysql_error());
    }
    {
     
    $taxon mysql_query ("SELECT * FROM gzanimals AS GZA
    WHERE MyCla = 'mam' AND Rank="' . $_POST['
    Rank'] . '"");
    echo 
    '<div class="' $_GET['$MyGet'] . '">';
    }
    ?>


    <?php
    // Requires sctips/tax1stacks
    echo '<table style="margin-left: auto; margin-right: auto;">';
    //<!-- BeginDynamicTable -->
    // while ($row = mysql_fetch_array ($taxon)) {

    while ($row mysql_fetch_array($taxonMYSQL_ASSOC))

    {
    $replace_what = array("_""?");
    $replace_with = array(" """);
    $commonName str_replace($replace_what$replace_with,  $row['NameCommonD']) ;

    echo <<<EOD
       <tr>
         <td style="padding: 0px 10px;">
    {$row["NameCommonD"]}</td>
       </tr>
    EOD;
    }
    echo 
    '</table>';
    else{
    ?>

    <form action="<?=$self?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
    <select name="Rank">
    <OPTION>Scientific</OPTION>
    <OPTION>Common Names</OPTION>
    <OPTION>Common Names with Commas</OPTION>
    </select>
    <input type="submit" name="submit" value="submit">
    </form>
    <? ?>
    Someone showed me how to convert the first letter to a variable at http://www.sitepoint.com/forums/show...=1#post2195581 but that doesn't work for me, either. I usually get several parse errors, and by the time I've fixed them I'm lucky if anything works at all.

    Can someone show me the correct script?

    Thanks.

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This looks like a PHP/MySQL question to me.

    How is this an X/HTML question?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz
    This looks like a PHP/MySQL question to me.

    How is this an X/HTML question?
    I thought my problem was mostly with the form, and I couldn't find a forms forum.

  4. #4
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How is it a question about the form?

    It looks to me like you just need to know how to construct MySQL queries for every combination.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz
    How is it a question about the form?

    It looks to me like you just need to know how to construct MySQL queries for every combination.
    There are hundreds of possible combinations - 26 letters X various animal groups X various taxonomic ranks X places. That's why I want to use a form instead.

  6. #6
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huh?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kravvitz
    Huh?
    In plain English, it should be easier to create a form than to write a PHP/MySQL script for every possible combination I'm dealing with.

  8. #8
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I seem to be missing your point.

    You wouldn't need to write a separate query for every combination anyway. You could use several "if()s" and maybe some "else if()s" to dynamically generate queries.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •