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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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 …

832 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