SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: Array problem

  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2007
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array problem

    Morning All,

    Wondering if someone could help me with an array problem I have. On my page:

    http://www.slougheaz.org/football/ad....php?team_id=2

    I enter data into three tables. A

    reports2
    appearances2
    goals2

    Now using this form data is saved into each of the tables with the correct data apart from the goals2 table.

    Now if you select a scorer from my form above and hit submit the correct details are saved into ONE row in the database. - fine

    But there may be more than ONE scorer so I’ve tried to create my page so that when there is more than one scorer I can add another scorer by clicking the add another scorer button. Please try this

    However, when I add another scorer to my page I have two problems:

    1) The new scorer in the dropdown box is added to the bottom of the page?
    2) Say I have two scorers and add a scorer in the new dropdown box and press submit the value I select for the second scorer does not save in the goals column as a new row (IE two rows are saved - one for each scorer). Only the first scorer (one row) is added.

    Would anyone be able to help?

    PHP Code:
    <?php

    // Start the connection to the database        
      
    include('**************************');
        
    $can_i_connect db_connect(); // by db_connect function is in my include file 
        
    if(!$can_i_connect
          { 
           
    $msg "Could not connect to the database";
           } 

    // Include functions to get the various values from the dropdown boxes
    include('********************'); 

    if(isset(
    $_REQUEST['Submit']))
    {
        
    // Values used for all three tables
      
    $team_id $_GET['team_id'];
      
    // Values to be inserted into reports table 
      
    $match_date $_POST['match_date'];
      
    $home_team $_POST['home_team'];
      
    $home_score $_POST['home_score'];
      
    $away_score $_POST['away_score'];
      
    $away_team $_POST['away_team'];
      
    $headline $_POST['headline'];
      
    // Values to be inserted into appearances table  
      
    $goalkeeper $_POST['goalkeeper'];
      
    $defender $_POST['defender'];
      
    // Values to be inserted into goals table 
     
    $sql = array(); 
    foreach(
    $_REQUEST['r'] as $row)
       { 
            
    $sql[] = sprintf("('%s', '%s', %d)"
                
    $player_id $row['player_id'],
                
    $report_id $_GET['report_id'],  
                
    $team_id $_GET['team_id']);    
        } 


    // IF i'm not getting data then insert into various tables
    if(!isset($_GET['report_id']))
    {

    $query_one mysql_query("INSERT INTO `reports2` ( `team_id`, `match_date`, `home_team`,`home_score`,`away_score`,`away_team`,`headline` ) 
                              VALUES ('
    $team_id', '$match_date', '$home_team', '$home_score', '$away_score', '$away_team', '$headline')"); 

    $lastinsert mysql_insert_id(); //to get report_id from reports table
                          
    $query_two mysql_query("INSERT INTO `appearances2` ( `report_id`, `team_id`, `goalkeeper`, `defender` ) 
                              VALUES ('
    $lastinsert', '$team_id', '$goalkeeper', '$defender')");
                        
    $query_three mysql_query("INSERT INTO `goals2` ( `player_id`, `report_id`, `team_id` ) 
                                VALUES ('
    $player_id', '$lastinsert', '$team_id')"); 
    }
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>News</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script> 
    function addRow(form) { 
        var div = document.createElement("DIV"); 
        var html = document.getElementById("ROW").innerHTML; 
        var count = form.getElementsByTagName("DIV").length + 1; 
        div.innerHTML = html.split("[1]").join("[" + count + "]"); 
        form.appendChild(div); 

    </script>
    <style> 
    div {clear:both} 
    span.next {     
        float:left; 
        width:210px; 

    input.inputtext { 
        width: 200px; 
        font-size:1.1em; 
        background:#eee; 
         


    </style>  
    </head>
    <body>
    <form name="form1" method="post" action="">
      <p>1) Match Date :<br />
        <input name="match_date" type="text" id=",match_date" value="<?php echo $match_date?>" size="36">
        <br />
        <br />
        2) Teams &amp; Score:<br />
        <input name="home_team" type="text" id="home_team" value="<?php echo $home_team?>" size="36">
        <input name="home_score" type="text" id="home_score2" value="<?php echo $home_score?>" size="3">
        <strong>-</strong> 
        <input name="away_score" type="text" id="away_score2" value="<?php echo $away_score?>" size="3">
        <input name="away_team" type="text" id="away_team" value="<?php echo $away_team?>" size="36">
        <br />
        <br />
        3) Scorer 
      <div id="ROW"> <span class="next">
        <select class="input" name="r[1][player_id]" size="1" style="width: 145" tabindex="1">
          <option value="" selected>Goalscorers</option>
          <?php 
             $type_array
    =get_player_names(); 
               foreach (
    $type_array as $players
              { 
                print(
    "<option value=\"".$players['player_id']."\">".$players['player_name']."</option>\n"); 
              } 
          
    ?>
        </select>
        </span></div>
        <input name="button" type="button" onClick="addRow(this.form)" value="Add another Scorer">
      <br />
      <br / >
        4) Headline: <input name="headline" type="text" id="headline2" value="<?php echo $headline?>" size="36">
      <br />
      <br />
        5) Team: 
        <input name="goalkeeper" type="text" id="goalkeeper" value="<?php echo $goalkeeper?>" size="36">
      <input name="defender" type="text" id="defender" value="<?php echo $defender?>" size="36">
        <br />    <br />
        <input type="submit" name="Submit" value="Submit" class="button">
        <input type="reset" name="Reset" value="Reset" class="button">
      </form>
    </body>
    </html>
    Many Thanks

    Chris
    Last edited by chris_stfc; Jun 1, 2007 at 00:47.

  2. #2
    SitePoint Addict
    Join Date
    May 2005
    Location
    London, ON
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you use form.appendChild it will append that child just before the </form>

    The reason this data isn't being sent is because it then therefore appears after yours submit button.

    You need to append it after the last dropdown that is visible.

    From taking a quick look I would recommend that you add an ID to the new divs you're creating with javascript, and then from this you can getElementByID(ROW<someNumber>).appendChild(div)
    Ryan Price
    Subscribe to my blog for regular tips and tricks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •