#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0

    Multidimensional Associative Arrays


    Hello, I'm new here so I apologize if I leave out any necessary info to answer my question.

    My PHP page is reading in 3 fields from an oracle table and displaying the info on the page using a while(ocifetchinto($stmt, $result, OCI_NUM+OCI_RETURN_NULLS)) which is inside <form post method>. This works perfectly and does what I want it to.

    What I want to include in this while loops is a multidimensional array with the 3 fields I am reading in one at a time.

    In PERL I would have done a hash $arr{field1}{field2}{field3}=1; However, it doesn't work the same in PHP from what I've gathered.

    The reason for this is at the bottom of the page under the html table showing the 3 fields of data from the oracle table I have a submit button so the user can add additional records to the oracle table. This also works as I wanted.

    What I want is to be able to display an error if the record they are trying to enter is already listed in the oracle table and currently being display on the webpage.

    So, I figured aside from displaying the 3 fields of data on the webpage I would create a hash/associative array that I can compare the user's entry to before it attempts to insert it into the oracle table. If it exists it'll give them a message that it's already there. If not....it'll add it.

    I'm just having trouble wrapping my head around the process of this multidimensional associative array. One of the additional things I am unsure about, aside from creating and looping through this multi-array, is if the array carries over after clicking the submit button or do I need to use some sort of $_POST[] to access the array or am I missing a step completely?

    I'm still new to PHP so I apologize for my ignorance.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,183
    Rep Power
    9398
    The syntax is like Perl's:
    PHP Code:
    $arr["field1"]["field2"]["field3"
    but you have to have a 3D array, of course. I don't know Oracle and ocifetchinto() but I'm sure $result is just a 1D array.
    PHP Code:
    $result["field1"]
    $result["field2"]
    $result["field3"
    "Compare the user's entry"... Sounds like something that should be done in a database query, not in code. How's that supposed to work?
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    why not simply have the database check whether the entered values already exist?

    What you describe is very cumbersome, fragile and inefficient. No, PHP objects do not live until after the request -- unless you store them somewhere. So you'd have to take the whole array and push it into the session or something. Then you'd have to loop through the whole thing and look for the particular combination. And then you might still run into trouble, because the array can already be outdated (if somebody has entered the same data faster than you).

    I'd say: Forget it. It's much easier, more efficient and more robust to let the database system take care of this. Put a UNIQUE constraint on the combination (which you should have done, anyway). And then try to insert the row. If it fails, you tell the user.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    4
    Rep Power
    0
    I'm basically doing this to help myself learn different methods I can use. The oracle table I created already has a unique key set to those 3 fields. So, if it already exists nothing gets added to the database. My main goal was to provide the user a "pop-up" informing them that the entry they're submitting already exists. It would be easier to just provide "zero" info to the user since them trying to add a duplicate would do nothing to the table. But, as I said, I'm trying to learn as I'm still new to PHP and usually work with PERL & SQL and don't get quite as frequent chances to play with PHP. In addition, this is for a single client (one person) who will be the only one who has access and updates this page. It's basically a visual display of platform, database, table, field names that they wish to see on a report. This will also provide additional info (fields per table in the list) which they can choose whether they want to appear on a report which runs quarterly.

    Here's how I got it to work with some javascript.

    PHP Code:
    <?
    //pulls the data from an oracle table
    $qry="select field1, field2, field3 from Oracle_Table where field3 IS NULL order by field1, field2, field3";
    $stmt ociparse ($conn$qry);
    ociexecute($stmt);

    //loops through the results pulled row-by-row
    while(ocifetchinto($stmt$resultOCI_NUM+OCI_RETURN_NULLS))
         {
    //creates php array of existing data so $list looks like 
    //$list=("field1a.field2a.field3a", "field1b.field2b.field3b", etc)
         
    array_push($list$result[0].".".$result[1].".".$result[2]);
         }

    //loops through each element of the php array $list
    //writes it into a javascript associative array/perl hash
    //list2['field1a.field2a.field3a']=1
    //list2['field1b.field2b.field3b']=1
    //etc
    echo "<script>\n";
    echo 
    "list2=new Array()\n";
    foreach(
    $list as $val)
         {
         echo 
    "list2['$val']=1\n";
         }
    echo 
    "</script>\n";

    //where user inputs a new entry to add to the oracle table
    //i didn't include the code to writing to the oracle table 
    //mainly wanted to explain how I was able to compare the
    //user's new entry below to see if it exists in the table already
    echo "Field 1<input name='new_fld1' size='40' maxlength='30'>\n";
    echo 
    "Field 2<input name='new_fld2' size='40' maxlength='30'>\n";
    echo 
    "Field 3 <input name='new_fld3' size='40' maxlength='30'>\n";

    echo 
    "</br></br><input type='submit' value='Submit Changes' onclick='if (chkEx()) pgnbr.value=\"1\"; else return false;'>\n";
    ?>

    <script>
    //checks if the new entry exists in the table already
    function chkEx()
         {
    //defaults to return true to the submit button so it continues
    //normally and goes to the process code which adds the new
    //entry to the oracle table
         retval = true;

    //combines the user's entry into the variable v so it looks like
    //v=(new_fld1.new_fld2.new_fld3)
         v=document.myForm.new_fld1.value + "." + document.myForm.new_fld2.value + "."+ document.myForm.new_fld3.value;

    //uses the entry we combined & stored in v and sees if it
    //exists in the the list2 array we created earlier in the previous
    //<script> where we wrote the php array to the javascript array
         if (list2[v]==1)
              {
    //if it exists it returns false to the submit butto so it doesn't continue and provides a message the entry already exists.
              retval=false;
              alert("This entry already exists.");
              }
         return retval;
         }
    </script>
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    This is not even remotely a sensible solution, regardless of whether it's just for learning, for a single user or whatever.

    You've written an extremely inefficient and fragile emulation of database constraints. The whole thing will probably melt your CPU, and it will fall apart if only you use multiple tabs/windows or click "too fast".

    I mean, just think about it: You load the whole table into a PHP array. Then you write the whole array into the HTML output. Then JavaScript parses this whole expression. Then JavaScript iterates over the whole array, looking for a particular entry. If the entry doesn't exist, you take a shot and insert the thing, hoping the database hasn't been modified in the meantime -- and then Oracle again checks if the entry already exists. C'mon. This is not good.

    I don't know why you assume that you somehow can't use the errors provided by Oracle. Every error has a certain code. You fetch this code with oci_error(), and if it's a constraint violation, you simply display a custom error message. That's one line of code, and it actually works -- in constrast to all the JavaScript stuff.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo