Page 1 of 3 123 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Combo Box - Display results on Same Page


    Hi,

    I"m trying to -

    1. Populate a combo box using MySQL DB
    2. Display results on the same page as the combo box.

    I'm populating the combo box using MySQL DB.

    DB tables

    clothes - columns - id . categories (under categories - Men, Women) will add more later like kids.

    men - columns - id . product . info . category id

    women - columns - id . product . info . category id


    Code:
    <!-- This Code Populates The Combo Box--> 
    
    <form name='theform' method='post' action=''>
    <h3>Choose Gender</h3>
    <select name="clothes">
    <option value=0 selected>- Clothes For  -</option>
    <?php
    session_start();
    include('theconnection.php');
    
    $q = mysql_query("SELECT * FROM clothes"); 
    
    while ($row1 = mysql_fetch_array($q)){
      echo "<option value=$row1[id]>$row1[clothes]</option>";
      
    }
    ?>
    </select>
    <input type="submit" name="submit" value="Search">
    </form>
    
    
    <?php
    session_start();
    include('theconnection.php');
    
    
    $q = "SELECT clothes.id, clothes.categories, men.product , men.info, women.product , women.info\n".
         "FROM clothes, men, women\n".
         "WHERE clothes.id=category_id\n".
         "ORDER BY clothes.id LIMIT 0, 30 ";
        
    	 
    $results = mysql_query($q) or die(mysql_error());	
    
    // Print out the contents of each row
    
    while($row = mysql_fetch_array($results)){
    	echo $row['prod']. " - ". "<a href='$row[info]' target='_blank'>View</a>";
    	echo "<br />";
    }
    
    ?>

    I've tried and tried to display only Men's results when selected and the same for Women.
    But PHP and the DB doesn't know which one I'm selecting to display the results on the same page.

    Can someone show me how to do this? I'm trying to learn PHP and MySQL so step by step with all the details would be wonderful.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    If you want to keep it basic and stick with PHP, then you will need to add, I believe, onchange to the box and have it submit the new selection as a form choice and load as desired upon page reload.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    First off, it would be best to avoid using the mysql_connect/mysql_query set of functions entirely and just learning to use PDO. It's not much harder and way more secure.


    Second, you need to understand that what PHP is doing is creating a web page to send to the end user. By the time the user has the file in his browser, the PHP script has exited. Basically it's like this:

    1. User's browser makes a request to URL
    2. PHP script at that URL processes the request and returns an HTML file
    3. HTML file is sent to User's browser.

    So, if you want to have a form that the user can interact with on the same page:

    - Your form needs to be configured to send it's request to the same URL (via the "action" parameter)
    - Your script needs to check for the presence of either $_GET or $_POST data in the request. Which one it is depends on what you specify in the "method" parameter in your form tag. GET is default.
    - If there *is* form data in the request, act on it in some way.
    - If there isn't, this is probably the initial page load or a refresh, so display whatever the default is.

    A common pattern here is like this:
    PHP Code:

    <form action="<?=$_SERVER['PHP_SELF']?>" method='POST'>
    Enter data: <input name=foo />
    <input type=submit>
    </form>

    <?php

    if ($_POST){
    $foo = isset($_POST['foo']) ? $_POST['foo'] : Null;
    // Some logic involving $foo ...
    }
    else{
    // Whatever you display by default, without any info from the user
    echo "<p>Enter a value to get results!</p>";
    }

    ?>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Thank you admore . I think I understand what you're saying.
    One issue not sure what to add for foo and everything else
    In the if and else. I'm learning but terrible at if and else.
    Can you take my code variables etc and show me what needs
    to be done? If I see the actual code then I can go through it
    and understand. I've been working on this for 3 days
    and unable to get this to work. Thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by gloriab
    Thank you admore . I think I understand what you're saying.
    One issue not sure what to add for foo and everything else
    In the if and else. I'm learning but terrible at if and else.
    Can you take my code variables etc and show me what needs
    to be done? If I see the actual code then I can go through it
    and understand. I've been working on this for 3 days
    and unable to get this to work. Thanks
    No offense, but I'm not going to write your program for you. I could give you some code to copypaste but it'd be a waste of my time and you'd be no better a programmer for it.

    If you're struggling with the concept of if/else, then I'd suggest you're taking on too much too soon and need to go back and get solid on the fundamentals. Check out codeacademy.com; it's a free site with a good interactive PHP course that will step you through the basics.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Thanks admore. Not a problem. I know how I learn and I'm sure you know how you learn but I won't tell you how to learn differently. If I didn't already go through these things, I wouldn't be here asking for help. With that said can someone please take over and explain in more detail instead of preaching the norm? Thanks
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Simple if(), if/else, and an if/elseif/else for the sake of laying all 3 out. You never need more than the if, unless you wish to run it this way or that way. The 'elseif' also does NOT require the final 'else'.

    PHP Code:
    <?php
      
    if($a == "1") {
        echo 
    "Your answer is 1";
      }
      
      if(
    $a == "1") {
        echo 
    "Your answer is 1";
      } else {
        echo 
    "Your answer is NOT 1";
      }
      
      if(
    $a == "1") {
        echo 
    "Your answer is 1";
      } elseif(
    $a == "2") {
        echo 
    "Your answer is 2";
      } else {
        echo 
    "Your answer is neither 1 nor 2";
      }
    ?>
    Shorthand, using the ternary operator, is also a very nice option to write such in...

    PHP Code:
    $foo = (isset($_POST['foo'])) ? $_POST['foo'] : Null
    Is equal to...
    PHP Code:
    if(isset($_POST['foo'])) {
      
    $foo $_POST['foo'];
    } else {
      
    $foo Null;

    Pretty much same stages...
    PHP Code:
    $var = ($a == "1") ? true false
    Last edited by Triple_Nothing; August 5th, 2013 at 12:39 PM.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2010
    Posts
    153
    Rep Power
    5
    Originally Posted by gloriab
    Thanks admore. Not a problem. I know how I learn and I'm sure you know how you learn but I won't tell you how to learn differently. If I didn't already go through these things, I wouldn't be here asking for help. With that said can someone please take over and explain in more detail instead of preaching the norm? Thanks
    Again I did not intend any offense, but if you say you are struggling with basic concepts, I do not consider my teaching abilities to be so great that I will be able to explain it to you better than a site dedicated to teaching core programming concepts.

    I am not "preaching the norm", merely offering the best suggestion I can offer based on what I can ascertain about your skills and knowledge from your own words.

    Best of luck to you.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Thanks Triple. So looking at my code that was posted.
    and using this -
    $foo*=*(isset($_POST['foo']))*?*$_POST['foo']*:*Null;

    Replacing foo with clothes? I'm trying to understand this and then how if women is selected how will that section know I selected women and display the results. That's the big question for me. Thanks for your help.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    The whole "knowing" if something was selected is the entire purpose of isset(). When nothing is selected, such as during the initial page load, then you need to define something as an 'else', since isset() will be false, or a default item in your SELECT.

    As far as defining what you want in your SELECT query after an item is selected, that will just be defined after the WHERE clause.

    The selection can define a variable like $type. During initial page load, and when nothing is selected, then $type will equal default as you wish. If something is selected, $type will be define by setting it to something like:
    PHP Code:
    $type $_GET['clothes']; 
    So, a full, but shorthand version may be:
    PHP Code:
    $type = (isset($_POST['clothes'])) ? $_POST['clothes'] : 1
    The 1 will kinda say anything. It's more of a if true, or as long as there's something there, select it. Even columns in databases that appear empty, or to hold not true value, doesn't mean it doesn't exist.

    EDIT: Do keep in mind, and remember when you write your code in the future... $var = 1; and $var = "1"; are NOT the same... They may work in instances, but just keep in mind them as not an option.
    Last edited by Triple_Nothing; August 5th, 2013 at 04:11 PM.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Thank you Triple for explaining. I'm listing all items in the combo box but still unable to display results.

    Can someone look at my code and tell me why?



    Code:
    <form id="form1" name="form1" action="<?=$_SERVER['PHP_SELF']?>" method='POST'>
    <h3>Choose Gender</h3>
    <select name="clothes">
    <option value=0 selected>- Clothes For  -</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="<? echo $row_list['id']; ?>" <? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['clothes']; ?> 
    </option>
    
    <?php
    // End while loop.
    }
    ?>
    
    </select>
    <input type="submit" name="Submit" value="Select" />
    </form>
    <hr>
    <p>
    <?php
    // If you have selected from the combo box.
    if(isset($select)&&$select!=""){
    
    // Get records from database
    $results=mysql_query("SELECT clothes.id, clothes.categories, men.prod, men.info");
    while($row = mysql_fetch_array($results)){
    	echo $row['prod']. " - ". "<a href='$row[info]' target='_blank'>View</a>";
    	echo "<br />";
    
    ?> 
    // Repeat for testing purposes
    <strong><? echo $row['prod']; ?> <? echo $row['info']; ?></strong> </p>
    
    <?php
    // End While loop
    }
    // End if statement.
    }
    ?>
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    I, as many other will, am gonna say to drop the mysql_ during your training. If you're gonna start out, start out right. Move to MySQLi or PDO. PDO is best because it works across many database types, while MySQLi is just MySQL. My example here is MySQLi.
    PHP Code:
    function buildCompany($selectedID) {
      global 
    $link;
      
    $statement $link->prepare("SELECT `ID`,`Customer Name` FROM `customers` ORDER BY `Customer Name`");
      
    $statement->execute();
      
    $statement->bind_result($col1,$col2);
      while (
    $statement->fetch()) {
        
    $selected = ($selectedID == $col1 " selected" "");
        echo 
    '<OPTION value="' $col1 '"' $selected '>' $col2 "</OPTION>\n";
      }
      
    $statement->close();

    From beginning to end...
    $selectedID - An id to define the selected value of the drop-down, so it remains selected after reload. Make sure to set a default vault if such is not set.
    $link - The initial login/connection to the database.
    prepare - Set up your query.
    execute - Execute...
    bind_result - My SELECT is grabbing 2 items, ID and Customer Name, thus I need 2 variables defined, named as you wish.
    fetch - Grabs a row. Running within the while will continue until all rows are fetched.
    $selected - Defines if this will hold ' selected' a value, or remain blank.
    echo - Prints the option with the variables set to the items on the current row from the database.
    close - Closes connection to database and keeps the process clean.

    EDIT: Try not to use <?, but use full <?php instead. And as far as you HTML n PHP, try not to open/close as much as you do.
    Last edited by Triple_Nothing; August 6th, 2013 at 10:45 AM.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Hi Triple,

    Populating the dropdown isn't the issue that's the easy part. Displaying the results after a selection is made is the issue.

    I have to stick with mysql sorry. And I'm not in training just trying
    to find out how to display the results I want. Not looking to be a php
    Programmer.



    Open and close as much as I do? Examples?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Well, as far as building the listing... It's pretty much identical to building the drop-down. You're just adding another option/row each loop.

    As far as the open/close. Your while loop...
    PHP Code:
    while ($row_list=mysql_fetch_assoc($list)){    
    ?>

    <option value="<? echo $row_list['id']; ?><? if($row_list['id']==$select){ echo "selected"; } ?>><? echo $row_list['clothes']; ?> 
    </option>

    <?php
    // End while loop.
    }
    Can be...
    PHP Code:
    while($row_list=mysql_fetch_assoc($list)) {
      
    $selected = ($row_list['id'] == $select) ? "selected" Null;
      echo 
    "<option value=\"$row_list['id']\" $selected>$row_list['clothes']</option>";

  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    676
    Rep Power
    7
    Sorry for late post, if this is still an issue. I noticed you aiming something like just mens clothes, and saw multiple tables. Is it your desire for the seperate tables? If not, you can keep a single table, and provide a 'type' column to define if it's men's/women's or such. If you do wish for multiple tables, you can use the drop-down to define which table. Meerly make the 'value' of the option in your drop-down equal to the table's name, then in your query do something like:

    Code:
    SELECT ... FROM $dropdown WHERE ...
Page 1 of 3 123 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo