spidermann Posted January 9, 2009 Share Posted January 9, 2009 Hi folks, I need to be able to add, edit, and delete arrays from a multidimensional array stored in an external file, and am unsure the proper direction to take. Here's what I have so far. The stored arrays, named pages_custom.php: <?php $lang = array( 'pages' => array( 'default' => array( 'mast' => 'The Default Page', 'text' => '<h2>This is the default page.</h2><p>This is the default page\'s content</p>', ), '0' => array( 'mode' => 'foo', 'mast' => 'The Foo Page', 'text' => '<p>This is the foo page\'s content</p>', ), '1' => array( 'mode' => 'bar', 'mast' => 'The Bar Page', 'text' => '<p>This is the bar page\'s content</p>', ), ), ); ?> Now, I have the following function worked out, which prints the values of the stored arrays, but now what? I need to be able to manipulate the data; for example, to make a dropdown list of the 'mode' keys, and then be able to edit the corresponding 'text' value. <?php include 'pages_custom.php'; $content = ""; function foreach_loop($array) { global $content; foreach ($array as $key => $value) { if (!is_array($value)) { $content['key'] .= $key . nl2br("\n"); $content['value'] .= $value . nl2br("\n"); } else { foreach_loop($value); } } } foreach_loop($lang); print $content['value']; ?> What do I need to do to be able to manipulate the data, so that the keys and values stay together? A good push in the right direction would help out tremendously. If you need to know more about what I will be doing with the data, just ask. Thanks! Link to comment https://forums.phpfreaks.com/topic/140173-what-is-the-proper-way-to-get-and-use-array-data/ Share on other sites More sharing options...
Brian W Posted January 9, 2009 Share Posted January 9, 2009 $content['value'] .= $value . nl2br("\n"); to something like $content['value'] .= "<input type=\"text\" name=\"".$key."\" value=\"".$value."\">" . nl2br("\n"); The above though will only get you the text fields to modify also, isn't nl2br("\n") a little redundant? Couldn't it just be "<br>"? this is what I was messing with $content = ""; function foreach_loop($array) { global $content; foreach ($array as $key => $value) { if (!is_array($value)) { $content['key'] .= $key . nl2br("\n"); $content['value'] .= $key.": <input type=\"text\" name=\"".$key."\" value=\"".$value."\"><br>"; } else { $content['value'] .= "<ul>"; foreach_loop($value); $content['value'] .= "</ul>"; } } } foreach_loop($lang); print $content['value']; Link to comment https://forums.phpfreaks.com/topic/140173-what-is-the-proper-way-to-get-and-use-array-data/#findComment-733514 Share on other sites More sharing options...
spidermann Posted January 9, 2009 Author Share Posted January 9, 2009 Thanks for the info. I should have been more specific, I don't need to edit the content directly like you have shown, I need to pass the data to (I think) a variable to be used in another function. Kind of like $mode for the 'mode's and $text for the 'text's, but I need to call each as it's own unique value. I'm not sure how to ask the question, so I'll give an example. I want to have a dropdown list with the 'mode' keys' value listed (foo and bar in the above example), then when selected, that mode's text will go to a textbox for editing, so the modes and texts have to correspond to each other. And I also need to be able to add and delete each whole array (like all of the '0' => array), not just it's content. So I think I need to pass the data to variables, but I haven't come across any examples that explain what I need to do to achieve this. Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/140173-what-is-the-proper-way-to-get-and-use-array-data/#findComment-733536 Share on other sites More sharing options...
Brian W Posted January 9, 2009 Share Posted January 9, 2009 Something like this will get you a drop down menu of the keys with the values as the value. include 'pages_custom.php'; $content = ""; function foreach_loop($array) { global $content; global $i; global $menu; foreach ($array as $key => $value) { if (!is_array($value)) { $content['key'] .= $key . nl2br("\n"); $content['value'] .= $value . nl2br("\n"); $menu[$i] = array($value, $key); $i++; } else { foreach_loop($value); } } } foreach_loop($lang); //print $content['value']; echo "<select name=\"menu\">"; foreach($menu as $option){ echo "<option value=\"".$option[0]."\">".$option[1]."</option>"; } echo "</select>"; //Below for testing echo "<br>".nl2br(htmlentities(print_r($menu, TRUE))); As for modifying the array, I don't know how you plan on doing that. The array is hard coded it looks like, so you'll need to use fopen() and fwrite() and quite a bit of coding. Do you have access to mysql? That would make this easy. Link to comment https://forums.phpfreaks.com/topic/140173-what-is-the-proper-way-to-get-and-use-array-data/#findComment-733550 Share on other sites More sharing options...
spidermann Posted January 9, 2009 Author Share Posted January 9, 2009 That's closer, but it returns 'mode', mast', and 'text' in the drop for each array, what I need is for 'foo' and 'bar' (and 'default' too, if at all possible) to be passed -- the values of 'mode', so one can select the "page" they want to edit. And upon selection of the "page" ('mode'), the corresponding text ('text') will be passed to a textbox for editing. But your solution is very close. I have a complex example where file names from a directory are used in the list, and that seems pretty straightforward, but I don't know how to do it with an array from one file. For modifying the array, something like this should work (from PHP Manual): $file = fopen($filename, 'r+b); // binary update mode ... rewind($file); fwrite($file, $my_stuff); fflush($file); ftruncate($file, ftell($file)); ... fclose($file); But I haven't gotten that far yet. And I'd like to stay away from the database, if at all possible -- I realize it would be easier, but I guess I like doing it the hard way. Just kidding, I want it to be hard coded so it can be edited directly, without having to recover the data from the DB, if that makes any sense. Just so the end user has the choice to edit the file by hand. Thanks again. Link to comment https://forums.phpfreaks.com/topic/140173-what-is-the-proper-way-to-get-and-use-array-data/#findComment-733579 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.