Solved

create report - selecting records

Posted on 2004-08-06
6
188 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:
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: 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:Marcus Bointon
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
count download link and run update query 9 56
Sql query on a varchar that is numeric. 8 27
html input type 3 22
Selecting SUM data from mysql table with PHP 4 6
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
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 …

920 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

15 Experts available now in Live!

Get 1:1 Help Now