2013-08-27 149 views
0

我想顯示在儀表板上只有用戶選擇的頁面分離單列數據獲取並從數據庫

我在數據庫中兩列這樣

create table users(userid varchar(50), scripts varchar(100)) 
userid column

我將有登錄用戶名稱和scripts column,他們想要在儀表板中以逗號分隔的格式顯示它的頁面的名稱。 例如:總,cust_orders,...

我想從腳本列抓取頁面名稱分別像total.php, cust_orders.php... 我試圖做這樣的

$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' "; 

$result = DB_query($sql,$db); 
$myrow = DB_fetch_array($result); 


foreach ($myrow as $res) 
    { 

     $array123[] = $res; 
     $var123 = $array123[0]; 
     $var222 = $array123[1]; 

    } 

,但它不會工作作爲網頁可以從1至8,有人可以幫我嗎?

編輯

我曾經做過這樣的

$result = DB_query($sql,$db); 
$myrow = DB_fetch_array($result); 

$arr= $myrow['scripts']; 

$arr1 = explode(',', $myrow['scripts']); 
print_r ($arr1); 

和它的工作,它會顯示這樣的

Array ([0] => total_dashboard [1] => customer_orders [2] => unpaid_invoice [3] => lat 

但動態我怎麼能分開呢,我要加.php對此...

+0

爲什麼不歸你的方案? – Strawberry

+3

如果腳本是以逗號分隔的列表,那麼一旦您的數據返回到php中,您可以使用arr = explode(',',$ myrows ['scripts']); – jeff

+0

@jeff:我試過了,但它沒有工作 – user2669924

回答

1
$sql = "select scripts from users where userid = '".$_SESSION['UserID']."' "; 
$result = mysql_query($sql,$db); 
while($myrow = mysql_fetch_array($result)) 
{ 
    $arr=explode(',',$myrow["scripts"]);//this will strip the , separated values in an array 
    //now you can fetch the scripts from database 
} 
+0

是的,但我怎樣才能爲每個頁面添加.php。因爲我必須分開吶..我沒有得到 – user2669924

+0

@ user2669924-:請詳細說明你沒有得到 – bhawin

+0

@ user2669924-:你必須有與所有這些腳本相關的PHP頁面,只需將它們包括在儀表板中PHP包括 – bhawin

0

您可以使用PHP - explode像:

while ($myrow = mysql_fetch_object($result)) 
{ 
    // $myrow->scripts = "text1.php,text2.php,text3.php"; 
    $scripts = explode(',', $myrow->scripts); 
} 

所以$scripts然後將包含每個PHP頁/ Skript作爲數組中自己的位置。

$scripts[0] = "text1.php"; 
$scripts[1] = "text2.php"; 
$scripts[2] = "text3.php"; 
+0

但如果用戶選擇多3頁,那麼它是如何工作的?它應該動態地發生吶,我沒有得到 – user2669924

+0

'$ scripts'是動態的。如果他選擇了3,5或0頁,則不需要。 – Martin