Jump to content

Persistant Selected Combobox Problem


Karebac

Recommended Posts

Here are two different functions which build the same combo box in two different ways:

 

// FIRST FUNCTION WHICH BUILDS COMBO BOX GT AS HTML STRING FOR ECHO.
function buildcombo()
  {
  $db_host="mysql";
  $db_user="YoursTruly";
  $db_pass="YeahRight!";
  $db="MyDatabase";
  mysql_connect($db_host,$db_user,$db_pass);
  mysql_select_db($db);
  $sql = "SELECT pk, company FROM customer order by company";
  $query = mysql_query($sql) or die(mysql_error());
  $cb = "<select NAME = 'gt'>";
  while ($row = mysql_fetch_array($query)) {
   $cb .= '<option value="' . $row[0] . '">'
                . $row[1] . '</option>';
  }
  $cb .= '</select>';
  return $cb;
}


// SECOND FUNCTION WHICH BUILDS SAME COMBO BOX AS ARRAY OF HTML TO BE ECHOED IN FOREACH

function buildv()
  {
  $db_host="mysql";
  $db_user="YoursTruly";
  $db_pass="YeahRight!";
  $db="MyDatabase";
  mysql_connect($db_host,$db_user,$db_pass);
  mysql_select_db($db);
  $sql = "SELECT pk, company FROM customer order by company";
  $query = mysql_query($sql) or die(mysql_error());
  $cb = "";
  $cbcount = 0;
  while ($row = mysql_fetch_array($query)) {
   $cbcount = $cbcount + 1;
   $cb[$cbcount]= $row[0] . '">'
                . $row[1];
  }

  return $cb;
}

// DURING FIRST PASS INITIALIZATION, THE TWO COMBO BOXES ARE BUILT.

if (!isset($cb1)) {
$cb1 = buildcombo();
$v = buildv();
}

 

 

 

When the user selects a company name in the combo box called gt (for "go to"), the following code captures the primary key, and the form jumps to that record:

 

$nextkey = $_POST["gt"];

 

 

 

I have already succeeded in making my option buttons persistant, so that, whatever the user has chosen for an option will persist with each submit, until the user changes it again.

 

<p>Action
<br>

<? if ($processchoice == "None") {
echo "<input type='radio' name='processchoice' value='None' checked> None<br>";
} else {
echo "<input type='radio' name='processchoice' value='None'> Add<br>";
}
?>

<? if ($processchoice == "Add") {
echo "<input type='radio' name='processchoice' value='Add' checked> Add<br>";
} else {
echo "<input type='radio' name='processchoice' value='Add'> Add<br>";
}
?>

<? if ($processchoice == "Update") {
echo "<input type='radio' name='processchoice' value='Update' checked> Update<br>";
} else {
echo "<input type='radio' name='processchoice' value='Update'> Update<br>";
}
?>

<? if ($processchoice == "Delete") {
echo "<input type='radio' name='processchoice' value='Delete' checked> Delete<br>";
} else {
echo "<input type='radio' name='processchoice' value='Delete'> Delete<br>";
}
?>
<p>Direction
<br>

<? if ($directionchoice == "Backward") {
echo "<input type='radio' name='directionchoice' value='Backward' checked> Backward<br>";
} else {
echo "<input type='radio' name='directionchoice' value='Backward'> Backward<br>";

}
?>

<? if ($directionchoice == "Forward") {
echo "<input type='radio' name='directionchoice' value='Forward' checked> Forward<br>";
} else {
echo "<input type='radio' name='directionchoice' value='Forward'>Forward<br>";
}
?>
<? if ($directionchoice == "Go To") {
echo "<input type='radio' name='directionchoice' value='Go To' checked> Go To<br>";
} else {
echo "<input type='radio' name='directionchoice' value='Go To'>Go To<br>";
}
?>
<!-- $cb1 is the combo box we built, to see company name sorted and select goto -->
<p>
<?echo $cb1; ?><br />
<p>

 

 

Notice that the first combo box, built by the first function, is echoed as ONE LONG string of html.

 

In order to learn how to make such a combo box persistant in its selection, I have echoed it again, but this time using the array $v, built by the second function.  I do this so that I can make a test and, when I detect what the user has selected in the previous screen, I can echo SELECTED = 'TRUE', and thus maintain that selection.  You will notice that I do a DUMMY TEST  to see if the index is = to 7, just to know that my code works, and each time the user submits, the screen returns with the 7th value selected.

 

WHAT I WANT TO DO, is have a test which determines which entry was previously selected, but I cant figure out how to do it.

 


<? 
echo "<select NAME = 'vt'>";

