Solved

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

Posted on 2008-06-10
14
231 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
  • 6
  • 6
  • 2
14 Comments
 
LVL 11

Expert Comment

by:spoxox
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:firekiller15
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 11

Expert Comment

by:spoxox
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
what is variable $test you mention above??
0
 
LVL 11

Expert Comment

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

Author Comment

by:firekiller15
Comment Utility
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 250 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now