Solved

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

Posted on 2008-06-10
14
233 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
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

776 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