Solved

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

Posted on 2013-01-25
2
162 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 31

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 109

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
hbo knew my windows software 4 62
How to pass JavaScript generated HTML text form values to PHP? 5 38
PHP Mail error 3 26
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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