View Single Post
  #1 (permalink)  
Old 08-04-2005, 08:07 AM
daddyg daddyg is offline
WebProWorld New Member
 
Join Date: Apr 2005
Location: UK
Posts: 8
daddyg RepRank 0
Default Table layout using php/mysql to extract golf statistics

I'm trying to produce a table layout to display the finish position stats like this: http://www.golfbettingguide.com/cont...le-layout.html

The above is the layout I want. The problem I'm having is putting it all together when I'm actually extracting the finishing position results from my mysql database for two golfers.

I'm using a foreach loop to extract the finish position results for each golfer in turn. However my first attempt hasn't produced the result I'm looking for.

When I enter two golfers names in the text box the output is displayed in two seperate tables, rather than one table with a column for each golfers results as per the page in the link at the top.

You can see what I mean here (excuse the messy layout for the form, it's just a test page) if you type in Tiger woods and Ernie els for example:
http://www.golfbettingguide.com/cont...guide-test.php

Here's what my php code looks like:

Code:
if(isset($_POST['golferentry#1']))
{
    
    $golfers = array();  //create empty array of golfers - see pg 110 of sitepoint book
    $golfers[0] = $_POST['golferentry#1'];         //assign first golfer entered into first index of $golfer  
    $golfers[1] = $_POST['golferentry#2'];         //assign first golfer entered into first index of $golfer
    $tour = $_POST['tour'];
    
    
    $ctr = 0;    //set this to 0 to check if first time through foreach loop
    
    foreach($golfers as $golfer)
    {
        $result = @mysql_query("SELECT G.Golfer, T.T_Date, T.Tournament, F.Position FROM tbl_tournaments AS T,
        tbl_golfers AS G LEFT OUTER JOIN tbl_finish_pos AS F ON T.ID = F.TournamentID AND F.GolferID = G.ID
        WHERE T.Tour = '$tour' AND G.Golfer = ('$golfer') ORDER BY T.ID DESC, G.Golfer DESC LIMIT 10 ");
    
        if (mysql_num_rows($result) == 0)
        {
             echo ("



The golfer name $golfer does not exist in the database. Please check your spelling or try with a different name</p>
");
            
        }
        


        
        else if($result && ctr == 0) // if first iteration of foreach loop and valid result, echo top of table
        {

        
            echo ("<table id=\"tour_results\" cellpadding=\"0\" cellspacing=\"1\">
            
                    <tbody><tr>
                                <th colspan=\"4\" id=\"golf_form_title\">Golfer Form Guide</th>
                    </tr>

                    <tr class=\"golf_form_hdr\">

                        <td rowspan=\"2\" class=\"width_15pc\">Date</td><td rowspan=\"2\" class=\"width_35pc\">Tournament</td><td colspan=\"2\" class=\"width_50pc tops\">Finish Positions for:</td>
                    </tr>
                    
                    
                    <tr>
                      <td class=\"tops\">$golfer</td>
                 ");            
        }
        
        else {
                        die('

Error performing query: ' . mysql_error() . '</p>');
                }
        
        if(mysql_num_rows($result)!=0 && ctr == 1)          //if 2nd iteration of foreach loop and valid result, echo 2nd golfer name
        {    
                                    echo("<td class=\"tops\">$golfer</td></tr>");                     
        }    else if(ctr == 1)
            {
                        echo("<td class=\"tops\"></td></tr>");
            }
        
        
        
    
            
        
            
            while ($row = mysql_fetch_array($result) )
            {
                $Golfer = $row['Golfer'];
                $fp = $row['Position'];
                $Tournament = $row['Tournament'];
                $TournamentDate = $row['T_Date'];
                
                if(!isset($fp))
                {
                  // $var is null (does not exist at this time) and the execution goes here
               $fp = "-";
                }
                
            
                if(ctr == 0)
                {
                    echo('<tr><td class="odds_even">' . date('d-m-y', strtotime($TournamentDate)) . '</td>');
                    echo("<td class=\"odds_even\">$Tournament</td><td class=\"odds_even\">$fp</td>");
                }
                else
                {
                    echo("<td class=\"odds_even\">$fp</td></tr>");
                }
                
                            
                      
            }        
        
        
    $ctr++;         //increment ctr to set it to 1
    }  //end of foreach loop
    
    
    
            echo '</table>';
        
                    
}

else {
        echo ("


A golfer name must be entered in the first box.  Please try again...</p>
");
    }

?>
In writing the code I was trying to achieve these conditions :-
i) The first text box is mandatory but the second is optional, ie you can only enter one golfers name if you want but it must be in the first text box.
ii) If either of the golfers names is mispelt then no results are published, just a validation error telling the user to check the spelling.

I'm really stuck on this. If anyone could give me some advice on how to get both golfers finish position stats in the same table rather than in two seperate ones I'd really appreciated it.

Many thanks.
Reply With Quote