Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

html - PHP implode explode comma separated values for use in if else statement

I am working on a project where I need to enter comma separated values in a database field and then extract the data to use in if and else statement.

For example: in the database field user_status i have usernames like admin,admin1,admin2,admin3........etc. I want to use these values like:

$user_status = $row['user_status']; // database values of all the usernames
$username    = $_SESSION['username']; // username of the logged in user eg. admin

if($user_status == $username) // if session username matches with a username in the database field
{
  statements;
}else{
  statements;
}

I want to extract the comma separated database values from the database and match if the logged in user's username matches with one of many username's in the database. But I am not pretty sure how to do this. Please help me experts.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Create an array with explode and use in_array to check if the $username exists in the $user_status array.

<?php
$username    = $_SESSION['username'];
$user_status = explode(',', $row['user_status']);

if (in_array($username, $user_status)) {
    //...
}

http://php.net/manual/en/function.explode.php

http://php.net/manual/en/function.in-array.php


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...