user2669924
user2669924

Reputation: 85

fetching and separating single column data from database

I am trying to display only user selected pages in dashboard

I have two columns in database like this

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

in userid column i will have the logged in user name and in scripts column, names of the pages which they want to display it in dashboard in comma separated format. ex: total, cust_orders,...

I want to fetch page name from scripts column separately like total.php, cust_orders.php... i tried doing like this

$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];

    }

but it wont work as the pages can be from 1 to 8, can somebody please help me in this?

EDITED

I have done like this

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

$arr= $myrow['scripts'];

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

and it worked, it displays like this

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

but dynamically how can i separate it and i have to add .php to this ...

Upvotes: 0

Views: 2474

Answers (2)

bhawin
bhawin

Reputation: 285

$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
}

Upvotes: 1

Martin
Martin

Reputation: 3286

You can use PHP - explode like:

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

So $scripts will then contain each PHP-Page/Skript as own position in the array.

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

Upvotes: 0

Related Questions