KenJuma Posted January 23, 2013 Share Posted January 23, 2013 I'm new to php and my first assignment is to design a project that give a user synonyms to words and also allows the user to add words and their synonyms. The thesaurus doesnt have to be full of all the words, an example of two or three words would just be enough. I do not want to use a database since I want to fullu understand php before starting to interact with databases. Someone told me I could save the words in some file which will be accessed by the codes and when the user enters the new words, they would automatically be updated in the file. I was thinking of using arrays, any help..? Quote Link to comment https://forums.phpfreaks.com/topic/273541-thesaurus/ Share on other sites More sharing options...
Jessica Posted January 23, 2013 Share Posted January 23, 2013 You cannot "fully understand" PHP without interacting with a database. PHP and MySQL really go hand in hand, especially when you're talking about storing data. But yes, you can store data in a file. You can find many tutorials on file read/write if you google it. Quote Link to comment https://forums.phpfreaks.com/topic/273541-thesaurus/#findComment-1407742 Share on other sites More sharing options...
Andy123 Posted January 23, 2013 Share Posted January 23, 2013 In a "real" application, one would of course use a database for this, but I understand that you want to move forward slowly and not learn everything at once. As Jessica said, you would almost always be using a database for storage, so learning how to do this by file probably won't help you out that much in the future anyways. Using a database also makes it easier to make relationships between data such as words. For instance, one could make relationships between words that indicate that they are synonyms instead of replicating the words in PHP (avoiding this would probably get more complicated than it really has to be). Either way, here is a quick example of how you could use arrays in PHP for this. Surely there are better ways (especially if you use a database), but perhaps it is enough to get you started. $words = array( 'beautiful' => array('handsome', 'pretty', 'lovely'), 'funny' => array('comical', 'amusing', 'humorous', 'laughable'), 'clever' => array('smart', 'intelligent', 'ingenious') ); To save it to a file, you can can serialize the array (Google should help you out here). Each word has an array of synonyms. Imagine that for each of those words, you would have an array of all of the other synonyms. For instance, an entry for the word "smart" would have an array of "clever", "intelligent", "ingenious", and so on. You can use references, but I think you will end up overcomplicating things - and worse yet, you will probably not be able to leverage so much from the experience since it is probably far away from what you will face when doing "real" applications. So my advice is to keep it as close to how you would do it if you were paid to do it for someone. Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/273541-thesaurus/#findComment-1407756 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.