Solved

create report - selecting records

Posted on 2004-08-06
6
192 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
[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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

632 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