Sheils
asked on
How to display current selection in a select box
I have the following php form which is linked to a mysql database
I want the select box on line 66 to show the data in the table when they are available and allow me to edit that data. At present it only shows the first option and does not seem to be linked to the data.
So the question is how do I make the select box show the data for that record and allow me to update.
Thanks
<?php
$sql="SELECT `fldTransactionId` , DATE_FORMAT( `fldDate` , '%d %b %Y' ) , `fldWithdrawingAccount` , `fldSalesPerson` ,`fldInvoice` , `fldAccount` FROM `tbTransactions` LEFT JOIN `tbAccounts` ON `fldAccountID` = `fldWithdrawingAccount` WHERE `tbTransactions`.`fldtype` =2";
$j=$_GET['nextrow'];
switch ($_GET['recnav']){
case "Prev":
$j=$j-1;
break;
case "Next":
$j=$j+1;
break;
case null:
$j=$j+1;
break;
}
$result=mysql_query($sql);
switch($j){
case 0:
case 1:
$msg= "This is the first record. You can't go back any further";
$j=1;
break;
case mysql_num_rows($result):
$msg= "This is the last record";
break;
case mysql_num_rows($result)+1:
$j=mysql_num_rows($result);
$msg= "This is the last record";
break;
//Main form starts here
echo "<form class='mainform' method='GET' action='../pages/pgMain.php'>";
echo "<input type='hidden' name='nextrow' value='".$j."'>".$msg."<br/>";
echo "<input type='hidden' name='navbutton' value='Expenses'>";
echo "<div>Date:</div><input value='".mysql_result($result, $j-1,1)."' ><br/>";
echo "<div>Withdrawing Account:</div><input value='".mysql_result($result, $j-1,5)."' ><br/>";
include "../queries/qryAccounts.php";
$WithdrawingAccountssql="Select `tbTransactions`.`fldWithdrawingAccount`,`tbAccounts`.`fldAccount` From `tbAccounts` LEFT JOIN `tbTransactions` ON `tbAccounts`.`fldAccountID`=`tbTransactions`.`fldWithdrawingAccount`";
$Accounts=mysql_query($WithdrawingAccountssql);
while ($AccountsRow = mysql_fetch_array($Accounts)){
$AccountsOption=$AccountsOption."<option VALUE=".$AccountsRow[0].">". $AccountsRow[1]."</option>";
}
echo "<div>Withdrawing Account:</div><select>".$AccountsOption."</select><br/>";
echo "<div>Sales Person:</div><input value='".mysql_result($result, $j-1,3)."' ><br/>";
echo "<div class='navbuttons'>"
echo "<input type='submit' name='recnav' value='Prev' />"
echo "<input type='submit' name='recnav' value='Next' />"
</div>
echo "</form>
?>
I want the select box on line 66 to show the data in the table when they are available and allow me to edit that data. At present it only shows the first option and does not seem to be linked to the data.
So the question is how do I make the select box show the data for that record and allow me to update.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This code works when there is data in the field. But all the option disappears when there is no data in the field.
I believe that it has to do with spacing but I just can't get it right.
while ($AccountsRow = mysql_fetch_array($Accounts)){
if($AccountsRow[1]=mysql_result($result, $j-1,5)){
$selected=" selected ";
}
$AccountsOption=$AccountsOption."<option".$selected."VALUE=".$AccountsRow[0].">". $AccountsRow[1]."</option>";
}
echo "<div>Withdrawing Account:</div><select>".$AccountsOption."</select><br/>";
I believe that it has to do with spacing but I just can't get it right.
ASKER
I have tried the following if..else statement to no avail
if($AccountsRow[1]=mysql_r esult($res ult, $j-1,5)){
$selected=" selected ";
}
else{
$selected=" ";
}
if($AccountsRow[1]=mysql_r
$selected=" selected ";
}
else{
$selected=" ";
}
add $selected=""; in else
while ($AccountsRow = mysql_fetch_array($Account s)){
if($AccountsRow[1]=mysql_r esult($res ult, $j-1,5)){
$selected=" selected ";
}else{
$selected="";
}
while ($AccountsRow = mysql_fetch_array($Account
if($AccountsRow[1]=mysql_r
$selected=" selected ";
}else{
$selected="";
}
What do you mean by this //But all the option disappears when there is no data in the field.//
Can you explain more
Can you explain more
ASKER
Chaged as follows still no luck
while ($AccountsRow = mysql_fetch_array($Account s)){
$selected="";
if($AccountsRow[1]=mysql_r esult($res ult, $j-1,5)){
$selected=" selected ";
}
else{
$selected="";
}
$AccountsOption=$AccountsO ption."<op tion ".$selected." VALUE=".$AccountsRow[0]."> ". $AccountsRow[1]."</option> ";
}
see output on: http://www.wowislandcharter.com/wowaccounts/pages/pgMain.php?nextrow=4&navbutton=Expenses&recnav=Prev
while ($AccountsRow = mysql_fetch_array($Account
$selected="";
if($AccountsRow[1]=mysql_r
$selected=" selected ";
}
else{
$selected="";
}
$AccountsOption=$AccountsO
}
see output on: http://www.wowislandcharter.com/wowaccounts/pages/pgMain.php?nextrow=4&navbutton=Expenses&recnav=Prev
ASKER
It looks like the while loop is not running if the if statement is false. I also notice that when is does work the 4 options are the same.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you check if condition?
ASKER
ok that fixed it. Thanks a lot mate
ASKER