Solved

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

Posted on 2013-01-25
2
167 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 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 110

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 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 …

687 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