Solved

Replace Numeric Variable w/ Alpha-Numeric Variable

Posted on 2007-11-23
10
482 Views
Last Modified: 2013-12-12
A form (attached) passes various shipping values to a session variable on the checkout page. Once the form passes the shipping values (i.e. 03) to the checkout page; I lose the textual alpha-numeric value (i.e. Ground). I want to display the textual value on the checkout page. What's the best way to replace the created SESSION variable integer with the text version from the previous form? I know of various ways to accomplish this - but which way is best and most efficient?
<select name="UPS_Service" id="UPS_Service">

        <option value="03" selected="selected">Ground</option>

        <option value="12">3 Day Select</option>

        <option value="02">2nd Day Air</option>

        <option value="59">2nd Day Air AM</option>

        <option value="13">Next Day Air Saver</option>

        <option value="01">Next Day Air</option>

        <option value="14">Next Day Air Early AM</option>

      </select>

Open in new window

0
Comment
Question by:EMB01
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 4

Expert Comment

by:simonkin
Comment Utility
Hi,

Why not make a table of value and option pairs in your MySQL database.

Then do a MySQL lookup for the text option for the shipping value..
0
 
LVL 14

Author Comment

by:EMB01
Comment Utility
I'd rather keep it hardcoded; just because these values will never be modified.
0
 
LVL 4

Expert Comment

by:simonkin
Comment Utility
Alright,

Try some elseif statements...


<?php
 

    if ($_SESSION["shipping_value"] == 03) {
 

          $shipping_text = "Ground";
 

    } elseif ($_SESSION["shipping_value"] == 12){
 

          $shipping_text = "3 Day Select";
 

    } // and so on...
 

?>

Open in new window

0
 
LVL 14

Author Comment

by:EMB01
Comment Utility
Isn't there a better way to do it with arrays?
0
 
LVL 28

Expert Comment

by:gamebits
Comment Utility
Any reason why you use integer for the value submited instead of a text value

<option value="Ground" selected="selected">Ground</option>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 14

Author Comment

by:EMB01
Comment Utility
Yes, because the UPS Shipping Quote API requires variable integers such as 03, 12, etc.
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 100 total points
Comment Utility
You can use a switch case statement:


<?php

	switch($_SESSION["shipping_value"])

	{

		case "03":

			{

				$mytext = "Ground";

				break;

			}

			

		case "12":

			{

				$mytext = "3 Day Select";

			}

			

		// etc........

		

	}

?>

Open in new window

0
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 100 total points
Comment Utility
Alternatively you can use javascript.

Simple demonstration here:
<select name="UPS_Service" id="UPS_Service">

        <option value="03" selected="selected">Ground</option>

        <option value="12">3 Day Select</option>

        <option value="02">2nd Day Air</option>

        <option value="59">2nd Day Air AM</option>

        <option value="13">Next Day Air Saver</option>

        <option value="01">Next Day Air</option>

        <option value="14">Next Day Air Early AM</option>

</select>
 

<script type="text/javascript">

function show(){

var theContents = document.getElementById('UPS_Service')[document.getElementById('UPS_Service').selectedIndex].innerHTML;

window.alert(theContents);

}

</script>
 

<input type="button" onclick="show();" value="What is Selected?">

Open in new window

0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 25 total points
Comment Utility
You do

$array_alpha = array("03"=>"Ground","12"=>3 Day Select,etc);

$_SESSION["array_alpha"] = $array_alpha;

Once the form is submited you can access the name simply by doing

$name = $_SESSION["array_alpha"][$_POST["UPS_Service"]]
0
 
LVL 14

Author Closing Comment

by:EMB01
Comment Utility
I modified the code to:
<?php
      switch($_SESSION["shipType"])
      {
            case "03":
                  print "Ground";
                  break;
            case "12":
                  print "3 Day Select";
                  break;
            case "02":
                  print "2nd Day Air";
                  break;
            case "59":
                  print "2nd Day Air AM";
                  break;
            case "13":
                  print "Next Day Air Saver";
                  break;
            case "01":
                  print "Next Day Air";
                  break;
            case "14":
                  print "Next Day Air Early AM";
                  break;
      }
?>
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now