Rizigs Posted December 10, 2012 Share Posted December 10, 2012 Hi there everyone, i am new to PHP and trying my best to learn and hopefully be as good as you! I am looking for the following: There is a user form posts the value to code.php In code.php the value is retrieved and ran across a list If value is: abc then $value == 1a23; def then $value == 4b56; ghi then $value == 7c89; jkl then $value == 1d01; mno then $value == 1e23; pqr then $value == 1f45; so on... The only problem is the list is 12 thousand lines long, how can i do this in 1 if statement rather than 12 thousand if statements? Thank you so much for your time! Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 10, 2012 Share Posted December 10, 2012 The data would be in an array or a database. Then you would use the entered value to loopup/find the result. Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398564 Share on other sites More sharing options...
Rizigs Posted December 10, 2012 Author Share Posted December 10, 2012 The data would be in an array or a database. Then you would use the entered value to loopup/find the result. So each line would be a db entrance? then i would make a command that will look for $value then str_replace with the new value? Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398567 Share on other sites More sharing options...
mrMarcus Posted December 10, 2012 Share Posted December 10, 2012 (edited) If you had an array, you could do something as simple as: $arr = array( 'abc' => '1a23', 'def' => '4b56', 'ghi' => '7c89', 'jkl' => '1d01', 'mno' => '1e23', 'pqr' => '1f45' ); $key = 'abc'; if (array_key_exists($key, $arr)) { $value = $arr[$key]; } EDIT: It just depends on how you're going to go about storing the keys/values... database or array. Grabbing the value based on the input (abc, def, etc) can be a simple SELECT query: $key = 'abc'; // maybe coming from a form? $key = $_POST['key']; $sql = "SELECT `value` FROM `table` WHERE `key` = '". $key ."'"; Now you can process the query and `value` will hold the associated value to the corresponding key. Obviously just pseudo code, but I hope you catch the drift. Edited December 10, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398577 Share on other sites More sharing options...
Rizigs Posted December 10, 2012 Author Share Posted December 10, 2012 If you had an array, you could do something as simple as: $arr = array( 'abc' => '1a23', 'def' => '4b56', 'ghi' => '7c89', 'jkl' => '1d01', 'mno' => '1e23', 'pqr' => '1f45' ); $key = 'abc'; if (array_key_exists($key, $arr)) { $value = $arr[$key]; } EDIT: It just depends on how you're going to go about storing the keys/values... database or array. Grabbing the value based on the input (abc, def, etc) can be a simple SELECT query: $key = 'abc'; // maybe coming from a form? $key = $_POST['key']; $sql = "SELECT `value` FROM `table` WHERE `key` = '". $key ."'"; Now you can process the query and `value` will hold the associated value to the corresponding key. Obviously just pseudo code, but I hope you catch the drift. Thanks for the edit, would you recommend to store in db or use an array as listed? Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398583 Share on other sites More sharing options...
mrMarcus Posted December 10, 2012 Share Posted December 10, 2012 With ~12K possibilities, I would recommend a database table. Are the keys/values assigned to eachother permanently? E.g. will abc always equal 1a23? Or can abc have a different value at some point (perhaps based on other form input)? Do you currently have the entire list? If you do, I/we can assist in populating the table. Post the format in which you currently have the list saved (if you have it). Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398584 Share on other sites More sharing options...
Rizigs Posted December 10, 2012 Author Share Posted December 10, 2012 With ~12K possibilities, I would recommend a database table. Are the keys/values assigned to eachother permanently? E.g. will abc always equal 1a23? Or can abc have a different value at some point (perhaps based on other form input)? Do you currently have the entire list? If you do, I/we can assist in populating the table. Post the format in which you currently have the list saved (if you have it). Wow thank you very much! I believe that the best way to go is a DB as well (and yes the values are permanently same value). However i need to be easily adding values to the database, and i was thinking the best way is to make a textfield and explode all values. I can do a marco on the list so that it is in the order. What do you think? Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398628 Share on other sites More sharing options...
Christian F. Posted December 11, 2012 Share Posted December 11, 2012 If you're talking about how to enter the values into the system, and not how it should be stored in the DB, then using a textarea (or straight import from a file) would probably be the best way. Yes. Since you mention a macro, I reckon you already have it in a spreadsheet? If so, then just export it as CSV and MySQL can import it directly. The table itself need to have two fields (key and value), where each pair gets one row. Then it's a simple SELECT query to retrieve the correct value(s). Quote Link to comment https://forums.phpfreaks.com/topic/271827-need-php-assistance/#findComment-1398707 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.