i am having an issue with a dynamic table with dynamic combo box's on each row basically whats happening is all the information is getting pulled for a mysql database which is working well in the table sense but not with the combo box's the issue being the first row of the table is doing exactly (combo box and table working) what i want but the following rows are not they're are only displaying one value in the combo box's instead of 3 so i'm figuring its not processing the while loop properly i was wondering if any one had a开发者_StackOverflow社区 solution to this.
//Check to see if a driver has been assigned
$driver = $row["driver"];
if($driver==""){
$select_driver = 'Select Driver' ;
} else{
$select_driver = $driver;
}
echo ' <tr><td><select name= "stuff"><option value="'.$select_driver.'">'. $select_driver;
while ($row_driver = mysql_fetch_array($dbSearchDriver_result)){
echo '<option value="'.$row_driver["Username"] .'">'.$row_driver["Username"] .'</option>'; //Echo's each driver
}
echo '</option></select></td>';
thanks in advance
Looks like you are using over and over the same $dbSearchDriver_result.
This is a recordset so that everytime you do :
$row_driver = mysql_fetch_array($dbSearchDriver_result)
it advances by one record inside the recordset. When you are reaching the end of the recordset it does not come back to the first record. You have to explicitly tell it to.
http://php.net/manual/fr/function.mysql-data-seek.php
You can try :
while ($row_driver = mysql_fetch_array($dbSearchDriver_result)) {
echo ''.$row_driver["Username"] .''; //Echo's each driver
}
mysql_data_seek($dbSearchDriver_result,0);
The problem is this: You have to close the "option" tag in this line:
echo ' <tr><td><select name= "stuff"><option value="'.$select_driver.'">'. $select_driver;
You should close your <option>
tag after "Select Driver" and not after the while
loop
first of all database related stuff should stay in one place. Any other than the html code.
Step 1: create a function like this :
function query_mysql($SQL_QUERY) {
$data = array();
$result = mysql_query($SQL_QUERY);
while ($row_driver = mysql_fetch_array($result)) {
$data[] = $row_driver;
}
return $data;
}
put the function in a php file to be included when needed.
$myData = query_mysql("SELECT * FROM table");//your query
$myData
will be an array which can be used everytime you want to.
just parse it when you need it.
PS: read about MVC(model view controller) and separation of concerns.
精彩评论