Solved

create report - selecting records

Posted on 2004-08-06
6
187 Views
Last Modified: 2006-11-17
I need some help with this code ASAP, i have multiple tables involved in the query because an athlete can have multiple sports, squads, or scholarships attached to them. AthleteID is the unqiue identifier for each athlete.

My code is below, the problem is,

1. i need it to display all athletes if no method of sorting if selected. At the moment is just says query is empty

2. When 2 search by fields are selected, eg sport and squad. I want it to show only athletes of the chosen sport AND squad. At the moment is shows me athletes that may only be in the squad and not sport.

3. I have a date of birth field in the athletes table, how can i search by a min and max age field.

Ok so prior to this code is the html with drop down boxes to select criteria to search by such as sport, squad, performance level etc etc

After this code is the checkbox code where the user select which data from the chosen athletes they wish to view, if you need to see any more code just let me know.

<?php
include "..\connect.inc";
echo"<link rel='stylesheet' href='../report.css' type='text/css'>";
$where_query = "";

if ( isset($_POST["performanceLevel"]) && $_POST["performanceLevel"] != "" )
  {
     $where_query .= empty($where_query) ? " SELECT AthleteID FROM Athletes WHERE performanceLevelID = '" . $_POST['performanceLevel'] . "'"
     : " UNION SELECT AthleteID FROM Athletes WHERE performanceLevelID = '" . $_POST['performanceLevel'] . "'";
  }
if ( isset($_POST["region"]) && $_POST["region"] != "" )
  {
         $where_query .= empty($where_query) ? " SELECT AthleteID FROM Athletes WHERE regionID = '" . $_POST['region'] . "'"
         : " UNION SELECT AthleteID FROM Athletes WHERE regionID = '" . $_POST['region'] . "'";
  }

if ( isset($_POST["school"]) && $_POST["school"] != "" )
 {
    $where_query .= empty($where_query) ? " SELECT AthleteID FROM Athletes WHERE schoolID = '" . $_POST['school'] . "'"
    : " UNION SELECT AthleteID FROM Athletes WHERE schoolID = '" . $_POST['school'] . "'";
 }
 
if ( isset($_POST["sport"]) && $_POST["sport"] != "" )
 {
    $where_query .= empty($where_query) ? " SELECT AthleteID FROM athletesports where SportID = '" . $_POST['sport'] . "'"
    : " UNION SELECT AthleteID FROM athletesports where SportID = '" . $_POST['sport'] . "'";

 }

if ( isset($_POST["squad"]) && $_POST["squad"] != "" )
 {
   $where_query .= empty($where_query) ? " SELECT AthleteID FROM athletesquads where squadID = '" . $_POST['squad'] . "'"
   : " UNION SELECT AthleteID FROM athletesquads where squadID = '" . $_POST['squad'] . "'";
 }
 
if ( isset($_POST["Scholarship"]) && $_POST["Scholarship"] != "" )
 {
   $where_query .= empty($where_query) ? " SELECT AthleteID FROM athletescholarships where ScholarshipID = '" . $_POST['Scholarship'] . "'"
   : " UNION SELECT AthleteID FROM athletescholarships where ScholarshipID = '" . $_POST['Scholarship'] . "'";
 }

Thanks really appreicate the help,

jess
0
Comment
Question by:jess_z0
  • 2
  • 2
6 Comments
 
LVL 25

Accepted Solution

by:
Squinky earned 125 total points
ID: 11740882
1) if ($query == '') $query = 'SEELCT athleteid FROM athletes';
2) Someone else can answer that!
3) there are examples for selecting age ranges in user notes here: http://dev.mysql.com/doc/mysql/en/Date_calculations.html
0
 
LVL 4

Assisted Solution

by:macgruder
macgruder earned 125 total points
ID: 11741273
Isn't it easier just to append extra ANDs to your queries for muliple fields

Start with

SELECT AthleteID FROM Athletes WHERE 1

and with each $_POST

append an

AND  athletescholarships where whateverID = '" . $_POST['whatever'] . "'";

So you end up with

SELECT AthleteID FROM Athletes WHERE 1 AND fooID='bar' AND foo2ID = 'bar2';
etc.

p.s.
I think your isset 's are redundant
just
if ( $_POST["squad"] )
should be sufficient. If something has a value it must be set. You could have an
if(isset($_POST['Submit']))at the start to check that the form was submitted.

0
 
LVL 25

Expert Comment

by:Squinky
ID: 11744690
I must say I really don't like the syntax:

isset($array['property'])

I much prefer:

array_key_exists('property', $array)

They are not the same at all: the first would return false, the second true for this array:

$array = array('property' => NULL);

Using isset also means that you're relying on isset's ability to suppress warnings for unset array indices, which is just plain ugly. If you really want to test for isset, then you should use array_key_exists first otherwise you have incomplete information. If you call isset and get a false result, you can't tell if it's there but set to null, or just not there. This is especially important for processing form data, as it's very common to get null values for submitted items. For example I often use things like this:

<input type="checkbox" name="item1" />

Notice that it doesn't have a value attribute, because it doesn't need one - it's sufficient to know that if it's checked, then it will be submitted (unchecked boxes are not submitted), but have null value, so checking for isset($_POST['item1']) would actually give an incorrect result.
0
 
LVL 4

Expert Comment

by:macgruder
ID: 12839218
I think my suggestion answered at least part 2 of the original question so the point should be shared (weighted to Squinky if possible).
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.

758 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

22 Experts available now in Live!

Get 1:1 Help Now