Solved

How to display current selection in a select box

Posted on 2013-01-07
12
281 Views
Last Modified: 2013-01-07
I have the following php form which is linked to a mysql database

<?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>

?>

Open in new window


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
0
Comment
Question by:Sheils
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 12

Assisted Solution

by:sivagnanam chandrakanth
sivagnanam chandrakanth earned 500 total points
ID: 38750042
use selected attribute in option tag to get the default value selected by default

http://www.w3schools.com/tags/att_option_selected.asp
0
 
LVL 16

Author Comment

by:Sheils
ID: 38750056
The form moves through a number of records. So the selected cannot be static. Are you suggesting that I use some more code to move the selected attribute to the relevant option each time I move to a new record?
0
 
LVL 12

Accepted Solution

by:
sivagnanam chandrakanth earned 500 total points
ID: 38750068
yes use a if condition within while loop to identify and add "selected"


 while ($AccountsRow = mysql_fetch_array($Accounts)){
if(somecondition==somevalue){
$selected='selected';
}
      $AccountsOption=$AccountsOption."<option ".$selected." VALUE=".$AccountsRow[0].">". $AccountsRow[1]."</option>";

      }
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 16

Author Comment

by:Sheils
ID: 38750115
This code works when there is data in the field. But all the option disappears when there is no data in the field.

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/>";

Open in new window


I believe that it has to do with spacing but I just can't get it right.
0
 
LVL 16

Author Comment

by:Sheils
ID: 38750120
I have tried the following if..else statement to no avail

if($AccountsRow[1]=mysql_result($result, $j-1,5)){

      $selected=" selected ";

      }

      else{
        $selected=" ";
      }
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38750124
add  $selected=""; in else

while ($AccountsRow = mysql_fetch_array($Accounts)){
      if($AccountsRow[1]=mysql_result($result, $j-1,5)){

      $selected=" selected ";

      }else{
$selected="";
}
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38750130
What do you mean by this //But all the option disappears when there is no data in the field.//

Can you explain more
0
 
LVL 16

Author Comment

by:Sheils
ID: 38750155
Chaged as follows still no luck

while ($AccountsRow = mysql_fetch_array($Accounts)){
      $selected="";
      if($AccountsRow[1]=mysql_result($result, $j-1,5)){

      $selected=" selected ";

      }
      else{
        $selected="";
      }

      $AccountsOption=$AccountsOption."<option ".$selected." VALUE=".$AccountsRow[0].">". $AccountsRow[1]."</option>";

      }

see output on: http://www.wowislandcharter.com/wowaccounts/pages/pgMain.php?nextrow=4&navbutton=Expenses&recnav=Prev
0
 
LVL 16

Author Comment

by:Sheils
ID: 38750166
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.
0
 
LVL 12

Assisted Solution

by:sivagnanam chandrakanth
sivagnanam chandrakanth earned 500 total points
ID: 38750167
your if condition has issue, use two equal to (==)

 if($AccountsRow[1]==mysql_result($result, $j-1,5)){

      $selected=" selected ";

      }
0
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 38750190
Did you check if condition?
0
 
LVL 16

Author Comment

by:Sheils
ID: 38750249
ok that fixed it. Thanks a lot mate
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

696 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