?
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
?
174 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
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 count occurrences of each item in an array.
Suggested Courses

801 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