473,466 Members | 1,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Drop Down Box display the first element of an array twice from an MySQL query...

5 New Member
Hi Guys

I am creating a sports database and one of the features involves create drop down dialogue boxes that retrieve data from MySQL tables and provides them as <option> within <select></select>. I am using PHP for this and I'm fairly new at the programming language. I have got the dialogue to work with respect to retrieving the data from MySQL table but it only displays the data from first row/first column of the table twice. I want the foreach loop to iterate down the rows of the first column until it reaches the end of these rows in the MySQL table. Below is the code I used to do it. Please help me to find where I went wrong:

Expand|Select|Wrap|Line Numbers
  1. <td><select>
  2.     <?php
  3.  
  4.     $conn = mysql_connect("localhost", "root") or die(mysql_error());
  5.  
  6.     //database selected
  7.  
  8.     mysql_select_db("database", $conn) or die(mysql_error());
  9.  
  10.     $state = "SELECT player_name FROM table1";
  11.  
  12.     $query = mysql_query($state, $conn);    
  13.  
  14.     $queryColumn = mysql_fetch_array($query);
  15.  
  16.     $aData = $queryColumn;
  17.  
  18.     foreach( $aData as $test ){
  19.     echo '<option value="' . $aData . '">' . $test . '</option>';
  20.     }
  21.  
  22.     ?>
  23.     </select></td>
  24.  
Thanks for your help much appreciated! I've been stuck on this for a while now.
Mar 29 '08 #1
10 2694
RoryClapham
10 New Member
An easier way to do it would be to use a while loop to retrieve the information from the database such as:
[PHP]
<td><select>

<?php

$conn = mysql_connect("localhost", "root") or die(mysql_error());

//database selected

mysql_select_db("database", $conn) or die(mysql_error());

$state = "SELECT player_name FROM table1";

$query = mysql_query($state, $conn);

//Make sure the database connection worked and the $query variable has a value
if($query){
while($row = mysql_fetch_row($query){
//Use a while loop, while mysql_fetch_row can still fetch a new row do the following:
echo '<option value="' . $row[0] . '">' . $row[0] . '</option>';
//$row is set as an array with each index holding a column
}
}

?>
</select></td>
[/PHP]
Mar 29 '08 #2
ronverdonk
4,258 Recognized Expert Specialist
Hi Guys

I am creating a sports database and one of the features involves create drop down dialogue boxes that retrieve data from MySQL tables and provides them as <option> within <select></select>. I am using PHP for this and I'm fairly new at the programming language. I have got the dialogue to work with respect to retrieving the data from MySQL table but it only displays the data from first row/first column of the table twice. I want the foreach loop to iterate down the rows of the first column until it reaches the end of these rows in the MySQL table. Below is the code I used to do it. Please help me to find where I went wrong:

Expand|Select|Wrap|Line Numbers
  1. <td><select>
  2.     <?php
  3.     $conn = mysql_connect("localhost", "root") or die(mysql_error());
  4.     //database selected
  5.     mysql_select_db("database", $conn) or die(mysql_error());   
  6.     $state = "SELECT player_name FROM table1";      
  7.     $query = mysql_query($state, $conn);    
  8.     $queryColumn = mysql_fetch_array($query);
  9.     $aData = $queryColumn;
  10.     foreach( $aData as $test ){
  11.     echo '<option value="' . $aData . '">' . $test . '</option>';
  12.     }
  13.     ?>
  14.     </select></td>
  15.  
Thanks for your help much appreciated! I've been stuck on this for a while now.
The reason that your script goes wrong is that you:

1. fetch only one row $queryColumn from the result (you only have one mysql_fetch_xxx and not within a loop)

2. assign that row ($queryColumn) to another variable ($aData) and display the entire row (consists of 1 column only) as the option statement value and the content of the column the option statement text.

Ronald
Mar 29 '08 #3
flexsingh
17 New Member
The reason that your script goes wrong is that you:

1. fetch only one row $queryColumn from the result (you only have one mysql_fetch_xxx and not within a loop)

2. assign that row ($queryColumn) to another variable ($aData) and display the entire row (consists of 1 column only) as the option statement value and the content of the column the option statement text.

Ronald
Hello there,

I've been stuck on this problem to, could you see if im missing something in my code please. I put in the "while loop" but it returned me no values, and when I take it out I only get the first value and it repeats. I know I am close just something not there. If you could provide a example to your solution before that would be a huge help.

[PHP]<html>
<head>
</head>

<?php
$db_name="project"; // Database name
$tbl_name="bcourt"; // Table name

// Connect to server and select database.
$conn = mysql_connect($_SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>

<body background="main background1.jpg" link="blue" vlink="blue">

<table width="350" border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="Update Network Gaming form" method="post" action="updatebcourt_ac.php">
<table width="100%" border="1" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3" align="center"><strong><font face="sans-serif, Arial" font color="white">Insert Data Into mySQL Database </strong></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" font color="white">Member No</td>
<td><font color="white">:</td>
<td><input name="Member_No" type="int" id="Member_No"></td>
</tr>
<tr>
<td><font face="sans-serif, Arial" font color="white">Court No</td>
<td><font color="white">:</td>
<td><input name="Court_No" type="varchar" id="Court_No" value="Bcourt"></td>
</tr>

<tr>
<td><font face="sans-serif, Arial" font color="white">Time Slot No</td>
<td><font color="white">:</td>
<td><select>

<?php
$db_name="project"; // Database name
$tbl_name="bcourt"; // Table name

// Connect to server and select database.
$conn = mysql_connect($_SESSION['host'], $_SESSION['username'], $_SESSION['password'])or die("cannot connect");
mysql_select_db("$db_name", $conn)or die("cannot select DB");

$time = "SELECT Time_Slot_No FROM $tbl_name WHERE Member_No = 'null'";

$query = mysql_query($time, $conn);

$queryColumn = mysql_fetch_array($query);

$slot = $queryColumn;

//while($queryColumn=mysql_fetch_array($query)){
foreach( $slot as $test ){
echo '<option value="' . $slot . '">' . $test . '</option>';
}
?>
</select></td>
</tr>

<td colspan="3" align="center"><input type="submit" name="Submit" value="Continue"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>



</body>
</html>[/PHP]

I do not get any errors just duplicated values which in this case is "0". My timeslots are just "0,1,2,3,4". Even if I order in DESC order it still only shows "0".

Thank you in advanced for any help.
Mar 29 '08 #4
ronverdonk
4,258 Recognized Expert Specialist
flexsingh: do not hijack this thread!!
My comments on the OP's code also apply exactly to your code, so .... adapt it (why did you comment out the while statement, look at that).

Ronald
Mar 29 '08 #5
flexsingh
17 New Member
flexsingh: do not hijack this thread!!
My comments on the OP's code also apply exactly to your code, so .... adapt it (why did you comment out the while statement, look at that).

Ronald
Sorry didnt mean to do that, I didn't understand your responce. If you could just be alittle bit more clearer please, i'm still alittle new at the coding.

Sorry again.
Mar 29 '08 #6
ronverdonk
4,258 Recognized Expert Specialist
In this piece of code here:
1. You select from the database.
2. Then you fetch only 1 row from the result.
3. You assign the result row to another variable.
4. That 1 result is stored into 1 option statement, i.e. the result row $slot in the value part and the column $test in the text part of the option statement.
[php]
$time = "SELECT Time_Slot_No FROM $tbl_name WHERE Member_No = 'null'";
$query = mysql_query($time, $conn);
$queryColumn = mysql_fetch_array($query);
$slot = $queryColumn;
//while($queryColumn=mysql_fetch_array($query)){
foreach( $slot as $test ){
echo '<option value="' . $slot . '">' . $test . '</option>';
}
?>
</select></td>
[/php]You do not read and process any more rows from the database. So the above code should be something like[php]
$time = "SELECT Time_Slot_No FROM $tbl_name WHERE Member_No = 'null'";
$query = mysql_query($time, $conn); // do the query
while($queryColumn=mysql_fetch_assoc($query)){ // fetch each row in the result set
$val=$queryColumn['Time_Slot_No']; // make it simpler
echo "<option value='$val'>$val</option>"; // construct option statement
}
?>
</select></td>[/php]It is exactly as member RoryClapham alread said in an earlier post no 2 in this thread. but somehow that post was neither read nor followed.

Ronald
Mar 29 '08 #7
flexsingh
17 New Member
In this piece of code here:
1. You select from the database.
2. Then you fetch only 1 row from the result.
3. You assign the result row to another variable.
4. That 1 result is stored into 1 option statement, i.e. the result row $slot in the value part and the column $test in the text part of the option statement.
[php]
$time = "SELECT Time_Slot_No FROM $tbl_name WHERE Member_No = 'null'";
$query = mysql_query($time, $conn);
$queryColumn = mysql_fetch_array($query);
$slot = $queryColumn;
//while($queryColumn=mysql_fetch_array($query)){
foreach( $slot as $test ){
echo '<option value="' . $slot . '">' . $test . '</option>';
}
?>
</select></td>
[/php]You do not read and process any more rows from the database. So the above code should be something like[php]
$time = "SELECT Time_Slot_No FROM $tbl_name WHERE Member_No = 'null'";
$query = mysql_query($time, $conn); // do the query
while($queryColumn=mysql_fetch_assoc($query)){ // fetch each row in the result set
$val=$queryColumn['Time_Slot_No']; // make it simpler
echo "<option value='$val'>$val</option>"; // construct option statement
}
?>
</select></td>[/php]It is exactly as member RoryClapham alread said in an earlier post no 2 in this thread. but somehow that post was neither read nor followed.

Ronald
Im really sorry I must have missed it. Was just stressful and frustrating it didn't work after a long time. I'll read more carfully next time. Thank you for helping me and sorry again.
Mar 29 '08 #8
ronverdonk
4,258 Recognized Expert Specialist
Okay, does it work now as you want it to?

ROnald
Mar 29 '08 #9
flexsingh
17 New Member
Okay, does it work now as you want it to?

ROnald
Yes it does thank you for all the help :)
Mar 30 '08 #10
ronverdonk
4,258 Recognized Expert Specialist
You are welcome. See you again next time.

Now back to the original poster: darkenroyce. Sorry for the intrusion but did you also solve your problem?

Ronald
Mar 30 '08 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: knoak | last post by:
Hi there, I have a admin area for a website, and on some part the admin can select options from two different drop down boxes. Now i thought that it would be better to use one Query for both...
6
by: PT | last post by:
I got a form with many text boxes, checkboxes and 3 drop downs. From that 3, 2 are dependant. I can choose one drop down, and the next drop down should display the dependant values of the first...
0
by: vikram.cvk | last post by:
Hello Experts, Im trying to design a CSS vertical drop down menu which should have the following functionality. Home About Us | -->Overview
11
by: Dan | last post by:
Hello all, I am getting records from a db and displaying the records to the user through a drop down menu in an asp page. Each record has 6 fields and I need to display them all to the user in...
2
by: kmnotes04 | last post by:
Is it possible to link one drop-down box to another? For example, if a name is chosen from a drop-down list, can another drop-down list then automatically display the person's office as a result of...
2
by: tpaulson | last post by:
I have a couple of DIV's that I hide/display based on radio buttons. On the DIV's, I have multiple drop down boxes. The source shows that they are populated, but I can't make them drop down. Only...
4
by: teknoshock | last post by:
I have created a page with multiple drop down boxes, all populated with the same options. My problem is, for 12 dropdown boxes and 40 choices per box, I end up with a massive file. Also, if I...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
6
by: phpnewbie26 | last post by:
My current form has one multiple select drop down menu as well as few other drop down menus that are single select. Originally I had it so that the multiple select menu was first, but this created...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.