Jump to content
#StayAtHome ×

Archived

This topic is now archived and is closed to further replies.

oriental_express

Final year project, please point me in the right direction

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites

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 :)

 

Share this post


Link to post
Share on other sites

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 :P)

Also... you might want to change name="sex" to name="answer" :)

 

 

Share this post


Link to post
Share on other sites

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 !!!!

Share this post


Link to post
Share on other sites

Misses doing dinner for you.... lucky bastard :P

 

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)

Share this post


Link to post
Share on other sites

Misses doing dinner for you.... lucky bastard :P

 

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

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

<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

Share this post


Link to post
Share on other sites

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 ? ;) )

Share this post


Link to post
Share on other sites

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 :o

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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...

 

 

Share this post


Link to post
Share on other sites

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) ?

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

Almost there, although this code has one problem...

Read more on arrays.

Share this post


Link to post
Share on other sites
<?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);
?>

Share this post


Link to post
Share on other sites

And he specifically asked NOT to give him the answer :P

 

And echo $quest will just show that $quest is an Array()

Share this post


Link to post
Share on other sites

fgetcsv returns array of values from csv file line.

Share this post


Link to post
Share on other sites

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.  :o

 

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 ?

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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....

Share this post


Link to post
Share on other sites

×
×
  • 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.