My collection of things.

jKafer on the Net

January 12th, 2018 at 12:03 pm

PHP Multidimensional Array NOT using Index

Okay – so I run into this every once in awhile and I usually code around it, but I decided on this cold day in January (-18° below at 7am) that I was going to get this working the way I want it to be!!! 

When I google this issue it just loops around and around in a big circle – very frustrating – so when I got it working I decided to put it here – mostly so I could find it!!! 🙄 

SITUATION:

I have a form where the information is then put into an sql table.
On this form is a dropdown selection box – being a good data steward, I am only recording 1 character for each choice. However, when displaying the completed list of items that have been entered, I would rather display the actual description rather than the “value” of the dropdown. 
Seems like a simple thing – right?!

Here is the dropdown  Here is the code behind it
<select name="category" id="category">
   <option selected value="">Select Category</option>
   <option value="F">Food</option>
   <option value="L">Live</option>
   <option value="E">Equipment</option>
   <option value="P">Plants</option>
   <option value="M">Media</option>
   <option value="T">Tank</option>
   <option value="R">Large Item</option>
   <option value="O">Other</option>
 </select>
So when the items are listed it looks like this:

So, what I wanted to do is when I am looking thru the recordset of the database, I want to have an array that has a cross-reference to the Value that is stored in the Category field in the database.

Seems simple enough – but every reference I could find to arrays in PHP you use the index number to find and display the item in the array. I went thru 6 pages on google to finally find one that references it using the item not the index number.  Kudos to this place.

MY SOLUTION:

In my listing file, I created this array (this is the information used in the form – shown above):

$catList = array(
             "F"=>"Food",
             "L"=>"Live",
             "E"=>"Equipment",
             "P"=>"Plants",
             "M"=>"Media",
             "T"=>"Tank",
             "R"=>"Large Item",
             "O"=>"Other",
 );

Then, to do the lookup in the array:

           $catList[$row[‘category’]]

$row is the array of information that is being pulled from the database, category is the the field from the database that holds the info from the submitted form.

Well, now the list looks a whole lot better:

BEFORE AFTER

 

Tags: , ,
-

Comments are closed.


  • Count per Day

    • 4Visitors today:
    • 40Visitors per day:
    • 0Visitors currently online:
  • ScrapBook Software - MyMemoriesSuite
  • Latest Pins on Pinterest

    • Blue Carolina Starburst Quilt {Free Pattern}

    • Well Texas Tumbleweeds got finished.  If you missed that, find it here.  So it’s time for a new UFO.  Earlier I had pulled Fair and Square but after I got the string triangles done, I left it…

    • 719 Likes, 25 Comments - David (@scfdavid) on Instagram: “Here’s my Blockheads block for Week 45 - Double Dutch - from my friend, Jo Morton! Thanks, Jo! All…”

    • Quilt patriotic Hidden Glorry reserved for Carrie by marytequilts

    • Tutorial - How to Get Perfect Centers

    • How to sew and trim half rectangle triangles (HRTs) - Kirsty of Bonjour Quilts

    • Center Point designed by Elise Lea for Robert Kaufman. Features Chalk and Charcoal by Jennifer Sampou, shipping to stores May 2018. Roll up friendly. FREE pattern available for download from robertkaufman.com May 2018. #FREEatrobertkaufmandotcom

    • A modern quilt blog focused on practical tutorials and quilting tips. Sharing traditionally pieced quilt patterns and lots of free quilt alongs.