Jump to content

Need Php Assistance


Rizigs

Recommended Posts

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!

Link to comment
Share on other sites

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 by mrMarcus
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.