?
Solved

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

Posted on 2008-06-10
14
Medium Priority
?
236 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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