OPTION problem

Thread Solved

Join Date: Dec 2007
Posts: 28
Reputation: abhi287 is an unknown quantity at this point 
Solved Threads: 0
abhi287 abhi287 is offline Offline
Light Poster

OPTION problem

 
0
  #1
Feb 27th, 2008
Hi

i m trying to get the values from database and put it into the select options..
i m using code-
  1. <?php
  2. $con = mysql_connect("localhost","cdccpl","d123");
  3. if (!$con)
  4. {
  5. die('Could not connect: ' . mysql_error());
  6. }
  7. mysql_select_db("cdccpl_aus", $con);
  8. $result = mysql_query("SELECT * FROM bak_bill_cust");
  9. while( $row = mysql_fetch_array( $result ) ):
  10. $option = $row["cust_code"];
  11.  
  12. echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
  13. endwhile;
  14. mysql_close($con);?>

but i m getting displayes with the values as--
  1. Customer auswidestonemanpnicholsonllwmplnewuserguestanftasturnbullanglic

means values getting displayed one after another and not in option..

why so..
plz help
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 84
Reputation: johnsquibb is an unknown quantity at this point 
Solved Threads: 14
johnsquibb's Avatar
johnsquibb johnsquibb is offline Offline
Junior Poster in Training

Re: OPTION problem

 
0
  #2
Feb 27th, 2008
echo your option tags inside of a select tag like this:

  1. <select name="test" id="test">
  2. <option value="value 1">option 1</option>
  3. <option value="value 2">option 2</option>
  4. <option value="value 3">option 3</option>
  5. </select>
The End
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,429
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 231
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: OPTION problem

 
0
  #3
Feb 27th, 2008
Well firstly
  1. $result = mysql_query("SELECT * FROM bak_bill_cust");
  2. while( $row = mysql_fetch_array( $result ) ):
  3. $option = $row["cust_code"];
  4.  
  5. echo "<option value='$row[cust_code]'>$row[cust_code]</option>";
  6. endwhile;
>>>
  1. $result = mysql_query("SELECT * FROM bak_bill_cust");
  2. while( $row = mysql_fetch_assoc( $result ) )
  3. {
  4. $option = $row["cust_code"];
  5. echo "<option value='$option'>$option</option>";
  6. }

I actually wasn't even aware PHP could use that format for while loops but I wouldn't advise its use given that it completely breaks the Perl-like syntax and just makes for ugly code.
Last edited by ShawnCplus; Feb 27th, 2008 at 3:32 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,761
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: OPTION problem

 
0
  #4
Feb 27th, 2008
I actually wasn't even aware PHP could use that format for while loops
Yeah.. I wasn't aware too..
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: OPTION problem

 
0
  #5
Feb 27th, 2008
Not sure if your porblem is solved yet, but here is something I use all the time for drop downs.
First get your connection string going. preferably using an include.
then use the following code, adapting statement to your needs:

  1. <?php
  2. $result = mysql_query("SELECT * FROM users ORDER BY username ASC") or
  3. die(mysql_error());
  4. $sticky= '';
  5. if (isset($_POST['id']))
  6. $sticky = ($_POST['id']);
  7. $pulldown1 = '<select name="id">';
  8. $pulldown1 .= '<option></option>';
  9. while($row = mysql_fetch_array($result))
  10. {
  11. if($row['id'] == $sticky) {
  12. $pulldown1 .= "<option selected value=\"{$row['id']}\">
  13. {$row['username']}&nbsp;-&nbsp;
  14. {$row['id']}
  15. </option>\n";
  16. } else {
  17. $pulldown1 .= "<option value=\"{$row['id']}\"> {$row['username']}&nbsp;-&nbsp;
  18. {$row['id']}
  19. </option>\n";
  20. }
  21. }
  22. $pulldown1 .= '</select>';
  23. echo $pulldown1;
  24. ?>
This works well for holding the users selected values when error checking as well...
hope this helps
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 28
Reputation: abhi287 is an unknown quantity at this point 
Solved Threads: 0
abhi287 abhi287 is offline Offline
Light Poster

Re: OPTION problem

 
0
  #6
Feb 28th, 2008
Hi all..
thanks for reply..

Problem is solved..
Last edited by abhi287; Feb 28th, 2008 at 12:29 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: OPTION problem

 
0
  #7
Feb 28th, 2008
try

  1. $sql = "SELECT * FROM bak_bill_cust";
  2. $query = mysql_query($sql) or die('Error: ' . mysql_error());
  3. $select = '<select name="test">';
  4. $select .= '<option value=""></option>';
  5. while ($row = mysql_fetch_assoc($query)) {
  6. $select .= '<option value="' . $row['cust_code'] . '">' . $row['cust_code'] . '</option>';
  7. }
  8. $select .= '</select>';
  9. echo $select;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 687 | Replies: 6
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC