473,396 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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 2692
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 Expert 4TB
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
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 Expert 4TB
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: 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 Expert 4TB
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
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 Expert 4TB
Okay, does it work now as you want it to?

ROnald
Mar 29 '08 #9
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 Expert 4TB
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.