Solved

compare variable to array PHP

Posted on 2008-06-09
8
3,001 Views
Last Modified: 2013-12-13
Hello guys
i cannot find answer at the simple question how to compare variable to array.
for example i have some data from database
"one", "two", "three", "four", "five".
and i need to do:
if ($some_var = "two" and "three")
{
execute some code
}
is it possible to comapre $some_var with some array that i retreive from database without looping? just to get what i need...
0
Comment
Question by:nzrubin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:DrNikon224
ID: 21748162
You could use the in_array() function to find if the value of $someVar is contained in your $array. See http://www.php.net/in_array(). Also see simple sample below.

If you can show us what you're working with, we can help in a more specific manner. Can you share your code with us? What are you working with in your query results? What exactly are you trying to compare those results to?
<?
$someVar=3;
$array=array(1,2,3,4,5);
 
if(in_array($someVar,$array)){
     echo "$someVar is in the array.";
}//endif
?>

Open in new window

0
 
LVL 4

Author Comment

by:nzrubin
ID: 21748212
thanks for quick reply
i m trying to separate "customers" so they can see only particular projects, but sometimes customers can see the same projects:
for exml:
cust1 can see 1,2,3
cust2 can see 4
cust3 can see 1,3,4.




if ($roww->permission == 'Customer')
{
     $result_cust = mysql_query("SELECT * FROM pr_cust_permiss WHERE username = '$username'");
     while ($row_cust = mysql_fetch_object($result_cust))
      {
        $var = ???????????????????????? some array or something???;
      } // end while
//// select data for customers they can see only particular info which id located in "pr_cust_permiss" table
$result = mysql_query("SELECT * FROM pr_projects WHERE proj_id = '$var' ORDER BY proj_id DESC");
} //end if = customer,  else following
else
{
//// select all data
$result = mysql_query("SELECT * FROM pr_projects ORDER BY proj_id DESC");
}// end else
0
 
LVL 16

Accepted Solution

by:
DrNikon224 earned 50 total points
ID: 21748265
Sounds like you're looking for something like this:
if ($roww->permission == 'Customer')
{
     $result_cust = mysql_query("SELECT * FROM pr_cust_permiss WHERE username = '$username'");
     $var = array(); // create empty array
     while ($row_cust = mysql_fetch_object($result_cust))
      {
        array_push($var,$row_cust['proj_id']); // add each project id to the array
      } // end while
      $projects = implode(',',$var); // convert the completed array to a comma-separated string
//// select data for customers they can see only particular info which id located in "pr_cust_permiss" table
$result = mysql_query("SELECT * FROM pr_projects WHERE proj_id IN ($projects) ORDER BY proj_id DESC"); // use the IN comparison to find values in the DB which match a value in the $projects list
} //end if = customer,  else following
else
{
//// select all data
$result = mysql_query("SELECT * FROM pr_projects ORDER BY proj_id DESC");
}// end else

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 4

Author Comment

by:nzrubin
ID: 21748273
O MAN!!!!!!!!!!!!!! OSANA!!!!!!!!!!!!!!!!!!!!!!

thanks very much!!!!!!!!!!!!
0
 
LVL 16

Expert Comment

by:DrNikon224
ID: 21748280
On second thought, this will combine both queries and save the time of building the array in the while loop. Much more elegant, and quicker.
if ($roww->permission == 'Customer')
{
     $result = mysql_query("SELECT $ FROM pr_projects WHERE proj_id IN (SELECT proj_id FROM pr_cust_permiss WHERE username = '$username') ORDER BY proj_id DESC");
     
     
 
} //end if = customer,  else following
else
{
//// select all data
$result = mysql_query("SELECT * FROM pr_projects ORDER BY proj_id DESC");
}// end else

Open in new window

0
 
LVL 4

Author Closing Comment

by:nzrubin
ID: 31465615
thanks very much!
0
 
LVL 4

Author Comment

by:nzrubin
ID: 21748297
thats great! last one !!!
0
 
LVL 4

Author Comment

by:nzrubin
ID: 21748307
it works work!!!!!!!!!!!!!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question