help with php/html/mysql

Discussion in 'PHP' started by programminh_test, Jul 6, 2014.

  1. #1
    hello.. does anyone know about dynamic dropdown menus?

    if i have a table with people names for example table "people"
    with columns name , surname , age...

    how can i create a dynamic dropdown menu with the ages of this table? i use php+html+mysql in the same project..thanks :)
     
    Solved! View solution.
    programminh_test, Jul 6, 2014 IP
  2. shemseddine

    shemseddine Active Member

    Messages:
    144
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    55
    #2
    Man! you got a long way to go if you're asking this question. I'm not going to give you the code to use it as I think it would be a waste of my time but I will say is, try reading a book on it first.

    Basically, the css or javascript is what you need to code you dropdown menu, and you use php to get the data from mysql and display it in this dropdown menu. Or you can just use the html select option instead of the css and javascript dropdown menu
     
    shemseddine, Jul 6, 2014 IP
  3. programminh_test

    programminh_test Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #3
    thanks for answering!! i have not to do that with javascript..only with html called by http.. my problem is that ive already make a code-part for the dynamic list in php


    $db = new mysqli("localhost", "root","" , "mycompany");
    if ($db->connect_errno) {
        echo "Failed to connect to MySQL: (" . $db->connect_errno . ") "
        . $db->connect_error;
    } else {
        $sql = "SELECT dept_name FROM department";
        $result_db = $db->query($sql);
        if (!$result_db) {
            echo $db->error . ' Error perform query!';
        } else {
            echo '<select name="dept_name">';
            echo '<option value="">Select...</option>';
            while ($row = $result_db->fetch_object()) {
                echo '<option value="' . $row->dept_name . '">';
                echo $row->dept_name;
                echo '</option>';
            }
            echo '</select>';
        }
    }
    PHP:
    but i have to change it so it will be html code called by php programme.. :/
     
    programminh_test, Jul 6, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    I don't understand this at all. Care to elaborate?
     
    nico_swd, Jul 6, 2014 IP
  5. programminh_test

    programminh_test Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    i dont know if i explained it very well.. i just want to have php code which call the html programme.. the programme "structure" will be in html and this will be nested in php programme..for example, i want to keep the code i wrote before but i want to be html nested in php
     
    programminh_test, Jul 6, 2014 IP
  6. shemseddine

    shemseddine Active Member

    Messages:
    144
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    55
    #6
    You still haven't explained it just right. From what I could gather, you might want to use ajax. Do you want it to automatically update the dropdown?
     
    shemseddine, Jul 6, 2014 IP
  7. programminh_test

    programminh_test Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #7
    yes sth just like that... and yes i want my list to be updaded automatically
     
    programminh_test, Jul 6, 2014 IP
  8. #8
    Well my friend, you will need to learn javascript and use ajax. It's not that bad if you get the hang of it
     
    shemseddine, Jul 6, 2014 IP
  9. programminh_test

    programminh_test Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #9
    thank you for your help.. i searched for some tutorials about ajax and javascript and i found sth and changed a little my code to this

    $html='<HTML><HEAD><TITLE>Question 3</TITLE></HEAD>';
        $html.='<BODY>';
    if ($db->connect_errno) {
        $html.='"Failed to connect to MySQL: (" . $db->connect_errno . ") "';
        $db->connect_error;
    } else {
        $sql = 'SELECT dept_name FROM department';
        $result_db = $db->query($sql);
        if (!$result_db) {
            $html.= $db->error . ' Error perform query!';
        } else {
            $html.='</BR><B><CENTER>Department:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select name="dept_name"></CENTER></B>';
            while ($row = $result_db->fetch_object()) {
                $html.='<option value="' . $row->dept_name .'">';
                $html.='<CENTER>'.$row->dept_name.'</CENTER>';
                $html.='</option>';
            }
            $html.='</select>';
            $html.='</BODY></HTML>';
        }
    }
        print $html;
    
    
    PHP:
    and i think it works.. but i hope it is what i wanted :p
     
    Last edited: Jul 6, 2014
    programminh_test, Jul 6, 2014 IP
  10. shemseddine

    shemseddine Active Member

    Messages:
    144
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    55
    #10
    Haha yeah good luck
     
    shemseddine, Jul 6, 2014 IP
  11. programminh_test

    programminh_test Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #11
    so, thats what i wanted to do? :O thank you so much :D
     
    programminh_test, Jul 6, 2014 IP