Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old May 13th, 2006, 12:35 AM
rhdyes@gmail.com
Guest
 
Posts: n/a
Default 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("host=localhost dbname=liberty user=postgres
password=password") or die;
$qunit = pg_query("SELECT unitid, unitname FROM li_unit ORDER BY
unitname");
while($myunits=pg_fetch_array($qunit)){
$eachid=$myunits["unitid"];
$eachunit=$myunits["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="unitchoice" method="post" action="<?php print
$_SERVER['PHP_SELF']?>">
<select name="homeunits">
<?php
pg_connect("host=localhost dbname=liberty user=postgres
password=password") or die;
$qunit = pg_query("SELECT unitid, unitname FROM li_unit ORDER BY
unitname");
while($myunits=pg_fetch_array($qunit)){
$eachid=$myunits["unitid"];
$eachunit=$myunits["unitname"]; ?>
<option value=<?php echo $eachunit;?>><?php echo
$eachunit;?></option>
<?php
}
?>
</select>
<input type="Submit" name="updateunit" value="Change Unit">
</form>

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

Any suggestions?

  #2  
Old May 13th, 2006, 12:55 AM
Rik
Guest
 
Posts: n/a
Default Re: I am getting double of all my units in this code. any ideas?

rhdyes@gmail.com wrote:[color=blue]
> $qunit = pg_query("SELECT unitid, unitname FROM li_unit ORDER BY
> unitname");
> while($myunits=pg_fetch_array($qunit)){
> $eachid=$myunits["unitid"];
> $eachunit=$myunits["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


  #3  
Old May 13th, 2006, 02:15 AM
rich
Guest
 
Posts: n/a
Default 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?

  #4  
Old May 13th, 2006, 11:25 AM
Rik
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,338 network members.