943,602 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 832
  • PHP RSS
Feb 27th, 2008
0

OPTION problem

Expand Post »
Hi

i m trying to get the values from database and put it into the select options..
i m using code-
PHP Syntax (Toggle Plain Text)
  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--
PHP Syntax (Toggle Plain Text)
  1. Customer auswidestonemanpnicholsonllwmplnewuserguestanftasturnbullanglic

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

why so..
plz help
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
abhi287 is offline Offline
28 posts
since Dec 2007
Feb 27th, 2008
0

Re: OPTION problem

echo your option tags inside of a select tag like this:

PHP Syntax (Toggle Plain Text)
  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>
Reputation Points: 14
Solved Threads: 14
Junior Poster in Training
johnsquibb is offline Offline
84 posts
since Nov 2007
Feb 27th, 2008
0

Re: OPTION problem

Well firstly
php Syntax (Toggle Plain Text)
  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;
>>>
php Syntax (Toggle Plain Text)
  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.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Feb 27th, 2008
0

Re: OPTION problem

Quote ...
I actually wasn't even aware PHP could use that format for while loops
Yeah.. I wasn't aware too..
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Feb 27th, 2008
0

Re: OPTION problem

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:

php Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Feb 28th, 2008
0

Re: OPTION problem

Hi all..
thanks for reply..

Problem is solved..
Last edited by abhi287; Feb 28th, 2008 at 12:29 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
abhi287 is offline Offline
28 posts
since Dec 2007
Feb 28th, 2008
0

Re: OPTION problem

try

PHP Syntax (Toggle Plain Text)
  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;
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Can't create sessions
Next Thread in PHP Forum Timeline: Displaying Data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC