Jump to content

[SOLVED] Retrieving Data from Multiple Tables???


kemper

Recommended Posts

I am still somewhat new at this stuff, been able to modify code to my likings, but am starting to write my own script for my schedules.  I am having difficulty trying to build a relationship with different tables.

 

MY TABLES:

CREATE TABLE `f2007_divisions` (

  `division_id` int(5) unsigned NOT NULL auto_increment,

  `age` text NOT NULL,

  `division` text NOT NULL,

  PRIMARY KEY  (`division_id`)

 

CREATE TABLE `f2007_scheduling` (

  `club_id` smallint(3) NOT NULL default '0',

  `status` text NOT NULL,

  `division` int(5) NOT NULL default '0',

  `game_id` int(6) unsigned NOT NULL auto_increment,

  `date` date NOT NULL default '0000-00-00',

  `notes` text NOT NULL,

  `time` time NOT NULL default '00:00:00',

  `home_team` varchar(50) NOT NULL default '',

  `h_score` text NOT NULL,

  `field` text NOT NULL,

  `field_no` text NOT NULL,

  `visit_team` varchar(50) NOT NULL default '',

  `v_score` text NOT NULL,

  `sched_user` smallint(5) unsigned NOT NULL default '0',

  PRIMARY KEY  (`game_id`),

  KEY `division_id` (`division_id`),

 

How do I build the relationship so that when I write:

<?php

// mySQL Table
$division_id = $_GET['division_id'];
$sql = "SELECT * FROM `f2007_scheduling` WHERE division='$division' ORDER BY date ASC";
$result = mysql_query($sql) or die(mysql_error());
$i = 0;


echo "</p>
<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>
<tr>
	<td><b><font size='1'>Division:</font></b></td>
	<td><b><font size='1'>Game ID:</font></b></td>
	<td><b><font size='1'>Date:</font></b></td>
	<td><b><font size='1'>True Time:</font></b></td>
	<td><b><font size='1'>Teams:</font></b></td>
	<td><b><font size='1'>Field:</font></b></td>
</tr>\n";
while ($data = dbarray($sql)) {
	$i % 2 == 0 ? $tclass='tbl1' : $tclass='tbl2';

	echo "<tr>
	<td class='$tclass'><font size='1'>".$data['division']."</font></td>
	<td class='$tclass'><font size='1'>".$data['game_id']."</font></td>
	<td class='$tclass'><font size='1'>".$data['date']=date("D, M j, Y",strtotime($data['date']))."</font></td>
	<td class='$tclass'>
	<p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['time']."</font></p></td>
	<td class='$tclass'><p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['home_team']." 
vs.</font></p>
	<p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['visit_team']."</font></font></td>
	<td class='$tclass'><font color='#0000FF' size='1'>".$data['field']." ".$data['field_no']."</font></td>
</tr>\n";
}
echo "</table>
?>

I can have "division" from f2007_division table display in place of ".$data['division']."?

 

This will probably give me a basic idea to make my database more efficient, moving teams into their own table for proper autoamtic standings generation also.

Link to comment
Share on other sites

First, this line is wrong. Your WHERE doesn't match the variable you've named:

 

$sql = "SELECT * FROM `f2007_scheduling` WHERE division='$division' ORDER BY date ASC";

 

should be:

 

$sql = "SELECT * FROM `f2007_scheduling` WHERE division='$division_id' ORDER BY date ASC";

Link to comment
Share on other sites

OK, so my script should be:

<?php

 

// mySQL Table

$sql = "SELECT * FROM `f2007_scheduling` WHERE division='$division' ORDER BY date ASC";

$result = mysql_query($sql) or die(mysql_error());

$i = 0;

 

 

echo "</p>

<table width='100%' cellpadding='0' cellspacing='1' class='tbl-border'>

<tr>

<td><b><font size='1'>Division:</font></b></td>

<td><b><font size='1'>Game ID:</font></b></td>

<td><b><font size='1'>Date:</font></b></td>

<td><b><font size='1'>True Time:</font></b></td>

<td><b><font size='1'>Teams:</font></b></td>

<td><b><font size='1'>Field:</font></b></td>

</tr>\n";

while ($data = dbarray($sql)) {

$i % 2 == 0 ? $tclass='tbl1' : $tclass='tbl2';

 

echo "<tr>

<td class='$tclass'><font size='1'>".$data['division']."</font></td>

<td class='$tclass'><font size='1'>".$data['game_id']."</font></td>

<td class='$tclass'><font size='1'>".$data['date']=date("D, M j, Y",strtotime($data['date']))."</font></td>

<td class='$tclass'>

<p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['time']."</font></p></td>

<td class='$tclass'><p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['home_team']."

vs.</font></p>

<p style='margin-top: 0; margin-bottom: 0'><font size='1'>".$data['visit_team']."</font></font></td>

<td class='$tclass'><font color='#0000FF' size='1'>".$data['field']." ".$data['field_no']."</font></td>

</tr>\n";

}

echo "</table>

?>

How do I link up the division table?

Link to comment
Share on other sites

Read the lesson here and here,

 

this will display both tables

SELECT * FROM f2007_scheduling, f2007_division WHERE division='$division' ORDER BY date ASC

 

this will display both tables where division is $division

SELECT * FROM f2007_scheduling, f2007_division WHERE f2007_scheduling.division='$division' AND  f2007_division.division_id = '$division' ORDER BY date ASC

 

as for joins

nice example here

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.