Solved

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

Posted on 2013-01-25
2
153 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
2 Comments
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 500 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 108

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now