Solved

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

Posted on 2008-06-10
14
235 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
The viewer will learn how to dynamically set the form action using jQuery.

688 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