2014-06-10 35 views
0

我試圖根據頁面的URL和數據庫中的先前設置來控制不同文本框的加載。如何根據url和數據庫數據加載不同的文本框?

例如:我有一個數據庫表,看起來像這樣:

db settings

和瀏覽www.mysite.com/us/mypage時,我想我的頁面看起來像 US

瀏覽時

www.mysite.com/canada/mypage

Canada

瀏覽 www.mysite.com/italy/mypage

Italy

的時候所以我想了解如何設計我的代碼

這樣。應該僅在客戶端處理,在頁面加載時使用javascript,還是應該使用服務器端的控制器進行處理。

謝謝!

+1

你能跟我們分享一下你試過的嗎? –

回答

0

必須設置條件和檢查值

if($isset($textbox) && $textbox==1) 
{ 
//print the label and text box 
} 
0

最好的辦法是嘗試才達到在服務器端的輸出。 我認爲下面的代碼會對你有用。

嘗試從url獲取國家/地區代碼,並將數據庫值獲取到$ textboxA數組中,然後運行以下代碼。

$textboxA = array(1,1,0,1); 
foreach($textboxA as $key => $value){ 
    switch($key){ 
     case 0: if($value) print $textbox1; 
     break; 
     case 1: if($value) print $textbox2; 
     break; 
     case 2: if($value) print $textbox3; 
     break; 
     case 3: if($value) print $textbox4; 
     break; 
     default: print ""; 
    } 
} 
1

首先,因爲您已經有規則。先設置它。其次,你需要解析網址(得到國家並把它看作是一個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 --> 
相關問題