oriental_express Posted November 28, 2008 Author Share Posted November 28, 2008 not yet now you should learn how to create a html form you should do something like this <?php $file = fopen("questions.txt","r"); $line = fgetcsv($file); fclose($file); ?> <html> <head></head> <h1>Question: <?php echo $line[1]; ?></h1> <body> <!-- here you should create your form using html tags </body> </html> Note how the question is being output within html. I just reopen <?php tag and use echo. You can do the same with any variable. Thats great I'll wack up a html form of what i think it should be and get back to you <h1>Question: <?php echo $line[1]; ?></h1> Am slightly amazed its that simple Watch this space Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701325 Share on other sites More sharing options...
oriental_express Posted November 28, 2008 Author Share Posted November 28, 2008 Hello again this is what I've produced <?php $file = fopen("questions.txt","r"); $line = fgetcsv($file); fclose($file); ?> <html> <head></head> <h1>Question: <?php echo $line[1]; ?></h1> <body> <form> <input type="radio" name="sex" value="male"> <?php echo $line[2]; ?> <br> <input type="radio" name="sex" value="female"> <?php echo $line[4]; ?> <INPUT TYPE=SUBMIT VALUE="submit"> </form> </body> </html> Using that example where you echoed the question into the H2 tags I assummed this was going to happen for the selection choices. Where it says value male / female I know that will be whether it goes to question 2 or 3 1, "How old are you?" , "10", 2 ,"11", 3 2, "Do you like sports?", "yes", 4 , "No", 5 3, "Do you like music?", "Yes", 6 , "No" , 7 4, "www.aaa.com" 5, "www.bbb.com" 6, "www.ccc.com" 7, "www.ddd.com" Could you confirm this is correct Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701329 Share on other sites More sharing options...
Mchl Posted November 28, 2008 Share Posted November 28, 2008 Ah... W3CSchools example Why don't you try and see? Yes. That should do the trick (It's been some time since I used radio buttons, so I might be wrong ) Also... you might want to change name="sex" to name="answer" Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701337 Share on other sites More sharing options...
oriental_express Posted November 28, 2008 Author Share Posted November 28, 2008 yahoooooo yes it works and is displaying the possible choices thanks for the hint. I have an idea what i should be working on next. which is the name variable and the value ? Is that right ? if yes can you give a hint please. is it possible to do a form action back to the same php script ? ??? ps the misses just made dinner so be back asap. Hope to hear from you soon if your gone. Thank you again !!!! Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701342 Share on other sites More sharing options...
Mchl Posted November 28, 2008 Share Posted November 28, 2008 Misses doing dinner for you.... lucky bastard Yes, you can set form action to same script (actually I think that if you leave it blank it deafults to current script) Next you should handle the values submitted from form using $_POST or $_GET (whichever you've chosen) Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701355 Share on other sites More sharing options...
oriental_express Posted November 28, 2008 Author Share Posted November 28, 2008 Misses doing dinner for you.... lucky bastard Yes, you can set form action to same script (actually I think that if you leave it blank it deafults to current script) Next you should handle the values submitted from form using $_POST or $_GET (whichever you've chosen) nah not really lucky, the deal is I give her a weekly allowance when i get rich to go shopping for bags and shoes lol Could you explain what you mean by leaving it blank ? i plan to use get method When you say handle the value is this where if statements come in ? Im thinking where it is : <input type="radio" name="answer" value="male"> <?php echo $line[2]; ?> <input type="radio" name="answer" value="female"> <?php echo $line[4]; ?> would the value male be something like $line [3]. Whta the correct way to write in php as a value instead of echoing ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701361 Share on other sites More sharing options...
DeanWhitehouse Posted November 28, 2008 Share Posted November 28, 2008 You should read some PHP tutorials, maybe ones based around form processing. He says empty like <form action="" method="post"> See the action is empty and therefore it will post to the current script. And yes where you handle the value will is where if statements and probably more will come. But we are not here to do your homework (assignments) for you, we can provide references and point you in the right direction etc. Although Mchl doesn't seem to mind. Thanks, Blade280891 (Mr.Grumpypants) Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701363 Share on other sites More sharing options...
oriental_express Posted November 28, 2008 Author Share Posted November 28, 2008 Thank you for reply and reference but Im not asking anyone to do my assignment. I do want to do this my self, i just need guidance and understanding. PHP tutorial has helped me understand what it is but it hasn't taught me to think like a programmer. I mean the easist thing was <?php echo $line[1]; ?> as a question but how is a person meant to know that. After he showed that I was able to work out that you do the same for the choices Thank you Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701365 Share on other sites More sharing options...
oriental_express Posted November 29, 2008 Author Share Posted November 29, 2008 <html> <head></head> <h1>Question: <?php echo $line[1]; ?></h1> <body> <FORM action="" method="get"> <input type="radio" name="answer" value="<?php $line[3]; ?> "> <?php echo $line[2]; ?> <br> <input type="radio" name="answer" value="<?php $line[5]; ?>"> <?php echo $line[4]; ?> <INPUT TYPE=SUBMIT VALUE="submit"> </form> </body> </html> This is the code so far. I want to say something like move to next row in the csv file if $line[3] is chosen when submitted. Is this possible ? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701406 Share on other sites More sharing options...
Mchl Posted November 29, 2008 Share Posted November 29, 2008 This is the code so far. I want to say something like move to next row in the csv file if $line[3] is chosen when submitted. Is this possible ? Thanks Code looks good... as to your question... no really. Each time script is being executed it's loses all information from previous execution. So it doesn't know it loaded first line the previous time, and will load it again. That's why I suggested loading the whole file into an array, where you could just use array indexes to move from line to line. Work on it Blade: I do mind I could code this script in an hour or so and post it here. Instead I'm giving off small bits of information hoping that oriental_express will do some thinking and research on his own (hear that o_e ? ) Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701634 Share on other sites More sharing options...
oriental_express Posted November 29, 2008 Author Share Posted November 29, 2008 thanks for reply. if loading it all into an array then the format of the txt file will be one straight long line ? although not ideal to a user that just needs to edit te txt file it will have to do. I will think of a way to re write the txt file. be back asap the misses is in labour Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701688 Share on other sites More sharing options...
Mchl Posted November 29, 2008 Share Posted November 29, 2008 No. The file should be as it is now. You just need to find a way, to load each line into a separate variable (and store those lines in yet another array) Hint: Look at example #1 here Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-701694 Share on other sites More sharing options...
laffin Posted November 30, 2008 Share Posted November 30, 2008 Been following the progess. Michl doing a great job as per the references. As stated the format of the file is fine. But u need to parse the file for the index ya have in it either reading in the whole file into an array. or parsing the file until u get to the index ya need to be at. and for that u will need a loop structure. the form looks good, but now ya need to grind out the logic to the engine. Look at yer csv file. Remember fgetcsv only grabs 1 line at a time from the file. Ya still need, $_POST routine in order to get the user input as well... Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702110 Share on other sites More sharing options...
oriental_express Posted December 1, 2008 Author Share Posted December 1, 2008 hello there I'm back I think its this <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions = array($data); } fclose($handle); ?> I bet i'm wrong. Please don't tell the answer just yet. What should I look at more ? Are there any other examples that I could try to use to figure out ? Is it correct to to try and build an arrary like so $questions = array($data) ? Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702728 Share on other sites More sharing options...
laffin Posted December 1, 2008 Share Posted December 1, 2008 u can look at the data structure, the code looks good <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions = array($data); } fclose($handle); print_r($questions); ?> the print_r function is a great diagnostic tool when dealing with arrays. so ya can visually see yer data, now with that as a tool, ya can build more logic on getting to wut ya want from the data. ya still need the form processing... Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702827 Share on other sites More sharing options...
Mchl Posted December 1, 2008 Share Posted December 1, 2008 Almost there, although this code has one problem... Read more on arrays. Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702832 Share on other sites More sharing options...
DeanWhitehouse Posted December 1, 2008 Share Posted December 1, 2008 <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions[]= $data; } fclose($handle); foreach($questions as $quest) { echo $quest; } print_r($questions); ?> Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702879 Share on other sites More sharing options...
Mchl Posted December 1, 2008 Share Posted December 1, 2008 And he specifically asked NOT to give him the answer And echo $quest will just show that $quest is an Array() Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-702884 Share on other sites More sharing options...
DeanWhitehouse Posted December 1, 2008 Share Posted December 1, 2008 Is it multi-dimensional then? Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-703207 Share on other sites More sharing options...
Mchl Posted December 1, 2008 Share Posted December 1, 2008 fgetcsv returns array of values from csv file line. Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-703211 Share on other sites More sharing options...
DeanWhitehouse Posted December 1, 2008 Share Posted December 1, 2008 Ahh ok, then i will allow him to fix the code i posted. Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-703217 Share on other sites More sharing options...
oriental_express Posted December 2, 2008 Author Share Posted December 2, 2008 I appreciate you guys helping me out. Just that my daughter was born last night and I haven't had much time to catch up or sleep. Can you think of a way to load whole file into an array called $questions so that $questions[0] will have first line stored $questions[1] will have second line stored etc? I hope this is it <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions[]= $data; $question1 = $questions[0]; $question2 = $questions[1]; $question3 = $questions[2]; $question4 = $questions[3]; $question5 = $questions[4]; $question6 = $questions[5]; $question7 = $questions[6]; } fclose($file); print_r($question6[1]); ?> If im not mistaken, each line is now a variable with arrays ? Am I right ? <html> <head></head> <h1>Question: <?php echo $line[1]; ?></h1> <body> That reminds me, this will no longer work so ? Does that mean I'm going to have many seperate pages ? Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-703665 Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 Another father on the block! Cheers! I hope this is it <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions[]= $data; $question1 = $questions[0]; $question2 = $questions[1]; $question3 = $questions[2]; $question4 = $questions[3]; $question5 = $questions[4]; $question6 = $questions[5]; $question7 = $questions[6]; } fclose($file); print_r($question6[1]); ?> Err.. not really... <?php $file = fopen("questions.txt", "r"); while (($data = fgetcsv($file, 1000, ",")) !== FALSE) { $questions[]= $data; } fclose($file); print_r($question[6][1]); // <---- see here - a multidimensional array. //First number says which line from csv, //second number says which column from csv ?> No. You will not need many separate pages... You need just one, but with smart script creating it Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-704152 Share on other sites More sharing options...
oriental_express Posted December 2, 2008 Author Share Posted December 2, 2008 what have I essentially done ? $questions[]= $data; $question1 = $questions[0]; as far as I understand $questions1 is it not a variable for line 1 just clues please. many thanks Michael Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-704192 Share on other sites More sharing options...
Mchl Posted December 2, 2008 Share Posted December 2, 2008 Yes, but... The way you did it it would work for no more than 7 questions. and You could do it outside of while() Arrays are the smart way, to avoid lists of variables like $question1, $question2, $question3.... Quote Link to comment https://forums.phpfreaks.com/topic/134533-final-year-project-please-point-me-in-the-right-direction/page/2/#findComment-704208 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.