create report - selecting records

Posted on 2004-08-06
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.

include "..\";
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,

Question by:jess_z0
  • 2
  • 2
LVL 25

Accepted Solution

Marcus Bointon 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:

Assisted Solution

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';

I think your isset 's are redundant
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.

LVL 25

Expert Comment

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


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.

Expert Comment

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).

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

820 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