Jump to content

Dex2k

New Members
  • Content Count

    9
  • Joined

  • Last visited

Community Reputation

1 Neutral

About Dex2k

  • Rank
    Newbie
  1. Guys dont call me dickhead but honestly I cannot go for the MONGO thingy I tried a litte bit and came to following result. Can somebody just help me finish it ? I hope it also helps some other people with same issue. it has still some bugs --> doesnt take create tables with most fields in it for each table that should be created . Normal should only create 1x query or each table(1x per parent, 1x per child) --> Puts a "," in the last input or create SQL function --> datafields not assigned to INSERT INTO function yet <?php $get_data = (file_get_contents('free_getIncursions.json')); $myArray = json_decode($get_data, true);//Convert json 2 array function plotTree_create($arr, $table=Null , $indent=0, $mother_run=true){ if ($mother_run) { // the beginning of plotTree. We're at rootlevel //echo "start\n"; } foreach ($arr as $k=>$v){ // skip the baseval thingy. Not a real node. if ($k == "__base_val") continue; // determine the real value of this node. $show_val = (is_array($v) ? $v["__base_val"] : $v); // show the indents echo str_repeat(" ", $indent); if ($indent == 0) { // this is a root node. no parents echo ")<br>Create $table ("; if (is_int($k)) { $k = "ID" ; } echo $k . " VARCHAR(50) , " ; } elseif (is_array($v)){ // this is a normal node. parents and children echo ")<br> Create $table.sub_"; echo $k ."(`ID` int(11), "; } else { // this is a leaf node. no children echo ""; if (is_int($k)) { $k = "Dummy_" .$k; } echo $k . " VARCHAR(50) , " ; } // show the actual node //echo $k . " (" . $show_val. ")" . "\n <br>"; if (is_array($v)) { // this is what makes it recursive, rerun for childs plotTree_create($v,$table ,($indent+1), false); } } if ($mother_run) { echo");"; // echo "end\n"; } } function plotTree_insert($arr, $table=Null , $indent=0, $mother_run=true ,$parent=0 ){ if ($mother_run) { // the beginning of plotTree. We're at rootlevel //echo "start\n"; } foreach ($arr as $k=>$v){ // skip the baseval thingy. Not a real node. if ($k == "__base_val") continue; // determine the real value of this node. $show_val = (is_array($v) ? $v["__base_val"] : $v); // show the indents echo str_repeat(" ", $indent); if ($indent == 0) { // this is a root node. no parents echo ");<br> INSERT INTO $table ($k, "; // echo " (ISPARENT) " ; $parent = $k; } elseif (is_array($v)){ // this is a normal node. parents and children echo ");INSERT INTO $table.sub_$k ($parent,"; //echo " (PARENTID) " ; } else { // this is a leaf node. no children echo ""; if (is_int($k)) { $k = "Dummy_" .$k; } echo " " . $show_val. "," ; } // show the actual node //echo $k . " (" . $show_val. ")" . "\n <br>"; if (is_array($v)) { // this is what makes it recursive, rerun for childs plotTree_insert($v,$table ,($indent+1), false, $parent); } } if ($mother_run) { echo ");"; } } plotTree_create($myArray,"Table1"); plotTree_insert($myArray,"Table1"); ?>
  2. P.S. I know the given example is in JS but the same should be makeable in PHP or not?
  3. ok if its not a good idea to flatten how do i get it to mysql relationship your array converted to json [{"0":"bossman","name":"Bob","address":[{"type":"home","city":"Los Angeles"},{"type":"work","city":"Beverly Hills"}],"employee_id":1,"spouse":{"name":"Linda"}},{"0":"flunky","name":"Bob, Jr","address":[{"type":"home","city":"Los Angeles"},{"type":"vacation","city":"Beverly Hills"}],"employee_id":121,"notes":"This is Bob's son!"}] please try it out to see what i mean http://pojo.sodhanalibrary.com/convertJsonToSQL.html
  4. Sorry for the emotions but I don't like it if I ask one question and somebody in the first post begins with oh my Think if we clean the "oh my" in both posts it wouldnt have gotten so emotional Sorry for that didn't meant to
  5. Oh my you haven't even posted a solution it's just use application x and then do it and of course change your whole database to mongo bongo Seems you have no idea I myself I am a beginner but atleast I try . I know the structure of a database and I am aware of sql injections but it's basically something I do at the end if really needed for my local running little code.
  6. function array_keys_recursive ($array,$d=0) { $flat = array(); foreach($array as $key => $value) { if (is_array($value)) { $flat = array_merge($flat, array_keys_recursive($value)); } else { if (is_int($key)) { $key = "Dummy" . $key . "`"; $flat[] = "`" . $key . " VARCHAR(50) "; } else{$flat[] = $key . " VARCHAR(50)";} } } return $flat; } sorry first function got corrupted somehow when editing
  7. both should be inserted or created if possible vial only one function anyways i even would be glad if i could get the inserts in one row for text.txt with all the tables . Atm they are seperated due to array one dataset aways cause of "for each" you can see from output . Creating works OUTPUT FOR CREATE TABLE: CREATE TABLE `table1` (type VARCHAR(50), state VARCHAR(50), influence VARCHAR(50), has_boss VARCHAR(50), faction_id VARCHAR(50), constellation_id VARCHAR(50), staging_solar_system_id VARCHAR(50), `Dummy0` VARCHAR(50) , `Dummy1` VARCHAR(50) , `Dummy2` VARCHAR(50) , `Dummy3` VARCHAR(50) , `Dummy4` VARCHAR(50) , `Dummy5` VARCHAR(50) , `Dummy6` VARCHAR(50) , `Dummy7` VARCHAR(50) , `Dummy8` VARCHAR(50) ); OUTPUT FOR INSERT TABLE (doesnt work !) need the dummy keys and values behind the second insert mean in one line!) INSERT INTO `table1` (`Dummy0``,`Dummy1``,`Dummy2``,`Dummy3``,`Dummy4``,`Dummy5``) VALUES ('30001410','30001411','30001412','30001413','30001414','30001415'); INSERT INTO `table1` (`type`,`state`,`influence`,`has_boss`,`faction_id`,`constellation_id`,`staging_solar_system_id`) VALUES ('Incursion','mobilizing','1','1','500019','20000207','30001414');
  8. function array_keys_recursive ($array,$d=0) { $flat = array(); foreach($array as $key => $value) { if (is_array($value)) { $flat = array_merge($flat, array_keys_recursive($value)); } else { if (is_int($key)) { $key = "Dummy" . $key . "`"; $flat[] = "`" . $key . " VARCHAR(50) "; } else{$flat[] = $key . " VARCHAR(50)";} } } return $flat; } function traverseArray($array,$table=NULL,$d=1) { $fields = array(); $values = array(); // Loops through each element. If element again is array, function is recalled. If not, result is echoed. foreach ($array as $key => $value) { if (is_array($value)) { traverseArray($value,$table,$d+1); // Or } else { if (is_int($key)) { $key = "Dummy" . $key . "`"; $fields[] = "`" . $key . "`"; } else {$fields[] = "`" . $key . "`";} $values[] = "'" . $value . "'"; $field_list = join(",", $fields); $value_list = join(",", $values); $query = "<br><br><br>INSERT INTO `" . $table . "` (" . $field_list . ") VALUES (" . $value_list . ");"; } } echo $query; } $get_data_json= (file_get_contents('test.txt')); // or the other file test2.txt $myArray = json_decode($get_data_json, true); $myArray2 = traverseArray($myArray,"table1"); $myArray3 = array_keys_recursive($myArray); $myArray3 = array_unique($myArray3); $query = "CREATE TABLE `table1` (" . implode (', ', $myArray3). ");<br>"; test.txt test2.txt
  9. Hey there Looked already through the whole internet Somebody has a function which goes through a nested php Array no matter what structure The function should analyse the data entries and lookup for the keys and values It all sounds easy but when I come to the first nest there's another nest in it I get a headache. At the end I want the php function to create or insert values according to the array structure into the MySQL tables. Wonder why I haven't seen and solution out there Output : Create table1 (struc1,struc2...) Or. Insert into table1 (struc1,struc2...) (val1,val2,...) I myself would be satisfied with that easy solution
×
×
  • 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.