I am getting double of all my units in this code. any ideas?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rhdyes@gmail.com

    I am getting double of all my units in this code. any ideas?

    I work in a hospital and I am using php, postgres and apache to replace
    old paradox databases. I have made a form that contains a dropdown
    menu of the hospital units so I can choose the group of patient based
    on the unit they are on. For some reason my code has each unit
    appearing 2 times in the drop down so the dropdown looks like this

    unit1
    unit1
    unit2
    unit2

    and so on. I have tested the query in the pgadim query window and I
    get only one occurance. then I wrote the simple script:

    ?php
    pg_connect("hos t=localhost dbname=liberty user=postgres
    password=passwo rd") or die;
    $qunit = pg_query("SELEC T unitid, unitname FROM li_unit ORDER BY
    unitname");
    while($myunits= pg_fetch_array( $qunit)){
    $eachid=$myunit s["unitid"];
    $eachunit=$myun its["unitname"];
    echo "$eachid $eachunit<br>\n ";
    }
    ?>
    and I get the simple out put of

    1 unit1
    2 unit2
    3 unit3
    4 unit4

    Yet the below code gives me the double occurance of each unit. here is
    the code.
    <html>
    <head>
    <body>

    <form name="unitchoic e" method="post" action="<?php print
    $_SERVER['PHP_SELF']?>">
    <select name="homeunits ">
    <?php
    pg_connect("hos t=localhost dbname=liberty user=postgres
    password=passwo rd") or die;
    $qunit = pg_query("SELEC T unitid, unitname FROM li_unit ORDER BY
    unitname");
    while($myunits= pg_fetch_array( $qunit)){
    $eachid=$myunit s["unitid"];
    $eachunit=$myun its["unitname"]; ?>
    <option value=<?php echo $eachunit;?>><? php echo
    $eachunit;?></option>
    <?php
    }
    ?>
    </select>
    <input type="Submit" name="updateuni t" value="Change Unit">
    </form>

    </body>
    </head>
    </html>

    Any suggestions?

  • Rik

    #2
    Re: I am getting double of all my units in this code. any ideas?

    rhdyes@gmail.co m wrote:[color=blue]
    > $qunit = pg_query("SELEC T unitid, unitname FROM li_unit ORDER BY
    > unitname");
    > while($myunits= pg_fetch_array( $qunit)){
    > $eachid=$myunit s["unitid"];
    > $eachunit=$myun its["unitname"];
    > }
    > Any suggestions?[/color]

    What happens if you instead use pg_fetch_assoc( $qunit)?

    pg_fetch_array gives both a numerical and a associative array, maybe that's
    the issue. It should not give the error in this code, but maybe you ommited
    some seemingly insignificant code. I have no PostgreSQL-server, so I can't
    test it.

    Grtz,
    --
    Rik Wasmus


    Comment

    • rich

      #3
      Re: I am getting double of all my units in this code. any ideas?

      Still getting the same thing. There must be a bug. maybe it has
      something to do with it being part of a table?

      Comment

      • Rik

        #4
        Re: I am getting double of all my units in this code. any ideas?

        rich wrote:[color=blue]
        > Still getting the same thing. There must be a bug. maybe it has
        > something to do with it being part of a table?[/color]

        hmmmz, atih with pf_fetch_assoc you're sure there is only the associative
        array, so that would take care of doubles on that side (and uses less space,
        is about the same speed, so always a good idea).

        What is the exact reason you assign it to the variable $eachunit and
        $eachid? The only thing I can think of is that somehow, there is a second
        loop through the code.

        PHP doesn't care you echo it in a select box...... What does yout HTML
        output look like?

        What about trying:
        printf('option value="%s">%s</option>', $myunits['unitid'],
        $myunits['unitname']);

        Else, for debugging purposes:
        $check = array();
        while($myunits= pg_fetch_assoc( $qunit)){
        $check[] = $myunits;
        }
        print_r($check) ;

        What does that tell you?

        Grtz,
        --
        Rik Wasmus


        Comment

        Working...