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
Solved

if all rows found are equal to each other

Posted on 2013-11-20
4
265 Views
Last Modified: 2013-11-21
How can we use if statement inside the foreach loop below to check whether all the row values (no matter what their count is) returned by the mysql query are equal to each other?

$query = "SELECT field_qty_item FROM ...";
foreach ($query as $row) {

   if all $row values returned are equal to each other...
   // do smthng
   
}
else {
// do nothing... or do another thing...
}
0
Comment
Question by:sbayrak
4 Comments
 
LVL 32

Expert Comment

by:Daniel Wilson
ID: 39664471
There are many ways ... but this should work.
$FirstValSet = false;
$FoundMismatch = false;

$query = "SELECT field_qty_item FROM ...";
foreach ($query as $row) {
   if (!$FirstValSet){
    $val = $row[0];
    $FirstValSet = true;
  }else{
    if ($row[0] != $val){
      $FoundMismatch=true;
      break;
    }
  }
}
   if all $row values returned are equal to each other...
if (!$FoundMismatch){
   // do smthng
   
}
else {
// do nothing... or do another thing...
} 

Open in new window

0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39664564
$query = "SELECT count(distinct field_qty_item) FROM ...";

if result == 1 then
  #they are all the same
else
 #they're not
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39665291
What exactly are you trying to achieve. Maybe the IF statement isn't the best way forward.

You can have a GROUP BY clause in your SQL Statement:

SELECT count(field_qty_item) as Amount, field_qty_item FROM yourTable GROUP BY field_qty_item

Open in new window

You would then only get unique values of field_qty_item, along with an Amount of how many times each value exists in the database. If you only get 1 record returned then clearly all the rows match each other.
0
 
LVL 1

Author Closing Comment

by:sbayrak
ID: 39667005
smooth
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change background color in td if any value is 1 9 23
Correct syntax for subdirectories 9 18
issue with DB import 1 17
JQuery Search Filter 2 27
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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