sabecs
asked on
PHP - reduce repetitive code
What is the best way to code below instead of just repeating six times and replacing the word "profile" with
"hobbies", "personality", "photos", "video" & "match" each time?
"hobbies", "personality", "photos", "video" & "match" each time?
if(isset($_POST['users_profile_upd'])){
if($_POST['users_profile_upd'] == "Y"){
$sql_users_profile = "AND profile.updated = '1'" ;
$users_profile_upd_pic = $tick ;
}
if($_POST['users_profile_upd'] == "N"){
$sql_users_profile = "AND profile.updated = '0'" ;
$users_profile_upd_pic = $cross ;
}
if($_POST['users_profile_upd'] == "A"){
$sql_users_profile = "";
$users_profile_upd_pic = $question ;
}
}
You might use an array or package this in a function.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
BTW: In my code snippet, I probably missed area where I should have substituted $value, you you'd ahve to go in and clean it up.
HTH
HTH
ASKER
Thanks for your help,
I keep getting errors, how should I code the following line?
$sql_users_profile = "AND profile.updated = '1'" ;
should it be something like this
$sql_users_.$value = "AND ".$value.".updated = '1'" ;
or something like this
$sql_users_.{$value} "AND {$value} updated = '1'" ;
I have tried both but they don't appear to work?
I keep getting errors, how should I code the following line?
$sql_users_profile = "AND profile.updated = '1'" ;
should it be something like this
$sql_users_.$value = "AND ".$value.".updated = '1'" ;
or something like this
$sql_users_.{$value} "AND {$value} updated = '1'" ;
I have tried both but they don't appear to work?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I was thinking of a more general design pattern. With things like this, it makes sense to have an internal array of the options and a one-to-one mapping of the keys in the POST array with the column names in the data base table. The code in the code snippet only translates Y into 1, N into 0 and A into something absent. This is not a very powerful piece of programming. Why not just let the right information come in from the POST array so no translation is needed?
Another way that this sort of thing might be handled would be with strtr().
http://us3.php.net/manual/en/function.strtr.php
You might also find the switch/case implementation to be useful.
http://us3.php.net/manual/en/control-structures.switch.php
You might consider using an array of objects, where the array keys are "profile","hobbies","perso nalities", "photos"," videos","m atch" and the objects contain all the "active record" information about the user in question. Just a thought, ~Ray
Another way that this sort of thing might be handled would be with strtr().
http://us3.php.net/manual/en/function.strtr.php
You might also find the switch/case implementation to be useful.
http://us3.php.net/manual/en/control-structures.switch.php
You might consider using an array of objects, where the array keys are "profile","hobbies","perso
if($_POST['users_'. $value .'_upd'] == "Y"){
$sql_users_profile = "AND profile.updated = '1'" ;
$users_profile_upd_pic = $tick ;
}
if($_POST['users_'. $value .'_upd'] == "N"){
$sql_users_profile = "AND profile.updated = '0'" ;
$users_profile_upd_pic = $cross ;
}
if($_POST['users_'. $value .'_upd'] == "A"){
$sql_users_profile = "";
... etc...
Oops. I forgot to add the ', So it should be:
${'sql_users_'.$value}
${'sql_users_'.$value}
Hey thanks for the points. Which solution worked for you?
${'sql_users_'.$value}
or
${sql_users_.$value}
${'sql_users_'.$value}
or
${sql_users_.$value}
ASKER
Thanks jocasio123 for your help, {'sql_users_'.$value} worked for me.