Solved

Using More Query Results

Posted on 2000-03-15
9
197 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
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

776 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