Avatar of sabecs
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?

 
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 ; 
			}
}

Open in new window

PHP

Avatar of undefined
Last Comment
sabecs

8/22/2022 - Mon
Ray Paseur

You might use an array or package this in a function.
ASKER CERTIFIED SOLUTION
Juan Ocasio

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Juan Ocasio

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
sabecs

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
SOLUTION
Juan Ocasio

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

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","personalities","photos","videos","match" and the objects contain all the "active record" information about the user in question.  Just a thought, ~Ray
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...

Open in new window

Juan Ocasio

Oops.  I forgot to add the ', So it should be:

${'sql_users_'.$value}
Juan Ocasio

Hey thanks for the points.  Which solution worked for you?

${'sql_users_'.$value}

or

${sql_users_.$value}
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sabecs

ASKER
Thanks jocasio123 for your help,  {'sql_users_'.$value} worked for me.