Advertisement
Guest User

Arrays and Hashes

a guest
Apr 8th, 2022
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.60 KB | None | 0 0
  1.  
  2. EXAMPLE ARRAY:
  3.  
  4. <cms:set populated_authors__array = '
  5. {
  6.    "26b3eed025edc2ef22a0466bab45ae57" : {
  7.        "page_id" : "18",
  8.        "name" : "Ray Bradbury"
  9.    },
  10.    "60ee1ca285beb8348406446e56e6cda4" : {
  11.        "page_id" : "19",
  12.        "name" : "Isaac Asimov"
  13.    }
  14. }
  15. ' is_json='1' scope='global' />
  16.  
  17.  
  18. -- Variable '_input_author_hash' comes from CSV_READER and I create a hash of it to avoid string comparison
  19.  
  20. <cms:set _input_author_hash = "<cms:md5 _input_author />" scope='global' />
  21.  
  22. -- Next, we check if that hash is already in my list of Authors in CMS
  23.  
  24. <cms:if "<cms:arr_key_exists key=_input_author_hash in=populated_authors__array />" >
  25.  
  26.     -- Author already populated and stored in array, thus I know its k_page_id -->
  27.  
  28.       <cms:set related_author_id = "<cms:get "populated_authors__array.<cms:show _input_author_hash />.page_id" />" />
  29.  
  30.  
  31. -- Otherwise add a new Author to the list:
  32. <cms:else />
  33.  
  34.     -- creates a new page with this Author
  35.  
  36.       <cms:db_persist _masterpage='related_authors.php' .... />
  37.  
  38.     -- saves 'k_last_insert_id' into 'populated_authors__array' variable:
  39.  
  40.       <cms:set related_author_id = k_last_insert_id />
  41.       <cms:set populated_authors__array. = _input_author_hash scope='parent' />
  42.  
  43.       <cms:put "populated_authors__array.<cms:show _input_author_hash />" "[]" is_json='1' scope='parent' />
  44.       <cms:put "populated_authors__array.<cms:show _input_author_hash />.page_id" related_author_id scope='parent' />
  45.       <cms:put "populated_authors__array.<cms:show _input_author_hash />.name" _input_author scope='parent' />
  46.  
  47. </cms:if>
  48.  
  49.  
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement