#16
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Yes this is still a problem and yes I'm using 3 tables which I figured out that is the issue. and not knowing what to add to the sql statement. I'm not sure where and how to reference $dropdown. and what to add to the sql statement.

    Here are my tables

    clothes - columns - id . categories .category id
    men - columns - id . product . info . category id
    women - columns - id . product . info . category id

    Is this sql statement correct?
    SELECT clothes FROM $dropdown WHERE category id

    How will $dropdown be referenced? Not working yet.
  2. #17
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    875
    Rep Power
    275
    Is everything in your Men table clothing related? same for Women table? If so, I see no reason for you to even have the Clothes table or any need to reference it in your dropdown or sql statements.

    Just setup your dropdown form and set the value for men's category to "men" and the value for women's category to "women". After the customer clicks the Submit button, setup your sql statement according to which value was selected and output that Table's contents accordingly.

    And, I don't think anyone else has mentioned this, but, you only need the session_start(); and your database connection include(); statement written once per file, not several times in one file.

    EDIT:
    Jacques1 is going to scold me something fierce for even suggesting THIS LINK because of all of the security implications that he posted HERE, but, it seems to me that the example posted there is very similar to what you are wanting to accomplish.
    Not that I suggest using that code AS IS, but, it might help you gain some insight as to what you need to do to accomplish your goal.
    Last edited by DonR; August 8th, 2013 at 03:18 PM.
  4. #18
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    The code has changed since then. I removed the duplicate session and include a while back.

    I need the clothes table to print out men and women in the select box - and it's doing that just fine.... I don't need help on the select box... I need help on printing the results after the form has been submitted.

    The other tables men and women will have many rows. If I add say, "Men" to the men table and print Men in the select box from the Men table, it will add a lot Men to the box correct?

    Since my code has changed, can anyone see why this doesn't work? Can someone see one thing that is causing it not to print the results?

    I know someone can look at my code and say this is the problem... rather than saying, "hey you should be using this or that". Someone can see the issue .. Are you out there? If so, please look at the code below and tell me what the problem is. Thank you in advance.


    Code:
    <form id="form1" name="form1" method="POST" action="<?php echo $PHP_SELF; ?> ">
    <strong>Gender:</strong>
    <select name="category_id">
    <option value="">--- Please Select Gender ---</option>
    <?php
    
    include('connection.php');
    
    // Get records from database (table "name_list").
    $list=mysql_query("SELECT * FROM clothes");
    // Show records by while loop.
    while($row_list=mysql_fetch_assoc($list)){
    ?>
    <option value="<?php echo $row_list['category_id']; ?>" <?php if($row_list['categories']==$select){ echo "selected"; } ?>><?php echo $row_list['categories']; ?></option>
    
    
    <?php
    // End while loop.
    }
    ?>
    </select> 
    <input type="submit" name="select" value="Select" />
    
    <br />
    <p>
    
    
    <?php
    if(isset($_POST['category_id'])){
    	$string = "SELECT clothes.categories, men.prod, men.info FROM clothes INNER JOIN men ON clothes.category_id = men.category_id WHERE men.category_id='$_POST[category_id]'";
    	
    	
    	
        $sql_string = mysql_query($string);
        while($row = mysql_fetch_array($sql_string)){
          
            echo $row['prod'];
            echo $row['info'];
            echo $row['categories'];
            echo $row['category_id'];
        }
    }  
    
    ?>
  6. #19
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    This is to be very simple for you. As far as building the table, it is almost IDENTICAL to building the drop-down. You are selecting items from a DB table, and instead of echo-ing an OPTION for each while, it would just be another HTML table row.

    This will do 1 if defined, or both if not, but table will be build listing all men's first, then all women's:
    PHP Code:
    if(isset($_POST['category_id'])) {
      
    $list=mysql_query("SELECT * FROM " $_POST['category_id'] . ";");
      while(
    $row_list=mysql_fetch_assoc($list)){
        echo 
    "<TR><TD>" $row_list['name'] . "</TD><TD>" $row_list['size'] . "</TD><TD>" $row_list['price'] . "</TD></TR>\n";
      }
    } else {
      
    $list=mysql_query("SELECT * FROM mens UNION SELECT * FROM womens;");
      while(
    $row_list=mysql_fetch_assoc($list)){
        echo 
    "<TR><TD>" $row_list['name'] . "</TD><TD>" $row_list['size'] . "</TD><TD>" $row_list['price'] . "</TD></TR>\n";
      }

    EDIT: If you wish to organize by anything, then use ORDER BY. It's best in your UNION query to then wrap each SELECT in parentheses to keep things cleen:
    Code:
    (SELECT * FROM mens) UNION (SELECT * FROM womens) ORDER BY `size`;
    And try not to use the * when selecting, but actual column names...
    Last edited by Triple_Nothing; August 10th, 2013 at 12:53 AM.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    I tried what you suggested but no results.

    Here's my code - See any thing wrong?

    Code:
    <form id="form1" name="form1" method="POST" action="<?php echo $PHP_SELF; ?> ">
    <select name="select">
    <option value="">--- Select Gender ---</option>
    <?php
    
    include('connection.php');
    
    // Get records from database.
    $list=mysql_query("SELECT * FROM clothes");
    // Show records by while loop.
    while($row_list=mysql_fetch_assoc($list)){
    ?>
    <option value="<?php echo $row_list['category_id']; ?>" <?php if($row_list['categories']==$select){ echo "selected"; } ?>><?php echo $row_list['categories']; ?></option>
    
    
    <?php
    // End while loop.
    }
    ?>
    </select> 
    
    <input type="submit" name="select" value="Select" />
    
    <?php
    
    if(isset($_POST['category_id'])) {
      $list=mysql_query("SELECT * FROM clothes" . $_POST['category_id'] . ";");
      while($row_list=mysql_fetch_assoc($list)){
        echo "<TR><TD>" . $list['clothes_id'] . "</TD><TD>" . $list['prod'] . "</TD><TD>" . $list['info'] . "</TD></TR>\n";
      }
    } else {
      $list=mysql_query("SELECT * FROM men UNION SELECT * FROM women;");
      while($row_list=mysql_fetch_assoc($list)){
        echo "<TR><TD>" . $list['category_id'] . "</TD><TD>" . $list['prod'] . "</TD><TD>" . $list['info'] . "</TD></TR>\n";
      }
    }  
    
    ?>
    This issue looks to be it's not communicating with the men and women tables. it's prints the tr and td but not what's in the prod and info columns.

    THE TABLES -

    clothes

    category_id -- categories -- related_id

    men

    men_id -- prod -- info -- related_id

    women

    women_id -- prod -- info -- related_id
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Ok. The 2 items, unless this was intentional for testing, in your two queries are aiming for different targets. If the isset() is true, it is still selecting from a men or women table, but defines just 1. You are missing a space after the word clothes in:
    PHP Code:
    $list=mysql_query("SELECT * FROM clothes" $_POST['category_id'] . ";"); 
    In your while() loops, I did write something wrong. Since it's while($row_list..., you need to switch the $list[' items in your echo to $row_list. Sorry. I corrected my example.
    Last edited by Triple_Nothing; August 10th, 2013 at 12:55 AM.
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    I made the changes but it displays when refreshing the page rather than when a selection is made to the select box.




    PHP Code:

    <form id="form1" name="form1" method="POST" action="<?php echo $PHP_SELF?> ">
    <strong>Brand:</strong>
    <select name="select">
    <option value="0">--- Please Select Brand ---</option>
    <?php

    include('connection.php');

    // Get records from database (table "name_list").
    $list=mysql_query("SELECT * FROM clothes");
    // Show records by while loop.
    while($row_list=mysql_fetch_assoc($list)){
    ?>
    <option value="<?php echo $row_list['category_id']; ?><?php if($row_list['categories']==$select){ echo "selected"; } ?>><?php echo $row_list['categories']; ?></option>


    <?php
    // End while loop.
    }
    ?>
    </select> 
    <input type="submit" name="select" value="Select" />

    <?php

    if(isset($_POST['category_id'])) {
      
    $list=mysql_query("SELECT * FROM clothes " $_POST['category_id'] . ";");
      while(
    $row_list=mysql_fetch_assoc($list)){
        echo 
    "<TR><TD>" $row_list['category_id'] . "</TD><TD>" $row_list['prod'] . "</TD><TD>" $row_list['info'] . "</TD></TR>\n";
      }
    } else {
        
      
    $list=mysql_query("SELECT * FROM men UNION SELECT * FROM women;");
      while(
    $row_list=mysql_fetch_assoc($list)){
        echo 
    "<TR><TD>" $row_list['category_id'] . "</TD><TD>" $row_list['prod'] . "</TD><TD>" $row_list['info'] . "</TD></TR>\n";
      }
    }  


    ?>



    defines just 1 - do you mean it needs an elseif? Not sure what you're saying here.
  14. #23
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    PHP Code:
    if(isset($_POST['category_id'])) {
      
    // Code to run if a selection WAS made
    } else {
      
    // Code to run otherwise...

    You're saying it DOES display as desired after a page refresh, but not after making a selection from the drop-down, then clicking Submit?
    What is in your 'value' field for your drop-down items? The value should hold either 'men' or 'women'.
    Since you are building your drop-down from a table, I am hoping you do have more options than just men or women.
    Can you define what options will all be in the drop-down?
    Last edited by Triple_Nothing; August 10th, 2013 at 01:53 AM.
  16. #24
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by Triple_Nothing

    You're saying it DOES display as desired after a page refresh, but not after making a selection from the drop-down, then clicking Submit?
    What is in your 'value' field for your drop-down items? The value should hold either 'men' or 'women'.
    Since you are building your drop-down from a table, I am hoping you do have more options than just men or women.
    Can you define what options will all be in the drop-down?

    I'm saying it is displaying both men and women but should not be displaying until after a selection is made. Also after a selection is made, it should only display men or women information depending which gender is selected.


    What is your 'value' - I changed it to men and women. I had category_id but changed it to categories but still the same issues after that was changed.


    Since you are building your drop-down from a table, I am hoping you do have more options than just men or women.
    Can you define what options will all be in the drop-down?
    Yes

    men
    women
    kids
    shirts
    pants
    cool
    warm
    hot
    dry
    new tech
    cotton
    silk
  18. #25
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Ok. So you want nothing to be listed AT ALL until after a selection from the drop-down? Then the 'else' half can be edited to something you wish to define by default, or even removed entirely.
    When you make a selection, and submit, the page reloads to offer the same view?
    Since it won't build a table of just men, is it building a table at all? Within your if() statement, before your SELECT line, create a line to echo your SELECT. Then use this in phpMyAdmin or such to manually run the string and see what data/error is returned.
  20. #26
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Yes nothing should be listed until a selection has been made - after selecting Men or Women.

    I removed the else and nothing displays which is what I want.

    When I select Men or Women - I get both Men and Women results. So it doesn't know the difference between Men and Women.


    Ran "SELECT * FROM men UNION SELECT * FROM women "in phpMyAdmin. I get as expected all info from men and women.

    Which is what the select is saying to do-

    SELECT * FROM men UNION SELECT * FROM women
    ---------------------------------------------------------------------------------------------------

    Doesn't seem like we're going anywhere with this. I don't understand why this is so tough. I'm not getting it..

    This is easy -

    1. Select box gets populated by the Clothes table. Right now it populates with Men and Women.
    2. Select an option from the select box - Men or Women (will have more later)
    3. There is a table for Men and for Women with info for both.
    3. Display results for Men "OR" Women - This depends which one you select (Men "OR" Women). SHOULD NOT DISPLAY INFO FOR BOTH MEN "AND" WOMEN.
    AGAIN HERE ARE TABLES IN AN ORGANIZED MANNER:

    THE TABLES -

    clothes

    category_id -- categories -- related_id

    men

    men_id -- prod -- info -- related_id

    women

    women_id -- prod -- info -- related_id
  22. #27
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Are you wishing this to become displayed as your choice is made? Your current script is to make a choice from the drop-down, press Submit, page reloads, and SELECT runs. Upon page reload after a selection was made from your drop-down, did your SELECT query build correctly when you echo-ed it to your screen for your viewing? So it then reads on the screen 'SELECT * FROM men' and still does not work?
  24. #28
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Are you wishing this to become displayed as your choice is made? NO

    Your current script is to make a choice from the drop-down, press Submit, page reloads, and SELECT runs. Upon page reload after a selection was made from your drop-down - YES THIS IS HOW IT"S SUPPOSE TO WORK.

    Did your SELECT query build correctly when you echo-ed it to your screen for your viewing? NO

    No matter which option I selected "Men OR Women" the results were both "Men and Women". NOT one of the two.

    So it then reads on the screen 'SELECT * FROM men' and still does not work? YES IT WORKS But I can select option 0 and it will still run the script and display Men. I can select women and it displays Men.
  26. #29
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    I don't suppose you can post the HTML outcome of your script, which you receive from a browser upon viewing this page?
  28. #30
  29. D'oh!
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    East Coast
    Posts
    75
    Rep Power
    73
    This is just off the top of my head - meaning it hasn't been checked for typos or anything - but it should give you a better starting point.

    A few notes:
    1. Always write your conditionals as blocks and *never* as statements. Blocks make your code easier to read, easier to expand on, easier to set breakpoints in and, frankly, statement-based conditionals look like you just smashed your head on your keyboard. This isn't C, you have the room to make it look readable.

    2. Try to keep the separation of church and state (output and processing, php and html). Code looks like crap when you mix languages. That may not sound like such a big deal until you're trying to troubleshoot a single function that's spread out over 500 lines with breakouts to html every other line. Build your output, then output it all at once and you'll save yourself a lot of headaches.

    3. Don't ever, ever use "id" as a field name. If this becomes habit and you progress to the point where you're working against some old, expensive corporate database system, you're likely to start breaking things without knowing why. 'id' is or has been a reserved word in a ton of different database systems.

    4. HTML tags are always lowercase.

    PHP Code:
    <?php
    // I'm assuming this sets up your db connection
    require_once('connection.php');

    // we'll need this later
    $clothing_list '';

    // since you have options between men and women, we're going to see if that
    // option has been submitted
    if( isset($_POST['itemtype']) && intval($_POST['itemtype']) > ){
        
    // we passed the category id, not the name, in the interest of speed and security
        
    $itemtype intval($_POST['itemtype']);
        
        
    // so we have to translate that into a name bc your tables don't use type as an id
        // which is something you should fix - you should pass numbers wherever you can
        
    $sql "SELECT * FROM clothes WHERE category_id='$itemtype'";
        
    $itemdetail mysql_fetch_assoc(mysql_query($sql));
        
    $cat_name strtolower($itemdetail['categories']);
        
        
    // set the table based on itemtype
        // we don't just $dbtable = $itemtype here bc we're not sure
        // of what $itemtype might contain
        
    switch($cat_name){
            
    // different case for each possible value
            
    case 'women':
            
    $dbtable 'women';
            break;

            case 
    'men':
            
    $dbtable 'men';
            break;        
        } 
    // switch($itemtype)
        
        
    $sql 'SELECT * FROM '.$dbtable;
        
        
    // grab the db data and put into an array
        
    $dataset = array();

        
    $result mysql_query($sql);
        while( 
    $row mysql_fetch_assoc($result) ){
            
    // don't use just 'id' for a field name in mysql, some versions will break
            
    $the_id $row['product_id'];
            
    $dataset[$the_id] = $row;
        } 
    // while
        
        // $dataset now contains all the clothing data for this type
        // you can test with
        // print_r($dataset);
        
        // let's build this table while we're here
        
    $clothing_list '<table>';
        foreach( 
    $dataset as $item ){
            
    $clothing_list .= '<tr><td>'.$item['prod'].' - <a href="'.$item['info'].'" target="_blank">View</a></td></tr>';
        }
        
    $clothing_list .= '</table>';
    // if( isset($_GET['itemtype']) )

    // grab your "brand" options - may as well build this select here too
    $clothes_opts '<select name="itemtype">'
    .'<option name="itemtype" value="0">--- Please Select Brand ---</option>';

    $sql 'SELECT * FROM clothes';
    $result mysql_query($sql);
    while( 
    $row mysql_fetch_assoc($result) ){
        
    $the_id $row['category_id'];
        
    $clothes_opts .= '<option name="itemtype" value="'.$the_id.'"';
        if( isset(
    $_POST['clothes_opt']) && intval($_POST['clothes_opt']) == $the_id ){
            
    $clothes_opts .= ' selected';
        }
        
    $clothes_opts .= '>'.$row['categories'].'</option>';
    // while
    $clothes_opts .= '</select>';

    // since we're still parsing data, we assign to variables
    // rather than do direct output (mainly bc it looks cleaner)
    $output '<form id="form1" name="form1" method="POST" action="'.$_SERVER['PHP_SELF'].'">'
    .'<strong>Brand:</strong> '.$clothes_opts.'<input type="submit" name="select" value="Select" />'
    .'</form><br><br>';

    // output the form and table
    // if the user didn't submit the form, $clothing_list is empty (yet defined)
    echo $output.$clothing_list;
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo