PHP - reduce repetitive code

sabecs
sabecs used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016

Commented:
You might use an array or package this in a function.
Application Developer
Commented:
I agree:  An array may be your best bet:
<?php
$x=array("profile","hobbies","personalities","photos","videos","match");
foreach ($x as $value)
{
	if(isset($_POST['users_'. $value . '_upd'])){
			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 = "";
			$users_profile_upd_pic = $question ; 
			}
	} 
	
}

Open in new window

Juan OcasioApplication Developer

Commented:
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
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
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?
Juan OcasioApplication Developer
Commented:
Try

${sql_users_.$value}
Most Valuable Expert 2011
Top Expert 2016

Commented:
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 OcasioApplication Developer

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

${'sql_users_'.$value}
Juan OcasioApplication Developer

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

${'sql_users_'.$value}

or

${sql_users_.$value}

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial