首先,因爲您已經有規則。先設置它。其次,你需要解析網址(得到國家並把它看作是一個slu)),並將其納入規則。第三,如果需要打印或不打印,則只需使用正常的foreach循環和內部條件(1/0或true/false)。考慮這個例子:
<?php
// $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url1 = 'www.mysite.com/us/mypage';
$url2 = 'www.mysite.com/canada/mypage';
$url3 = 'www.mysite.com/italy/mypage';
// dummy values
// setup the rules
$rules = array(
'us' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 1,
'textbox4' => 1,
),
'canada' => array(
'textbox1' => 1,
'textbox2' => 1,
'textbox3' => 0,
'textbox4' => 0,
),
'italy' => array(
'textbox1' => 1,
'textbox2' => 0,
'textbox3' => 1,
'textbox4' => 0,
),
);
// properly parse the url
$current_url = $url2; // i just chosen canada for this example
if (!preg_match("~^(?:f|ht)tps?://~i", $current_url)) {
$current_url = "http://" . $current_url;
}
$current_url = array_filter(explode('/', parse_url($current_url, PHP_URL_PATH)));
$country = reset($current_url);
?>
<!-- after getting the slug/country, loop it with a condition -->
<form method="POST" action="">
<?php foreach($rules[$country] as $key => $value): ?>
<?php if($value == 1): ?>
<label><?php echo $key; ?></label>
<input type="text" name="<?php echo $key; ?>" /><br/>
<?php endif; ?>
<?php endforeach; ?>
<input type="submit" name="submit" />
</form>
<!-- textbox1 and textbox3 should be the only inputs in here since i picked canada -->
你能跟我們分享一下你試過的嗎? –