Jump to content

[SOLVED] Retrieving data from different parts of a code


Nymphetamine

Recommended Posts

:D 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!

:)

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.

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?

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.

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*

;D

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.