Welcome Guest, Not a member yet? Register   Sign In
Results and League Generator
#1

[eluser]ajh_8[/eluser]
Hi everyone,

Hoping someone can help me with a problem on my current project.

I'm trying to create a league table generator for a sports website. I'm hoping to add a result of a match which will then update the league table (points, games played, goal difference etc) as well as saving the result itself for referral for teams progress etc.

Below is my current attempt, so if anyone can help me out, that would be great!

Thanks in advance!

Controller:
Code:
function index() {
        $this->load->model('league/results_model');
        
        // field name, error message, validation rules
        $this->form_validation->set_rules('home_team', 'Home', 'trim|required');
        $this->form_validation->set_rules('away_team', 'Away', 'trim|required');
        $this->form_validation->set_rules('home_score', 'Home Score', 'trim|required');
        $this->form_validation->set_rules('away_score', 'Away Score', 'trim|required');


                
        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('includes/top');
            $this->load->view('includes/header');
            $this->load->view('league/add_result');
            $this->load->view('includes/footer');
        }
        
        else
        {            
            if($query = $this->results_model->add_result())
            {
                $this->load->view('includes/top');
                $this->load->view('includes/header');
                $this->load->view('league/about_result');
                $this->load->view('includes/footer');
                
            }
            else
            {
                $this->load->view('includes/top');
                $this->load->view('includes/header');
                $this->load->view('league/about_result');
                $this->load->view('includes/footer');        
            }
        }
    }
    

}


Model:
Code:
function add_result() {
        
        $add_result = array(
            'home_team' => $this->input->post('home_team'),    
            'away_team' => $this->input->post('away_team'),    
            'home_score' => $this->input->post('home_score'),    
            'away_score' => $this->input->post('away_score')                            
    
        );
        
        $homescore = $this->input->post('home_score');
        $awayscore = $this->input->post('away_score');
        $oldhomepoints = $this->input->post('old_home_points');
        $oldawaypoints = $this->input->post('old_away_points');
        $homepoints = $this->input->post('home_points');
        $awaypoints = $this->input->post('away_points');
        
        if($homescore > $awayscore) {
            $homepoints = 3;
            
            $calculation = $homepoints + $oldhomepoints;
            
            
        } else if ($homescore == $awayscore) {
            $drawpoints = 1;
            
            $calculation = $drawpoints + $oldhomepoints;
            $calculationn = $drawpoints + $oldawaypoints;
                
        } else if ($homescore < $awayscore) {
            $awaypoints = 3;
            
            $calculation = $awaypoints + $oldawaypoints;

            
            
        }
        
        
        $insert = $this->db->insert('teams', $calculation);
        $insert = $this->db->insert('results', $add_result);
        
        if ($homescore == $awayscore) {
            $insert = $this->db->insert('teams', $calculationn);
        }
        

        

        return $insert;

    }

View:
Code:
&lt;? $status = validation_errors(); if (isset($status)) { ?&gt;
           <div class="form-errors">&lt;? echo validation_errors(); ?&gt;</div>
    &lt;? } ?&gt;
    
    
    &lt;?php echo form_open('results') ;?&gt;    
    
    <label for="home_team" class="label">Home:</label>
        &lt;input type="text" class="input" name="home_team" value="&lt;?php echo set_value('home_team'); ?&gt;"   /&gt;
    
    <label for="away_team " class="label">Away Team:</label>
        &lt;input type="text" class="input" name="away_team" value="&lt;?php echo set_value('away_team'); ?&gt;" /&gt;
    
        
    <label for="home_score" class="label">Home score:</label>
        &lt;input type="text" class="input" name="home_score" value="&lt;?php echo set_value('home_score'); ?&gt;" /&gt;
        
    <label for="away_score" class="label">Away score:</label>
        &lt;input type="text" class="input" name="away_score" value="&lt;?php echo set_value('away_score'); ?&gt;" /&gt;

    
    &lt;input type="submit" name="submit" value="Add Result" class="submit"  /&gt;
        
    
    &lt;?php        
        echo form_close();
    ?&gt;
#2

[eluser]LuckyFella73[/eluser]
You didn't make clear what the actual problem is. Do you get error messages,
is your code not working as expected or are you asking for a better approach
to do this? Please post more details about your problem then you will get help
faster.
#3

[eluser]ajh_8[/eluser]
Apologies about the lack of information.

When processing the form it's giving the following error message:

Unknown column '3' in 'field list'

INSERT INTO `teams` (`3`) VALUES ('')


So it's recognising the number of points as a field when it's meant to be adding that number to a points field. It's also recognising what team has the highest amount of goals to give the designated points too, (which is what is required) but is not adding the number of points.

I'm not sure whether the way I have approached it is the best way to do so or whether I should go another route?
#4

[eluser]LuckyFella73[/eluser]
Your INSERT command is wrong. The right synthax is like this:
Code:
// from CI userguide
$data = array(
               'title' => 'My title' ,
               'name' => 'My Name' ,
               'date' => 'My date'
            );

$this->db->insert('mytable', $data);
Note that $data is an array! In your model you are trying to pass a variable as an array.

You can also set up your insert values like this:
Code:
// from CI userguide as well
$this->db->set('name', $name);
$this->db->set('title', $title);
$this->db->set('status', $status);
$this->db->insert('mytable');

This page of the Ci user guide should makes some things clearer:
http://ellislab.com/codeigniter/user-gui...ecord.html

Got to leave office now .. cheers and hope you get it to work now!




Theme © iAndrew 2016 - Forum software by © MyBB