Solved

Using More Query Results

Posted on 2000-03-15
9
198 Views
Last Modified: 2008-03-06
I have a drop-down box. How could I take the values from a database and have them inserted automatically into the option list for the drop-down box?

Thanks,

Dave
0
Comment
Question by:dht1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 

Expert Comment

by:mrvithan
ID: 2622312
Wow... Could you tell me how you put a text on option list. How you made a drop-down box. Use a DHTML or JScript? I think i need to know before i can give any suggestion.
0
 

Author Comment

by:dht1
ID: 2623246
I've pasted some of my code from the drop-down box:

            </td>
            <td><p align="left"><select name="Day" size="1">
                <option selected>01</option>
                <option>02</option>
                <option>03</option>
 
etc.... </SELECT>

Obviously I won't know how many options there will be...

Cheers,
Dave
0
 

Expert Comment

by:mrvithan
ID: 2623428
Wow...... it's not too hard to do...
For example... you have a table that keep all option you want....

<?php $result->query("Select * from TABLE"); ?>
..
..
..
<p align="left"><select name="Day" size="1">
<?php $rowitem = mysql_fetch_array($result)) ?>
<option selected><?php echo $rowitem['columnname'] ?></option>
<?php while($rowitem = mysql_fetch_array($result)) : ?>
<option> <?php echo $rowitem['columnname'] ?> </option>
<?php endwhile ?>


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dht1
ID: 2623904
I've pasted the code here. I've attempted to incorporate the code as above, but the result is an "empty" drop-down box...???

What could be wrong?

<div align="center"><center>
<table border="2" width="60%"
bordercolor="#000000" bordercolordark="#008080"
bordercolorlight="#000080">
<tr>
<td><p align="left"><font color="#000000" size="5">Facility
Required</font></p>
</td>
<td><p align="left"><font color="#000000">
<select name="Facility" size="1">

<?php $rowitem = mysql_fetch_array($result)) ?>

<option selected><? echo "$rowitem[name]"; ?>
</option>
<? $count = 0;
while($rowitem = mysql_fetch_array($result)) : ?>
<option value="<? print "$var[$count]"; ?>" <? echo "$var[$count]"; ?>
</option>
<? $var[$count] = $rowitem[name];
++ $count;
endwhile ?>
0
 

Expert Comment

by:mrvithan
ID: 2626085
Well.... I amn't quite sure what's wrong to the code... but let's me guess
1. actually, PHP engine start working when start with <?php not only <?
2. when you fetch a row to array, you refer it by using %rowitem['name'], it means you must have (') to cover the name of column.
3. your code doesn't make any sense...
 why.... 'cause $var (it support to be a array) get a value ($var[$count] = $rowitem[name];) after you print it print "$var[$count]"; ). what value does it use to print!!!!! Right.
 <it might be have some value before your print at somewhere above this code but your code doesn't show it>



0
 
LVL 2

Expert Comment

by:freshmeat
ID: 2626974
try to modify this part:
<? $count = 0;
while($rowitem = mysql_fetch_array($result)) : ?>
<option value="<? print "$var[$count]"; ?>" <? echo "$var[$count]"; ?>
</option>
<? $var[$count] = $rowitem[name];
++ $count;
endwhile ?>

TO:
<? $count = 0;
while($rowitem = mysql_fetch_array($result)) : ?>

<option value="<? print "$var[$count]"; ?>">
 <? echo "$var[$count]"; ?>
</option>
<? $var[$count] = $rowitem[name];
++ $count;
endwhile ?>

u missed the last ">" of ur "option" tag

hope it can work as u wish!
0
 

Author Comment

by:dht1
ID: 2627700
mrvithan,

1. If the filename = <filename>.php3 then <? ... ?> is acceptable - and yes, I do have a .php3 extension.

2. What I did not include was a bit of debugging code that I whacked in before the form:

while($rowitem = mysql_fetch_array($result));
$var[$count] = $rowitem[name];
print "$var[$count]<BR>";
++ count;

This works fine. All I need to do is get either $var[$count] or $rowitem[name] to be inserted on the option list... and I'll be damned if I can.


Freshmeat...it just didn't work.

Perhaps I'm going about this all the wrong way, but surely there IS a way of doing it... You're the experts :)
0
 

Accepted Solution

by:
dave5050 earned 200 total points
ID: 2631051
Try:

<select name=Day>
<?php
$res = mysql_db_query($db, $sql);
while(list($val)=@mysql_fetch_row($res)) {

 if($Day == $val) {
?>
<option SELECTED value="<?php echo $val;?>">
<?php } else { ?>
<option value="<?php echo $val;?>">
<?php echo $val;?>
<?php } } ?>
</select>


Obviously $db and $sql will contain appropriate values. Also if there is a variable called $Day in the global scope and it contains the value of one of the rows returned from the query, then that option will be selected in the drop list.
0
 

Author Comment

by:dht1
ID: 2631344
Thank you!!

I'm hungover... it took me a while to work out what $db and $sql should be, but I got there in the end  :)

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question