$j = 0;
$j = $j - 1;
foreach($v as $value)
{
$j = $j + 1;
echo $value;
// if($values[$x] == $_POST['gt'])

if ($v[$j] == 7) {
echo '<option selected = "true" value="' . $value  . ' </option>' ;
                } else {
echo '<option value="' . $value  . ' </option>' ;
                       }


}
echo '</select>';
?>

 

Thanks for your help!

 

Link to comment
Share on other sites

One of my problems, in trying to solve this question, is that I could not examine $value during the FOREACH loop, to figure out what the data looks like and know how to get a match.

 

I tried various ways to echo it to the screen, but could see nothing.

 

So, I decided to open a text file, and write out $value.  Then I examined the text file in the editor, and can clearly see where the record key is in $value that I need to match.

 

<? 
echo "<select NAME = 'vt'>";

$j = 0;
$j = $j - 1;
$file = fopen("dumpit.txt", "w");
foreach($v as $value)
{
$j = $j + 1;
fwrite($file, $value);
// if($values[$x] == $_POST['country'])

if ($v[$j] == $_POST[gt]) {
echo '<option selected = "true" value="' . $value  . ' </option>' ;
                } else {
echo '<option value="' . $value  . ' </option>' ;
                       }


}
fclose($file);
echo '</select>';
?>

 

 

The first line of the dump looks like this:  19">3rd. Ave.

The Primary Key is 19. Each primary key is at the beginning of $value, and delimited by a double quote.

 

So now, all I need to do is, during that FOREACH, capture the leading characters in $value up to the first double quote, match to the primary key currently selected, and set SELECTED = 'TRUE'.

Link to comment
Share on other sites

Thanks for reading and replying!

 

I went to #php on quakenet irc and found an answer that works.

 

I am playing with a combo box.

 

I want the combo box to remember the previous selection, and remain selected.

 

I did it like this:

 

<? 
echo "<select NAME = 'vt'>";

$j = 0;
$j = $j - 1;
$file = fopen("dumpit.txt", "w");
foreach($v as $value)
{
$j = $j + 1;
fwrite($file, $value);
$grabbit = substr($value, 0, strpos($value, '"'));

fwrite($file, "gotcha " . $grabbit . "nyah nyah");

// if($values[$x] == $_POST['country'])

if ($grabbit == $_POST[gt]) {
echo '<option selected = "true" value="' . $value  . ' </option>' ;
                } else {
echo '<option value="' . $value  . ' </option>' ;
                       }


}
fclose($file);
echo '</select>';
?>

 

I am leaving in the debug code for dumping $value to a text file, because I think it is useful and also amusing.

 

I could see from my dump file that each $value had the record key as the leading characters, followed by a double quote.

 

I couldnt think how to grab the record key, so I asked in that IRC channel and was told to do it this way:

 

$grabbit = substr($value, 0, strpos($value, '"'));

 

 

 

Link to comment
Share on other sites

Yesideez,

 

Thanks for asking what I will be using this file for.

 

As a sort of hobby (I dont get paid to do this), I created a website for my employer, a Yahoo Business website, that I was able to quickly paint using their "Sitebuilder" freeware, a wysiwyg that requires zero knowledge of html.

 

I want to learn enough mysql and php to build a form that will permit the entry of a sales order.  The table "customer" is the first step, containing all the bill to addresses.

 

My main goal is just to have fun learning some php and mysql.

 

If I can get a useable application, then sales reps may possibly use it to enter orders, or they may not, depending on how much they like keyboards, computers and Internet.

 

My first goal was to learn how to build some kind of session login.  I found a very simple example in Google, and make that work. 

 

My next step was to teach myself how to write an add/edit/update/delete program for any given table.

 

I figure that once I get a model that works for me, and is simple, then I can keep modifying it to handle different tables. 

 

My session login brings me to a menu.php .  As I get more and more useable scripts, I add them to that menu.

 

I exported our customers from QuickBooks to an Excel csv file, and then figured out how to import that into MySQL on my desktop.  From their, I generated the sql code to create the table and load the data at the Yahoo site.

 

This week, I worked on the edit/update/add/delete script (part of which you see here).  I decided that I wanted check boxes to remain persistent from screen to screen.

 

Then, I decided I wanted a combo box that would have all the customer names in sorted order, and when you select a customer, the script jumps to that record.  I wanted to see if I could make THAT combo box also persistent in its choice, from screen to screen, rather than defaulting with each post to its initial select value.

 

Of course, I could try to search and find someone else's code, and use it as a model. But then, I will not learn as much about php and mysql, as I would if I struggle with each feature, like I am doing now.

 

I feel that if I post in detail about what I am doing, then just possibly, such posts might help people who are even more of a beginner than I am, and give them ideas for their own php exercises and experiments.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.