Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to add string the querie if the value is not found in the condition

Posted on 2008-06-10
14
Medium Priority
?
237 Views
Last Modified: 2008-06-12
PHP
How to add string the querie if the value is not found in the condition
say i have
$a = array(123457,546789,"Is not a students",245678)//is studentID
2007 --> intake

if($a != "is not a student")
query = mysql_query("select getitem_std_Address("2007", "$a");
else
$a = "is not a student";

is this code correct can i get is not a student is $a==is not a student
can "is not a student" read from array?
0
Comment
Question by:firekiller15
[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
  • 6
  • 6
  • 2
14 Comments
 
LVL 11

Expert Comment

by:spoxox
ID: 21756883
If

$a = array(123457,546789,"Is not a students",245678);

then

$a[2] == "Is not a students"



Does this help?
0
 

Author Comment

by:firekiller15
ID: 21757316
i thought if use statement if you have to use boolean

like example
if(a==b)
{do this}
else
{do that}

are you sure above code is correct
0
 
LVL 4

Expert Comment

by:afzz
ID: 21757525
if $ a is an array use the following code
if(!in_array("is not a student",$a)){
query = mysql_query("select getitem_std_Address("2007", "$a");
}
else{
//The following is wrong since $a is an array. It should be like $a[2]="is not a student"; which is already the case as we have compared above. do something else here
//$a = "is not a student";
echo "is not a student;
}

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:firekiller15
ID: 21757793
ya $ a is an array but why the outcome still like previous example

$a = array(123457,546789,"Is not a students",245678)//is studentID
$b =  array(2007,2008,2009) --> intake


if(!in_array("is not a student",$a)){
query = mysql_query("select getitem_std_Address("$b", "$a");
}
else{
echo "is not a student;
}

it will output is not a student why like that

0
 
LVL 4

Expert Comment

by:afzz
ID: 21757821
my code is just an example. Remove echo "is not a student; from that  and remove the extra s in the array "Is not a students"
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21760061
I don't know what you need to know.

! is a logical operator that means "NOT".

! $a        is TRUE if $a is not TRUE.
http://ca.php.net/manual/en/language.operators.logical.php

Your sample code (with syntax errors fixed, and with matching strings in array and IF statement - this seems to be what you're asking):

$a = array(123457,546789,"is not a student",245678); // missing semicolon
if(!in_array("is not a student",$a)){
query = mysql_query("select getitem_std_Address("$b", "$a");
}
else{
echo "is not a student"; // missing closing quote
}

This ECHOes "is not a student" because
in_array("is not a student",$a) is true; therefore
! in_array("is not a student",$a) is false.

if (expression) {
//statements to execute if true (expression evaluates to nonzero value)
}
else {
//statements to execute if false (expression evaluates to 0 value)
}


NOTE also string matching like this is case sensitive.
$a = array(123457,546789,"is not a student",245678);
in_array("is not a student",$a) is true
in_array("IS not a student",$a) is false
0
 

Author Comment

by:firekiller15
ID: 21765710
in the
$a = array(123457,546789,"is not a student",245678); // missing semicolon
that mean it will read the first element = 123457 right?

so if  if(!in_array("is not a student",$a)){ --> this one mean if "is not a student in array" then do query = mysql_query("select getitem_std_Address("$b", "$a"); right?
hence 123457 is not "is not a student in array" why still in go to echo "is not a student"; // missing closing quote



0
 
LVL 11

Expert Comment

by:spoxox
ID: 21765810
in_array looks at all elements in the array to find the first parameter.

$inThisArray = array(123457,546789,"is not a student",245678);
$canYouFindThisValue = 123457;
in_array($canYouFindThisValue, $inThisArray); //true

$canYouFindThisValue = "is not a student";
in_array($canYouFindThisValue, $inThisArray); //true

More here: http://ca.php.net/in_array

So:

$a = array(123457,546789,"is not a student",245678);

in_array(123457, $a) == true
in_array(546789, $a) == true
in_array("is not a student", $a) == true
in_array(245678, $a) == true

Does this help?
0
 

Author Comment

by:firekiller15
ID: 21765940
Is that mean everytime i want to compare a value in the array i need to write a code for it?

if say in my array i have 4 element i need to write
4 time if statement?
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21765990
You can use in_array to search an entire array for one value.

You can check each value in an array in a loop:
$a = array(...);
for ($i = 0; $i < sizeof($a); $i++)
  if ( $a[$i] == $test)
    do something....

You can compare two arrays to get the different elements:
 array array_diff  ( array $array1  , array $array2  [, array $ ...  ] )
Compares array1 against array2 and returns the difference.
$array3 = array_diff($array1, $array2)

You can compare two arrays to get the common elements:
  array array_intersect  ( array $array1  , array $array2  [, array $ ...  ] )
array_intersect() returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
$array3 = array_intersect ($array1, $array2)


php offers many other array functions
http://ca.php.net/array
0
 

Author Comment

by:firekiller15
ID: 21766106
what is variable $test you mention above??
0
 
LVL 11

Expert Comment

by:spoxox
ID: 21766344
$test is a variable representing a value that we are looking for in the array.
0
 

Author Comment

by:firekiller15
ID: 21766403
you mean like this
$a = array(123457,546789,"is not a student",245678); // missing semicolon
$test = "is not a student";
$a = array(...);
for ($i = 0; $i < sizeof($a); $i++)
  if ( $a[$i] == $test)
    do something....

can i just get the value from array without assign to variable
like
if ( $a[$i] == "is not a student"
0
 
LVL 11

Accepted Solution

by:
spoxox earned 1000 total points
ID: 21770807
you mean like this
$a = array(123457,546789,"is not a student",245678);
$test = "is not a student";
for ($i = 0; $i < sizeof($a); $i++)
  if ( $a[$i] == $test)
    do something....

yes


can i just get the value from array without assign to variable
like
if ( $a[$i] == "is not a student"  )

yes
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 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