I want to check if the data is match between array and the string.
Im trying to check if the string is equals to each other but the problem is the condition is returning false and both value of $subex2[1] and $subdat is IT100. I think the problem is you can't compare an array to a string. Can someone help me about this?
here's the code
$subex = $objPHPExcel->getActiveSheet()->getCell('D8')->getValue();
$subex2 = explode(":", $subex);
$q = "select * from class where id='$id'";
$r = mysql_query($q);
$data = mysql_fetch_array($r);
$subdat = $data['subject'];
if($subdat == $subex2[1]) {
echo "Data matched";
}else {
echo "Data doesn't matched";
}
As mentioned in the comments. One value has an space before it. You can solve this kind of problems like this:
if(trim($subdat) == trim($subex2[1])) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
for case sensitive issue, this trick should apply.
if(strtolower(trim($subdat)) == strtolower(trim($subex2[1]))) {
echo "Data matched";
} else {
echo "Data doesn't matched";
}
this test works fine
$subdat = "IT100";
$subex2[1] = "IT100";
if($subdat == $subex2[1]) {
echo "Data matched";
}
You are not comparing same string
You first use array_map and trim values, delete space, next use in_array, example
$subex2 = array_map('trim',$subex2);
if( is_array($subex2) ){
if(in_array($subdata, $subex2)){
echo "Data matched";
} else {
echo "Data doesn't matched";
}
}
It's always good to check if it's actually an array, with is_array
Reference in_array https://www.w3schools.com/php/func_array_in_array.asp
try this:
$isMatched = strval($subex2[1]) === strval($subdat) ?: false;
Related
I'm trying to get a n else statement working with a print_r such that if there's no value it outputs "no values".
In the code I'm getting values from json converted to an array.
The logic I'm trying to achieve is
IF fieldTag contains "i" THEN output the content associated with it
ELSE says its empty.
Right now blank is outputted as opposed to "no values".
Thanks
for($b=0; $b<count($res['entries'][$i]['bib']['varFields']); $b++) //loop thru the varFields
{
if($res['entries'][$i]['bib']['varFields'][$b]['fieldTag'] == "i")
{
$subfieldText2 = $res['entries'][$i]['bib']['varFields'][$b]['subfields'][0]['content']."<br>";
if(count($subfieldText2) > 0) {
print_r($subfieldText2);
} else {
echo "no values";
}
}
}
count() is for arrays, not strings, the way to get the length of a string is with strlen(). And if you want to check for an empty string, just compare it with $var == "", you don't need to get the length.
But you're concatenating "<br>" to the value, so the length will never be zero. You could check the length before concatenating.
$subfieldText2 = $res['entries'][$i]['bib']['varFields'][$b]['subfields'][0]['content'];
if($subfieldText2 != "") {
$subfieldText2 .= "<br>";
print_r($subfieldText2);
} else {
echo "no values";
}
And to avoid having to repeat that long expression to access the field, you could use foreach
for($res['entries'][$i]['bib']['varFields'] as $field) {
if ($field['fieldTag'] == "i") {
$subfieldText2 = $field['subfields'][0]['content'];
...
}
}
this worked for me thanks everyone
$subfieldText2="not detected";
echo "ISBN: ";
for($b=0; $b<count($res['entries'][$i]['bib']['varFields']); $b++) //loop thru the varFields
{
if($res['entries'][$i]['bib']['varFields'][$b]['fieldTag'] == "i")
{
$subfieldText2 = $res['entries'][$i]['bib']['varFields'][$b]['subfields'][0]['content'];
echo $subfieldText2.", ";
}
}
echo $subfieldText2;
How can i check if 5 variables are the same or not the same in a simple way?
here's my sample code:-
$roster_1 = $_POST['roster_1'];
$roster_2 = $_POST['roster_2'];
$roster_3 = $_POST['roster_3'];
$roster_4 = $_POST['roster_4'];
$roster_5 = $_POST['roster_5'];
$checkcommon = array($roster_1 , $roster_2 , $roster_3 , $roster_4 , $roster_5);
if(array_sum($checkcommon) == count($checkcommon))
{
$errormsg = "All the same";
}
else
{
$errormsg = "not the same";
}
Is there anyone who can help me?...
You can check through array_unique() with count():-
if(count(array_unique($checkcommon)) ==1){
echo "All values are same";
}
Example:- https://eval.in/728091
Reference:- http://php.net/manual/en/function.array-unique.php
This is a repeat question of Check $_POST['id'] is numeric then do this if not do otherwise
But..
The answers given do not answer the question IMHO.
The question is testing a $_POST string to see if it is a number or contains non-numeric characters. Not change the code to use $_GET.
The original question is as such:
if (!empty($_POST['id'])) {
echo "empty";
} else {
if (is_numeric($_POST['id'])) {
echo "numeric!";
} else {
echo "not empty but not numeric how come?";
}
}
I have tried, is_numeric() , but since $_POST delivers the variable as a string, even if it is a number this is useless.
I have tried, if(!preg_match('#[^0-9]#',$id)), but I have no idea why this doesn't work.
ctype_digit() I believe still has a problem with seeing all things from $_POST as strings, so no good.
There has to be a way, but I'm lost on how to do this. And no I can't use $_GET.
UPDATE ANSWER!!! (TYPO!!!!! god dammit!!!!):
// test.php
// testing with $_POST['id'] from forum with id = 5 and another test where id = new
$id = $_POST['editid'] ;
echo "<br>---".$id."---<br>";
if (empty($id)) {
echo "<br>1: empty";
} else {
if (!is_numeric($id)) {
echo "<br>2: This is the number 5";
} else {
echo "<br>3: the must be the word new";
}
}
// test 2 ... ctype_digit
if (empty($id)) {
echo "<br>4: empty";
} else {
if (!ctype_digit($id)) {
echo "<br>5: This is the number 5";
} else {
echo "<br>6: the must be the word new";
}
}
// test 3 ...
if (empty($id)) {
echo "<br>7: empty";
} else {
if (!preg_match('#[^0-9]#',$id)) {
echo "<br>8: This is the number 5";
} else {
echo "<br>9: the must be the word new";
}
}
/**
result from 5
---5---
3: the must be the word new
6: the must be the word new
8: This is the number 5
results from "new"
**/
i need to use if statement inside my table
the value of the row is "Released" but i want to show Pending on my html table. how can i achieve it. this is my current code. what's wrong with my if statement?
if (strlen($row['signed']) == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen checks for string length. first check either signed is set in the array and then check if value is equal
if (isset($row['signed']) && $row['signed'] == "Released") {
echo "Pending";
}
else{
echo $row['signed'];
}
strlen() returns the length of the argument, so it returns an integer. You can check if value is equals to the string which you want something like this:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo "Released";
}
strlen() is used to count the length of the string, to check if it matches "Released", just use == to compare:
if ($row['signed'] == "Released") {
echo "Pending";
} else {
echo $row['signed'];
}
To find out is $row['signed'] is set, just use isset():
if (isset($row['signed'])) {
echo "Pending";
} else {
echo $row['signed'];
}
More information on isset(): http://php.net/manual/en/function.isset.php
More information on PHP operators: http://php.net/manual/en/language.operators.php
Try this:
if ($row['signed'] == "Released") {
$value = "Pending";
} else {
$value = "Released"
}
And add <?php echo $value; ?> in your table
I want to check if a certain field of all rows match a criteria.
So if all rows has in the Status field 'RUN' as value then echo "Success".
If there is one row with END as value then echo "Fail":
I'm guessing I need a loop and an IF statement ?
I was thinking something like this but it doesnt return anything:
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) == ("Run" ) {
echo "Success<br />";
} else
echo "Fail";
}
I don't want to echo each row, I want all rows to match a criteria then echo, if one doesn't match a criteria then echo as well.
This should work for you:
First of all you have to fetch all rows with mysqli_fetch_all(). After this I extract only the Stat column with array_column(). Then I just simply array_fill() an array with X values as you have in $states with the value "Run". And check if every value is equals.
$result = mysqli_fetch_all($source_selection, MYSQLI_ASSOC);
$states = array_column($result, "Stat");
if($states == array_fill(0, count($states), "Run")) {
echo "Success";
} else {
echo "Fail";
}
There are some error in the code and you can use the count to match -
$count = 0;
while($source_row = mysqli_fetch_array($source_selection)){
if ($source_row['Stat'] == "Run" ) {
$count++;
}
}
if($count != mysqli_num_rows($source_selection)) {
echo "Fail";
} else echo "Success";
For best performance you can do it directy on the SQL:
select count(*) from table where Stat <> 'Run';
And then test the returning value to check that is greater than zero.
To do it with php you should know that when you find an error you can stop the iterations. The code would look like that:
while($source_row = mysqli_fetch_array($source_selection)){
if ( $source_row['Stat'] != "Run" ){
$fail = true;
break;
}
}
if ($fail) {
echo "Fail";
} else
echo "Success";
}
Try this
$success=true;
while($source_row = mysqli_fetch_array($source_selection)){
if ( ($source_row['Stat']) != ("Run" ) {
$success=false;
}
}
if ($success) {
echo "Success";
} else {
echo "Fail";
}