PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Can someone help me with the logic of this code especially the ifs what i want to achieve is for the script to select an .inc file (which holds a function) based on what the corresponding $_post[] is. Are my else's in place or are they not necessary
A switch statement is usually both simpler and faster than lots of if...elseif comparisons. Your use of $all[] I'm not sure of - are you adding all the POST values to an array? And if so how are you reference any one value?
Try making these all option boxes in your form - select one with name="region" to create a key $_POST['region'] you can compare to your allowed list of values. You can also add a default for non-matching values. If region was sent by URL you can use $_GET['region'] from URL like index.php?region=greater_accra.
switch($_POST['region'])
{
case 'greater_accra':
require_once 'by_region/genccadv_GH_gar.inc';
generate_Accra();
break;
case 'ashanti_region':
require_once 'by_region/genccadv_GH_ashR.inc';
generate_Ashanti();
break;
case 'central_region':
require_once 'by_region/genccadv_GH_centR.inc';
generate_CentralCoast();
break;
case 'western_region':
require_once 'by_region/genccadv_GH_westR.inc';
generate_Western();
break;
case 'eastern_region':
require_once 'by_region/genccadv_GH_eastR.inc';
generate_Eastern();
break;
case 'volta_region':
require_once 'by_region/genccadv_GH_voltaR.inc';
generate_Volta();
break;
case 'brong_ahafo':
require_once 'by_region/enccadv_GH_brongR.inc';
generate_BrongAhafo();
break;
case 'northern_region':
require_once 'by_region/genccadv_GH_northR.inc';
generate_Northern();
break;
case 'upper_west':
require_once 'by_region/genccadv_GH_uwestR.inc';
generate_UpperWest();
break;
case 'upper_east':
require_once 'by_region/genccadv_GH_ueastR.inc';
generate_UpperEast();
break;
default:
require_once 'by_region/genccadv_GH_gar.inc'; // default kicks in if no match found
generate_Accra();
}
Few things.
$_POST and other array keys use single-quotes - $_POST['key'] not $_POST["key"]. $_POST is itself an array of values - no need for $all[] = x type operations. You just need a specific parameter to grab from $_POST, in the case above I used $_POST['region'] which is a value set via a form.
Now if you were allowing multiple values which I think you are doing - then just check if $_POST['greater_accra'] is a valid value - maybe its being set to 1 in your form using checkboxes? The HTML for this check would tell us a lot more.