Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Counting issue, only want it to count as 1, even if there are more values.

Posted on 2013-01-25
2
Medium Priority
?
182 Views
Last Modified: 2013-03-12
Good afternoon experts.

I'm trying to count different Misdemeanor and Felony charges in our jail system.  When someone is booked in jail, they are given a booking number, that booking number may have multiple charges associated with it.  If all the charges are Misdemeanor, then I want to count 1 misd count, if there are multiple misd and one felony charge in that booking, then that booking should count as a felony booking.

My current code that is counting each "CHARGE" separately (which is not desired) is below

	$sql3 = "select * from jms_03mast where jm3_booking_num = '".$row['jm1_active_booking']."'";
		$result3 = $myConn->query($sql3);
		while($row3 =& $result3->fetchRow(DB_FETCHMODE_ASSOC))
		{
			//this is a total count, per charge, could have multiple per booking....
			if($row3['jm3_felony'] == 'F' || $row3['jm3_felony'] == 'Y')
			{
				$felony++;
			}
			if($row3['jm3_felony'] == 'M' || $row3['jm3_felony'] == 'N')
			{
				$misd++;
			}
			else
			{
				$othermf++;
			}
		
		}
			

Open in new window


As you can see i'm looping through a charge table (jms_03mast) and counting each charge per booking... I'm not sure how I would do this and only get 1 count per booking...

As a quick example:

Booking#   Charge#   Felony/misd:
01               01            M
01               02            M
01               03            M
02               01            M
02               02            F

Open in new window


So in the above example, I would have a total count of '2' bookings and 1 felony booking (since booking 02 has a felony charge entered) and 1 misd booking.

I hope I have explained this well enough, its been giving me a headache trying to figure it out!

Thank you!
0
Comment
Question by:hej613
[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
2 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 total points
ID: 38820369
You can try this untested code. I assumed in your table the column which holds the booking id is called 'booking' (as in the table you posted above) so you have to update this value accordingly to your table column name for booking id

<?php
//SET A DEFAULT VALUE FOR BOOKING
$booking = "00";
$sql3 = "select * from jms_03mast where jm3_booking_num = '".$row['jm1_active_booking']."'";
		$result3 = $myConn->query($sql3);
		while($row3 =& $result3->fetchRow(DB_FETCHMODE_ASSOC))
		{
			//CHECK IF THE BOOKING IS THE SAME: IF NOT, WE UPDATE BOOKING ID FLAG
			if ($row['booking'] != $booking) $booking = $row['booking'];
			//this is a total count, per charge, could have multiple per booking....
			if($row3['jm3_felony'] == 'F' || $row3['jm3_felony'] == 'Y' && $row['booking'] == $booking)
			{
				$felony++;
			}
			if($row3['jm3_felony'] == 'M' || $row3['jm3_felony'] == 'N' && $row['booking'] == $booking)
			{
				$misd++;
			}
			else
			{
				$othermf++;
			}
		
		}
			
                                  

Open in new window


Cheers
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38822251
Please post the CREATE TABLE statement for this table.  Armed with that we may be able to give you some tested and working demonstration code.

My first guess is that you can GROUP BY the booking and ORDER BY felony/misd.  If the first row contains felony, you've got your answer (I think).  My second guess is that you would want to get a DBA involved to normalize your data base tables.
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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

597 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