Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2014
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    drop down menu from sql database not displaying correctly

    Hi guys,

    I am getting a drop down menu in php, its pulling the data but looks like its only displaying the first result. i have a feeling its in an array and i need to explode it or something?

    Then drop down only supplys the very first result. Any help would be greatly appreciated.

    The query:

    Code:
    $x="SELECT
    nfw_services.id_num,
    nfw_services.service_name
    FROM
    nfw_services
    WHERE
    nfw_services.user_id = '$UserID1'";
    $xx= mysql_query("$x") or die($myQuery."<br/><br/>".mysql_error());
    the form below is echo'd out. hence the slashes below near "

    Code:
     <option value=\"$service_name\">$service_name</option>
                                                </select>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    You need to fetch the values one by one from the result-set returned by the mysql_query(). Check the manual to get examples.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Apr 2014
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    are you saying list them like $service1, $service2 etc?

    If so that wont work, i need them dynamic as there are always things being added from the database. i would have to edit in more then one place doing that.

    unless you meant something else?

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,352
    Thanks
    61
    Thanked 528 Times in 515 Posts
    He means check the manual - as he said.

    The result set comes back as an object that you then fetch items from dynamically as part of a LOOP.

    EG:

    PHP Code:
    $Query "select * from mytable where user_active = 1";

    if (
    $Result mysql_query($Query))
       {
       while (
    $Row mysql_fetch_array($Result))
          {
          
    //Do something with the values in the $Row array here
          
    }
       } 
    You see that while() call? - That is a loop. Every item in the $Row array will have an index name that matches the column names in your database.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New Coder
    Join Date
    Apr 2014
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Ok so i got this to work, but i have one more issue

    "
    <?php
    $x="SELECT
    nfw_services.id_num,
    nfw_services.service_name
    FROM
    nfw_services
    WHERE
    nfw_services.user_id = '$UserID1'";
    $xx= mysql_query("$x") or die($myQuery."<br/><br/>".mysql_error());
    while(list($service_id,$service_name)= mysql_fetch_row($vv)){
    ?>
    <option value="<? echo $service_id ?>"><? echo $service_name ?></option>

    <? }}
    ?>
    I need it to go into an echo statement.

    An entire page is being echoed if then form needs to be returned because of a blank form field etc.

    i have tried puting a \ infront of " aboove. that fixes the errors but stops displaying the results.

    Example would be:

    <?php
    $x="SELECT
    nfw_services.id_num,
    nfw_services.service_name
    FROM
    nfw_services
    WHERE
    nfw_services.user_id = '$UserID1'";
    $xx= mysql_query("$x") or die($myQuery."<br/><br/>".mysql_error());
    while(list($service_id,$service_name)= mysql_fetch_row($vv)){


    echo "<div class='page-content'>

    <div class='row'>

    <select id='service_type' name='service_type' style='<?php echo $returnStyle2; ?>' class='form-control'><option id='service_type' name='service_type'></option>

    <option value=\"$service_id\">$service_name</option>

    }}

    </select></div></div>

    ";
    ?>
    hopefully you can see what i tried to do.

    I i enter it all outside of the echo in plain html it works fine as per below, but i need it to be inside an echo..

    "
    <?php
    $x="SELECT
    nfw_services.id_num,
    nfw_services.service_name
    FROM
    nfw_services
    WHERE
    nfw_services.user_id = '$UserID1'";
    $xx= mysql_query("$x") or die($myQuery."<br/><br/>".mysql_error());
    while(list($service_id,$service_name)= mysql_fetch_row($vv)){
    ?>
    <option value="<? echo $service_id ?>"><? echo $service_name ?></option>

    <? }}
    ?>
    Last edited by elgoots; 06-20-2014 at 04:52 AM.

  • #6
    New Coder
    Join Date
    Apr 2014
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts
    for anyone else wanting actual help on this.....

    i fixed it by doing this:

    PHP Code:
    <?php 
    $str 
    '';
    $x="SELECT
    nfw_services.id_num,
    nfw_services.service_name
    FROM
    nfw_services
    WHERE
    nfw_services.user_id = '$UserID1'"
    ;

    $xxmysql_query("$x") or die($myQuery."<br/><br/>".mysql_error()); 
          while(list(
    $service_id,$service_name)= mysql_fetch_row($vv)){
              
    $str .= "<option value='$service_id' > $service_name </option>";
        
    ?>
            <option value="<? echo $service_id ?>"><? echo $service_name ?></option>
           
        <? }} ?>

    then down the page on the select form where i was using echo, i put the $str :


    PHP Code:
    <?php

    echo "<select id='service_type' name='service_type' style='<?php echo $returnStyle2; ?>' class='form-control'>
    <option id='service_type' name='serovice_type'></option>
    $str
    </select>"

    ?>
    Last edited by elgoots; 06-21-2014 at 01:12 PM.


  •  

    Tags for this Thread

    Posting Permissions

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