Connecting Tech Pros Worldwide Help | Site Map

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

  #1  
Old May 13th, 2006, 12:35 AM
rhdyes@gmail.com
Guest
 
Posts: n/a
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

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

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

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


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 14th, 2005 04:15 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 11:37 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 09:56 PM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit answers 0 November 13th, 2005 03:15 AM