raila Posted June 16, 2007 Share Posted June 16, 2007 I do see what you meen, but i don't get the result i whant from that. But i may have missed completly on the bitt of code..... Here is what i want to happen: I as making a smal quiz. the question file looks like this: (submitted by users) <q> What is your name? <a1> Thomas <a2> Roger <a3> John --end of file-- I want to make a script that identify each line ass a question(q) or an alternative(a1,a2,a3......) and buts the line into a string so i can print it on the page in the right order. can anyone help me out here? Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/ Share on other sites More sharing options...
MasterACE14 Posted June 16, 2007 Share Posted June 16, 2007 do you want to use a MySQL Database for this as well? or pure PHP? Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275790 Share on other sites More sharing options...
raila Posted June 16, 2007 Author Share Posted June 16, 2007 pure php please :-) Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275791 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2007 Share Posted June 16, 2007 ok. Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275794 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 Hello, I do not get a clear idea about your requirement. What I understood is : Read the line of strings and identify it as a question or an answer and print it? Could you please post the strings u need to be processed ? Regards Aniesh Joseph anieshjoseph@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275795 Share on other sites More sharing options...
raila Posted June 16, 2007 Author Share Posted June 16, 2007 this was an replypost to the original post.. http://www.phpfreaks.com/forums/index.php/topic,145320.msg621675.html#msg621675 but something fishy happend so it posted as new topic.. belive it will be mutch clearer if you all read that first :-) Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275798 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2007 Share Posted June 16, 2007 hmm ok, sorry I can't help their. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275801 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 Hello, Make two tables named tableQuestions & tablesAnswers with the following field. tableQuestions -------------- id Question 1 What is your name? 2 Your favourite place? tablesAnswers ------------- id questionID answers 1 1 Thomas 2 1 Roger 3 1 John 4 2 US 5 2 UK 6 2 India I think u got the right way to start. Regards Aniesh Joseph anieshjoseph@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275802 Share on other sites More sharing options...
raila Posted June 16, 2007 Author Share Posted June 16, 2007 File to bee read: <q> what is your name? <a1> Thomas <a2> Roger <a3> John Result i want: Question: What is your name? ($question = What is your name?) posible answers: Alternative 1: Thomas ($a1 = Thomas) Alternative 2: Roger ($a2 = Roger) Alternative 3: John ($a3 = John) Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275803 Share on other sites More sharing options...
redarrow Posted June 16, 2007 Share Posted June 16, 2007 1 min before we all start pogramming this have u got the database setup as advised please. Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275805 Share on other sites More sharing options...
MasterACE14 Posted June 16, 2007 Share Posted June 16, 2007 nah he said he doesn't want to use a database. ---------------------------- Question: What is your name? ($question = What is your name?) What the answer is. $answer1 = "John"; ($answer1 = $a3) Alternative 1: Thomas ($a1 = Thomas) Alternative 2: Roger ($a2 = Roger) Alternative 3: John ($a3 = John) if($answer1 = $a3) { echo("Correct, the answer was $answer1."); } else { echo("Wrong, the answer was not $answer1."); } Something along those lines. and of course you will have the form coding and what not to work with it. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275806 Share on other sites More sharing options...
aniesh82 Posted June 16, 2007 Share Posted June 16, 2007 hello, Please copy the following lines of code. This reads a file named "data.txt" with the content /* File Name :: data.txt */ <q> what is your name? <a1> Thomas <a2> Roger <a3> John /* File Name :: show.php */ <? error_reporting(E_ERROR ^ E_WARNING); $handle = @fopen("data.txt", "r"); if ($handle) { while(!feof($handle)) { $buffer = fgets($handle, 4096); $pattern = '/<a(.*)>(.*)/'; $no_of_matches = preg_match($pattern,$buffer,$matches); if($no_of_matches) { $answer = $matches[2]; $key = $a.$matches[1]; $array[$key] = $answer; } $qPattern = '/<q>(.*)/'; $noMatches = preg_match($qPattern,$buffer,$qMatches); if($noMatches > 0) { $question = $qMatches[1]; } } fclose($handle); echo "<br> Questions is: ".$question; foreach($array as $key => $val) { echo "<br> Answer $key: $val"; } } ?> I code it very fastly so if u find any bugs, pls reply Regards Aniesh Joseph anieshjoseph@gmail.com Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275820 Share on other sites More sharing options...
raila Posted June 16, 2007 Author Share Posted June 16, 2007 seems to work perfectly:-) Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/55830-hmm/#findComment-275826 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.