Nymphetamine Posted August 10, 2008 Share Posted August 10, 2008 Hey hey I'm going to have a sections of my website where people enter a code, which would have previously been given to them through the website. From this code I want to retrieve data from different parts of it... For example, if the code was "ADBYZX" ... and the user has submitted it through a form... I want to do something like: <?php $code = $_POST['code'] if ($code=="A*****") $data1="something"; elseif ($code=="B*****") $data1="somethingelse"; etc... if ($code=="*A****") $data2="something"; elseif ($code=="*B****") $data2="somethingelse"; etc... Hope that's clear enough, I want to be targetting specific bits of the code, is that possible? Thanks alot! Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/ Share on other sites More sharing options...
ratcateme Posted August 10, 2008 Share Posted August 10, 2008 i think you should look at preg_match() Scott. Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612665 Share on other sites More sharing options...
Fadion Posted August 10, 2008 Share Posted August 10, 2008 This should work the way u have it. People enter the code in a form, u validate the code (through a database table or just an array) and u redirect (or whatever) them to that part of the site u want. <?php $codeslist = array('AAA', 'BBB', 'CCC'); if(isset($_POST['entercode'])){ $code = $_POST['entercode']; if(in_array($code, $codeslist)){ if($code == 'AAA'){ //do smth } elseif($code == 'BBB'){ //do another thing } } else{ echo 'The code doesnt exist'; } } ?> Thats pretty basic as far as im understanding this. Otherwise u should explain it better. EDIT: Ok sorry i missunderstood it. Didnt note the "A***" and "*A***". The previous post should help. Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612666 Share on other sites More sharing options...
Nymphetamine Posted August 10, 2008 Author Share Posted August 10, 2008 Thanks for the help guys, I looked at preg_match()... looks cool but wouldn't there be a conflict if there were 2 of the same letters? For example if the code was "BBAYZX", and I got preg_match() to look for "B", it would find it twice wouldn't it? Or is there something I'm missing? Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612673 Share on other sites More sharing options...
Fadion Posted August 10, 2008 Share Posted August 10, 2008 As im not good with regex, i can suggest a string method. If you are searching just for a letter inside the code, lets say for the C in "ADCDE", u can always use: <?php $code = 'ABCDE'; if($code[2] == 'C'){ //do smth } ?> That should make it too conditional but i cant think of smth else. My 2 cents. Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612676 Share on other sites More sharing options...
Nymphetamine Posted August 10, 2008 Author Share Posted August 10, 2008 Hmm... I've looked at a load of pages about regex, and kinda make sense of it (i think)... But can't find one which targets the first or second or whatever letter within a series of letters. Maybe it isn't possible? *awaits a guru to arrive on the scene* Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612689 Share on other sites More sharing options...
ratcateme Posted August 10, 2008 Share Posted August 10, 2008 i dont now regex very well at all but you could use substr like this $first_letter = substr($code,0,1); switch($first_letter){ case 'A': //do something break; case 'B': //do something break; } $second_letter = substr($code,1,1); switch($second_letter){ case 'A': //do something break; case 'B': //do something break; } Scott. Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612691 Share on other sites More sharing options...
Nymphetamine Posted August 10, 2008 Author Share Posted August 10, 2008 Ahhhhhhh thank you! I didn't use exactly like you did but substr works great. Thanks ^^ Link to comment https://forums.phpfreaks.com/topic/118980-solved-retrieving-data-from-different-parts-of-a-code/#findComment-612709 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.