web development - server-side processing
![GirlWithCookies](https://dakd0cjsv8wfa.cloudfront.net/images/photos/user/f638/c353/48d2/212e/a13d/6934/6b25/b9ed4682776b55bfa1b035bc90ad0c0f342d.jpg)
GirlWithCookies
Posts: 138 Member
Hi, all. I know myfitnesspal is a totally weird place to be asking for web development help, but I'm desperate. I need help with a PHP assignment. I take online classes and all I can do is email my professor, who won't teleconference with me because he thinks I don't actually need his help. I don't think the online tutoring my school provides covers PHP (they haven't gotten back to me). The assignment is due tomorrow night (Sunday). If anyone can offer help, feel free to respond to this thread or DM me. I won't explain the whole assignment right now, but it has to do with putting user input into a multidimensional array and traversing the array to check info. One specific question is: How do I write a multidimensional array to a .txt file?
0
Replies
-
I have a team which does this but you show up at the wrong time. Weekend. They don’t come until Monday or I can direct you to one of my guys. Bad timing, kid.0
-
Take the question and google it. How to put an array in a txt file. You will be surprised how much google can tell you0
-
Yes, I've done a lot of Googling, but can't find anything clear-cut that answers my specific questions. I really need someone to look at my code and to be able to communicate with someone.0
-
I can help you Monday. But not today 😭
Good luck. Have you tried Reddit?? You will get help rightaway
There is a lot of techie subs on Reddit0 -
Go to the sub called twoxchromosomes
Lot of girls code there. It is a 11million strong sub Reddit for women.
Or just go to the php sub itself.0 -
Thank you!0
-
Let me know how it goes0
-
What kind of data are you storing in the array?
You could always iterate through the array, concat each line with a delimiter, then copy that line to a string variable to store it temporarily (use a new line at the end of the line before you copy it to the storage variable).
Once you've finished your loop, hand the string variable with all the data to file_put_contents.
If you don't want to use delimited strings and can use a csv output, check out fputcsv.
The data parameter of that function is called fields and expects a list of items of type array. You basically open a file stream (write) and then as you get a list (line) you hand that line to fputcsv with the name of the file handle and the array/line variable name.
Php.net manual is your friend. 😁
0 -
This content has been removed.
-
This content has been removed.
-
What kind of data are you storing in the array?
You could always iterate through the array, concat each line with a delimiter, then copy that line to a string variable to store it temporarily (use a new line at the end of the line before you copy it to the storage variable).
Once you've finished your loop, hand the string variable with all the data to file_put_contents.
If you don't want to use delimited strings and can use a csv output, check out fputcsv.
The data parameter of that function is called fields and expects a list of items of type array. You basically open a file stream (write) and then as you get a list (line) you hand that line to fputcsv with the name of the file handle and the array/line variable name.
Php.net manual is your friend. 😁
0 -
-
What kind of data are you storing in the array?
You could always iterate through the array, concat each line with a delimiter, then copy that line to a string variable to store it temporarily (use a new line at the end of the line before you copy it to the storage variable).
Once you've finished your loop, hand the string variable with all the data to file_put_contents.
If you don't want to use delimited strings and can use a csv output, check out fputcsv.
The data parameter of that function is called fields and expects a list of items of type array. You basically open a file stream (write) and then as you get a list (line) you hand that line to fputcsv with the name of the file handle and the array/line variable name.
Php.net manual is your friend. 😁
It's "user input" with username, first name, last name, and email that needs to go into the multidimensional array in a "guest book" text file. My professor told me to try building the array like:
$Users = array (
array( "username", "nextusername"),
array("firstname", "nextfirstname"),
array("lastname", "nextlastname"),
array("email", "nextemail")
);
So each index of the same number in each array would correspond to one person, like $Users[0][0] / $Users[1][0] / $Users[2][0] / $Users[3][0] would be one user's info. So, how would I use file_put_contents to correctly append the sub-arrays within the text file? With all the ways I've tried, it still just puts each entry at the end of the text file, so the file is just one long string. When I use file() to read the text file as an array to try to traverse it, it doesn't seem to work. I should be able to use in_array or array_search to see if a submitted username already exists in the "guest book."0 -
GirlWithCookies wrote: »What kind of data are you storing in the array?
You could always iterate through the array, concat each line with a delimiter, then copy that line to a string variable to store it temporarily (use a new line at the end of the line before you copy it to the storage variable).
Once you've finished your loop, hand the string variable with all the data to file_put_contents.
If you don't want to use delimited strings and can use a csv output, check out fputcsv.
The data parameter of that function is called fields and expects a list of items of type array. You basically open a file stream (write) and then as you get a list (line) you hand that line to fputcsv with the name of the file handle and the array/line variable name.
Php.net manual is your friend. 😁
It's "user input" with username, first name, last name, and email that needs to go into the multidimensional array in a "guest book" text file. My professor told me to try building the array like:
$Users = array (
array( "username", "nextusername"),
array("firstname", "nextfirstname"),
array("lastname", "nextlastname"),
array("email", "nextemail")
);
So each index of the same number in each array would correspond to one person, like $Users[0][0] / $Users[1][0] / $Users[2][0] / $Users[3][0] would be one user's info. So, how would I use file_put_contents to correctly append the sub-arrays within the text file? With all the ways I've tried, it still just puts each entry at the end of the text file, so the file is just one long string. When I use file() to read the text file as an array to try to traverse it, it doesn't seem to work. I should be able to use in_array or array_search to see if a submitted username already exists in the "guest book."
If you're leaving the arrays intact, use fputcsv.
Been a minute since I've used multi-dimensional, but I think maybe you're mixing up the structure of your nested arrays in the dimensions.
$Users = array (
array( "username", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email")
);
Open a file pointer in write mode.
Iterate your users array with a foreach
Hand each user array item to fputcsv.
Fputcsv should be expecting the array for each user as a value passed to it (fields in the documentation) along with the file handle to use for the put.
From the horse's mouth: 😊
https://www.php.net/manual/en/function.fputcsv.php0 -
$Users = array (
array( "username", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email"),
array( "nextusername", "firstname", "lastname", "email")
);
This is the way I had it set up before, but my professor was the one who suggested I try it as
$Users = array (
array( "username", "nextusername"),
array("firstname", "nextfirstname"),
array("lastname", "nextlastname"),
array("email", "nextemail")
);
I'll probably just go back to array( "username", "firstname", "lastname", "email") and use fputcsv, like you said. What's bugging me is that the assignment instructions say "use the techniques you learned in this chapter," but the chapter doesn't mention fputcsv (and I couldn't find it in the textbook's index) and doesn't explain how to write a multidimensional array to a file.0 -
You can do it either way structurally. It just makes more sense to me to have all the same user's info in the same array, not one array of usernames, then one of firstnames, etc.
Some people do it that way, but I've seen it and do it more in grouped fashion where at the item level (in a nested array of not) the individual item I read is for one thing...whatever that thing is.....in other words, when you read the users item the way I said, you're essentially reading that user's info all in one iterations not having to piece it together from four different locations.
Were they wanting you to bust up the dimensions and they write the arrays?
You could always serialize them too.
If you organized the dimensions the way I said, you can then iterate in a for each and serialize the item. Copy that item (concat a new line to the serialized string) to a master string variable.
Then when you've finished iterating and fall out of your for each, hand that string variable to file_put_contents.
There's lots of ways you can get it to file.0
This discussion has been closed.
Categories
- All Categories
- 1.4M Health, Wellness and Goals
- 394.2K Introduce Yourself
- 43.9K Getting Started
- 260.4K Health and Weight Loss
- 176.1K Food and Nutrition
- 47.5K Recipes
- 232.6K Fitness and Exercise
- 437 Sleep, Mindfulness and Overall Wellness
- 6.5K Goal: Maintaining Weight
- 8.6K Goal: Gaining Weight and Body Building
- 153.1K Motivation and Support
- 8.1K Challenges
- 1.3K Debate Club
- 96.4K Chit-Chat
- 2.5K Fun and Games
- 3.9K MyFitnessPal Information
- 15 News and Announcements
- 1.2K Feature Suggestions and Ideas
- 2.7K MyFitnessPal Tech Support Questions