Ferdinand Posted June 4, 2013 Share Posted June 4, 2013 Having these questions and answers hard coded. I want to be able to read them from a text file and load the arrays questions with questions from questions.txt while also read answers array from answers.txt. but at the same time maintain my data structures. How should I go abou this? <?php $questions = array('FTP','AJAX','RSS','XSS','PHP','W3C','XML','YUI','HTML','CGI','SSL','SQL','HTTP','CSS','SOAP','WAI','SSI','JSON','XSLT','WCAG'); $answers = array( // the correct answer for each question must be the first array item 0=>'item' array(0 =>'File Transfer Protocol','Force Through Privately','File Through Protocol','File Test Protocol'), array(0 => 'Asynchronous JavaScript and XML','All JavaScript and XML','Alternative Java and XML','Actual JavaScript and XML'), array(0 => 'Really Simple Syndication','Really Simple Scripting','Ready-Styled Scripting','Really Stupid Syndication'), array(0 => 'Cross-site Scripting','Cross-site Security','Cleverly Structured Scripting','eXtremely Safe and Secure'), array(0 => 'PHP: Hypertext Preprocessor','Post Hypertext Processor','Practical HTML Processing','Process HTML Prettily'), array(0 => 'World Wide Web Consortium','World Wide Web Committee','World Wide Web Creatives','Wakefield Willy Wavers Club'), array(0 => 'eXtensible Markup Language','eXtendable Markup Language','Crossover Markup Language','eXtreme Markup Language'), array(0 => 'Yahoo User Interface','Yahoo\'s Useful Idea','Yahoo Utility Interface','Yahoo User Interaction'), array(0 => 'Hypertext Markup Language','Human Markup Language','Helpful Markup Language','Hypertext Memory Language'), array(0 => 'Common Gateway Interface','Common or Garden Interaction','Computer\'s Graphical Intelligence','Common Graphical Interface'), array(0 => 'Secure Sockets Layer','Server Security Layer','Server Security Level','Secret Socket Layer'), array(0 => 'Structured Query Language','Stupid Query Language','Secure Query Language','Strict Query Language'), array(0 => 'Hypertext Transfer Protocol','Hypertext Traffic Protocol','HTML Traffic Transfer Protocol','HTML Through Traffic Protocol'), array(0 => 'Cascading Style Sheets','Custom Style Sheets','Clientside Style Sheets','Calculated Style Sheets'), array(0 => 'Simple Object Access Protocol','Structured Object Access Protocol','Simple, Objective And Private','Simply Obvious Access Principle'), array(0 => 'Web Accessibility Initiative','World Wide Accessibility Intiative','World Wide Accessibility Incorporation','Web Accessibility and Inclusion'), array(0 => 'Server-Side Include','Server-Side Intelligence','Scripted Server Include','Secure Server Include'), array(0 => 'JavaScript Object Notation','JQuery-Scripting Object Notation','Just Simple Object Notation','JavaScript Over the Net'), array(0 => 'eXtensible Stylesheet Language Transformation','eXpandable Stylesheet Language Transfer','eXtensible Stylesheet Language Transfer','eXtendable Stylesheet Language Transformation'), array(0 => 'Web Content Accessibility Guidelines','Wakefield Community Action Group','Web Criteria And Guidelines','World-wide Common Access Group') ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/ Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 Are you using objects or sticking with arrays? Also, why do you explicitly define an index for the first element in each answer array but not the other answers? Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1433980 Share on other sites More sharing options...
Ferdinand Posted June 4, 2013 Author Share Posted June 4, 2013 I want to stick with arrays. I am assigned thje fisrt index of the array to zero to use it later as the correct answer among the other answers for computation purposes. Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1433981 Share on other sites More sharing options...
cpd Posted June 4, 2013 Share Posted June 4, 2013 (edited) If you don't assign an index, php starts the indexing at 0 anyway so there's no need to explicitly set it. Answering your question directly: you can do what you're asking no problem but I'd keep the questions and answers together. They're related and shouldn't really be separated. Take a look at http://uk3.php.net/manual/en/ref.filesystem.php for file system functions. More specifically fopen($file), fgets($handle) and fclose($handle). A, what I consider as better, alternative is to create an XML model storing your data in that. You can then use the DOMDocument() object to parse the data for you and easily cycle through all your questions and answers. Food for thought. Edited June 4, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1433985 Share on other sites More sharing options...
Eiseth Posted June 4, 2013 Share Posted June 4, 2013 save them as csv like this File Transfer Protocol, Force Through Privately, File Through Protocol, File Test Protocol Asynchronous JavaScript and XML, All JavaScript and XML, Alternative Java and XML, Actual JavaScript and XML // other answer here then use fgetscsv to read the file $answers = array(); // Read the file and loop (use fopen, fgetcsv) // inside the loop, append the current array to answers (use array_push) Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1434056 Share on other sites More sharing options...
DavidAM Posted June 4, 2013 Share Posted June 4, 2013 If there is no requirement to edit the data outside of PHP, I would just use serialize and unserialize along with file_put_contents and file_get_contents file_put_contents($Filename, serialize($questions)); $questions = unserialize(file_get_contents($Filename); Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1434075 Share on other sites More sharing options...
Jessica Posted June 4, 2013 Share Posted June 4, 2013 Having these questions and answers hard coded. I want to be able to read them from a text file and load the arrays questions with questions from questions.txt while also read answers array from answers.txt. but at the same time maintain my data structures. How should I go abou this? Why? Why don't you just leave it as is...? Save those two arrays in a file and then include them. Quote Link to comment https://forums.phpfreaks.com/topic/278753-text-file-read/#findComment-1434106 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.