SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member TAViX's Avatar
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question PHP creating extra table rows with a given value.

    Hello guys. First let me tell you that I'm realy n00b in coding bussiness, my real job involves 3D design not web design, but still I have a very important project and it seems I can only get help by asking YOU people realy nice . :bow: :bow:

    So my problem is:...

    First on the page I have 2 text fields, for example, one with a "Batch Nr.", and another with "Person Nr".
    I also want to have a hidden form with a table that will appear only after I've clicked on the subbmit button (CREATE) in my case.

    And here is the catch. My table has only 1 row created, that's the title row, BUT the next rows (the number of) are needed to be created by the given value in the "Person Nr." text box. In other words, if I have 5 persons imputed, than the table will need to have extra 5 rows.

    I have drawn a sketch in paint ( ops: ) to easily understand what I'm asking...:

    *ttp://img520.imageshack.us/img520/5681/phpcreatetablerowsbyaspfj8.gif

    Then after creatind the rows, I need that the first column to be populated first, with "Batch number", then with a 3 digit nr, from 001 to "Person Nr.", for example 165001.

    THANK YOU GUYS IN ADVANCE, I kind of known the principle of how to do this, but I need some code, because PHP programing is not my strongest point.
    Thank you again.
    Last edited by TAViX; Feb 12, 2009 at 22:39.

  2. #2
    Non-Member
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    622
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all, Welcome to the SitePoint Forums

    Secondly, most members here are more than happy to help someone with their coding issues, but not do their coding for them completely.
    How about you have a go at doing it yourself, and if you have problems then post how far you have got here and we will guide you as best we can.

    You will need to understand and use:
    1) Variables, Strings and HTML

    2) Post Variables and how to use a HTML Form.

    3) array()

    4) foreach() or for() and isset().

    5) mysql_fetch_array()

    6) mysql_query()

    and

    7) mysql_real_escape_string()
    (important!)

    If you have no inclination to try and do the code your self, you could always pay a proffessional to do it, a simple task like this would be very affordable.

    I hope you enjoy your time here.
    ro0bear

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are 2 ways you could create the table/form.

    Either server side, or in the browser.
    Server side will be the easier way.

    You need to read the values entered into the form (and provide some method to validate them to avoid errors), then using either a For or While loop, generate the form.

    Then once the form has been submitted, you need to be able to read the form values into an array (should already be done provided you named the form fields correctly), then using a Foreach loop, validate the data, then Insert the results in the database.


    If you want to be able to read the database back into the form, then things will get a good bit more complicated.

  4. #4
    SitePoint Member TAViX's Avatar
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Quote Originally Posted by m_c View Post
    If you want to be able to read the database back into the form, then things will get a good bit more complicated.
    Ok, I have managed to create the table and to generate the rows, but I'm stuck there. I'll update here tomorow when I'm at work. Thanks.

  5. #5
    SitePoint Member TAViX's Avatar
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    I've managed to do a little something in PHP with the help of a friend from another forum, but I have some problems...

    PHP Code:
    <?php require_once('../Connections/DB_rev00.php'); ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

      
    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

      switch (
    $theType) {
        case 
    "text":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;    
        case 
    "long":
        case 
    "int":
          
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case 
    "double":
          
    $theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
          break;
        case 
    "date":
          
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
          break;
        case 
    "defined":
          
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
          break;
      }
      return 
    $theValue;
    }
    }

    mysql_select_db($database_DB_rev00$DB_rev00);
    $query_Batch_in "SELECT trainee_code, comp_code, sur_name, given_name, middle_name, nickname FROM passport_info";
    $Batch_in mysql_query($query_Batch_in$DB_rev00) or die(mysql_error());
    $row_Batch_in mysql_fetch_assoc($Batch_in);
    $totalRows_Batch_in mysql_num_rows($Batch_in);
    ?><html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Batch Member Input</title>
    <style type="text/css">
    <!--
    #title {
        position:absolute;
        left:150px;
        top:82px;
        width:742px;
        height:154px;
        z-index:1;
    }
    body {
        background-repeat: no-repeat;
    }
    #apDiv1 {
        position:absolute;
        left:150px;
        top:163px;
        width:699px;
        height:272px;
        z-index:2;
    }
    .style5 {font-size: smaller; font-weight: bold; }
    .style8 {font-weight: bold}
    #apDiv2 {
        position:absolute;
        left:128px;
        top:182px;
        width:727px;
        height:164px;
        z-index:3;
    }
    #apDiv3 {
        position:absolute;
        left:149px;
        top:637px;
        width:725px;
        height:631px;
        z-index:4;
    }
    .style10 {
        font-size: small;
        font-family: Verdana, Arial, Helvetica, sans-serif;
    }
    .style12 {color: #2A0055}
    #list_tbl1 {
        position:absolute;
        left:148px;
        top:503px;
        width:726px;
        height:174px;
        z-index:4;
    }
    #tbl01 {
        position:absolute;
        left:147px;
        top:503px;
        width:730px;
        height:508px;
        z-index:4;
    }
    #apDiv4 {
        position:absolute;
        left:148px;
        top:504px;
        width:744px;
        height:484px;
        z-index:4;
    }
    .style14 {color: #000000}
    .style16 {color: #000000; font-weight: bold; }
    #apDiv5 {
        position:absolute;
        left:17px;
        top:219px;
        width:105px;
        height:66px;
        z-index:5;
    }
    #apDiv6 {
        position:absolute;
        left:127px;
        top:442px;
        width:742px;
        height:196px;
        z-index:4;
    }
    #apDiv7 {
        position:absolute;
        left:102px;
        top:730px;
        width:725px;
        height:191px;
        z-index:4;
    }

    -->
    </style>
    </head>

    <body>
    <div id="apDiv2">
      <p>
     
        <style type="text/css">
    body{
        font-size:10pt;
        font-family:Arial,Tahoma,Verdana;
        color:#555;
    }
    table{
        background:#777;
        border:1px solid #cfcfcf;
    }
    td,th {
        background:#fff;
        padding:10px;
    }
    td {
        font-size:10pt;
    }
    form ul {
        list-style-type:none;
        width:250px;
    }
    form ul li {
        margin-top:1px;
        padding:10px;
        height:30px;
        border:1px solid #999;
    }
    strong.err {
        color:#930;
        display:block;
        margin:5px 0px;
    }
    #tab2 {
        margin-bottom:5px;
    }
    #tab2 input{
        border:0px;
        background:none;
    }
      </style>
    </p>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      
         <p>
           <?php
        error_reporting
    (0);
        
    $batch $_POST['batch_nr']; // Get batch textbox
        
    $person $_POST['person_nr']; // Get person textbox
    ?>
         </p>
      <table width="177" border="1" cellpadding="2" cellspacing="1" bordercolor="#2A0000">
           <tr>
             <th width="119" align="left" bordercolor="#6E0067" bgcolor="#2A00AA" scope="row"><span class="style12">バッチ番号</span></th>
             <td width="41" align="center"><input name="batch_nr" type="text" id="batch_nr" value="<?php echo $batch?>" size="2" maxlength="3"></td>
           </tr>
           <tr>
             <th align="left" bordercolor="#6E0067" bgcolor="#2A00AA" scope="row"><span class="style12">バッチ人数</span></th>
             <td align="center"><input name="person_nr" type="text" id="person_nr" value="<?php echo $person?>" size="2" maxlength="3"></td>
           </tr>
         </table>

        <p class="style10">上記の条件でバッチメンバー登録表作成します </p>
         <p>
           <input type="submit" name="submit1" value="実行">
        </p>
      </form>



      
      
    </div>



    <div id="tbl01">
    <?php
        
    if (isset($_POST['submit1'])) {
        
    $person = (int)$person;
            if(
    $batch==''||$person=='')
                echo 
    "<strong class='err'>Please ensure that \"Person nr.\" is a number</strong>";
            else {
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p class="style10">バッチメンバー登録表 </p>
      <span class="style10">「名前」、「ニックネーム」、「配属先会社コード」を入力してください</span>
          
        <table width="730" height="99" border="1" cellpadding="2" cellspacing="1" bordercolor="#2A0000" id="tab02">
        <tr>
          <th width="100" scope="col"><span class="style14">研修生番号</span></th>
          <th colspan="3" scope="col"><span class="style14">名前</span></th>
          <th width="108" scope="col"><span class="style14">ニックネーム</span></th>
          <th width="158" scope="col"><p class="style14">配属先会社コード</p></th>
        </tr>
        <tr>
          <td align="center"><span class="style16">Trainee code</span></td>
          <td width="95" align="center"><span class="style16">Sur Name</span></td>
          <td width="122" align="center"><span class="style16">Given Names</span></td>
          <td width="102" align="center"><span class="style16">Middle Name</span></td>
          <td align="center"><span class="style16">Nickname</span></td>
          <td align="center"><span class="style16">Company Code</span></td>
        </tr>
      
            
    <?php
                
    for($i=1;$i<=$person;$i++) {
                    echo 
    "
                        <tr>
                            <td align='center'><input type='text' name='tc[]' value='
    {$batch}00{$i}' size='3' maxlength='3'></td>
                            <td align='center'><input type='text' name='sn[]' size='7'></td>
                            <td align='center'><input type='text' name='gn[]' size='7'></td>
                            <td align='center'><input type='text' name='mn[]' size='7'></td>
                            <td align='center'><input type='text' name='nn[]' size='7'></td>
                            <td align='center'><input type='text' name='cc[]' size='4'></td>
                        </tr>
                    "
    ;
                }
            echo 
    "
            
      </table>
            
            
            <input type='submit' name='submit2' value='Insert'>
            
            </form>
            
            "
    ;
            
            }
        }
        
        if (isset(
    $_POST['submit2'])) {
            
    // How to insert all the data from the texboxes in the mysql table???
            
    echo "
            
            I need here to upload the data in the MySQL database table
            
            
            
            "
    ;        
     }

    ?>
        

    </body>
    </html>
    <?php
    mysql_free_result
    ($Batch_in);
    ?>
    I need that the numbers in the table to be 6 digits max, regarding the values I enter in the Batch no texbox (max 999) or Person no (max 999)


    Also how do I input all that data into a MySQL table?? I know how to do that for only 1 person, it's realy simple, but not for all at once....

    Yeah, I know...

    P.S. There are some erors in the code, but I cannot figure it out where. For example I cannot put the <php> code in a layer somewhere in the page...

    Thanks again.
    Last edited by TAViX; Feb 16, 2009 at 00:36.

  6. #6
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    great

  7. #7
    Non-Member
    Join Date
    Oct 2007
    Location
    United Kingdom
    Posts
    622
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by TAViX View Post
    I need that the numbers in the table to be 6 digits max
    You can achieve that by using preg_match() in a function.

    PHP Code:

    function six_digit_no($number) {
    if (!
    preg_match('/^[0-9]+$/'$number)) {return 'Error: Not Digits';} //if it doesnt only equal digits then return Error: Not Digits
    elseif (!preg_match('/^[0-9]{0,6}$/'$number)) {return 'Error: Over 6 Digits';}  // if it does equal digits but there are more than 6 return Error: Over 6
    else {return $number;} // if it does equal under six digits, return the number
    }

    echo 
    six_digit_no('123456'); // 123123

    echo six_digit_no('1234567'); // Error: Over 6 Digits

    echo six_digit_no('123F123'); // Error: Not Digits 
    you should be able to modify that function to do a 3 digit function or what ever number of digits function you need.

  8. #8
    SitePoint Member TAViX's Avatar
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank. I've tried to implemented in my code but no succes so far...

    PHP Code:

    <html>

    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Batch Member Input</title>
    <style type="text/css">
    <!--
    #title {
        position:absolute;
        left:150px;
        top:82px;
        width:742px;
        height:154px;
        z-index:1;
    }
    body {
        background-repeat: no-repeat;
    }
    #apDiv1 {
        position:absolute;
        left:150px;
        top:163px;
        width:699px;
        height:272px;
        z-index:2;
    }
    .style5 {font-size: smaller; font-weight: bold; }
    .style8 {font-weight: bold}
    #apDiv2 {
        position:absolute;
        left:128px;
        top:182px;
        width:727px;
        height:164px;
        z-index:3;
    }
    #apDiv3 {
        position:absolute;
        left:149px;
        top:637px;
        width:725px;
        height:631px;
        z-index:4;
    }
    .style10 {
        font-size: small;
        font-family: Verdana, Arial, Helvetica, sans-serif;
    }
    .style12 {color: #2A0055}
    #list_tbl1 {
        position:absolute;
        left:148px;
        top:503px;
        width:726px;
        height:174px;
        z-index:4;
    }
    #tbl01 {
        position:absolute;
        left:147px;
        top:503px;
        width:730px;
        height:508px;
        z-index:4;
    }
    #apDiv4 {
        position:absolute;
        left:148px;
        top:504px;
        width:744px;
        height:484px;
        z-index:4;
    }
    .style14 {color: #000000}
    .style16 {color: #000000; font-weight: bold; }
    #apDiv5 {
        position:absolute;
        left:17px;
        top:219px;
        width:105px;
        height:66px;
        z-index:5;
    }
    #apDiv6 {
        position:absolute;
        left:127px;
        top:442px;
        width:742px;
        height:196px;
        z-index:4;
    }
    #apDiv7 {
        position:absolute;
        left:102px;
        top:730px;
        width:725px;
        height:191px;
        z-index:4;
    }

    -->
    </style>
    </head>

    <body>
    <div id="apDiv2">
      <p>
     
        <style type="text/css">
    body{
        font-size:10pt;
        font-family:Arial,Tahoma,Verdana;
        color:#555;
    }
    table{
        background:#777;
        border:1px solid #cfcfcf;
    }
    td,th {
        background:#fff;
        padding:10px;
    }
    td {
        font-size:10pt;
    }
    form ul {
        list-style-type:none;
        width:250px;
    }
    form ul li {
        margin-top:1px;
        padding:10px;
        height:30px;
        border:1px solid #999;
    }
    strong.err {
        color:#930;
        display:block;
        margin:5px 0px;
    }
    #tab2 {
        margin-bottom:5px;
    }
    #tab2 input{
        border:0px;
        background:none;
    }
      </style>
      
      
      
      
    </p>
      <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
      
      
      
      
      
         <p>
           <?php
        error_reporting
    (0);
        
    $batch $_POST['batch_nr']; // Get batch textbox
        
    $person $_POST['person_nr']; // Get person textbox
        
        
        
        
        
    ?>
         </p>
      <table width="177" border="1" cellpadding="2" cellspacing="1" bordercolor="#2A0000">
           <tr>
             <th width="119" align="left" bordercolor="#6E0067" bgcolor="#2A00AA" scope="row"><span class="style12">バッチ番号</span></th>
             <td width="41" align="center"><input name="batch_nr" type="text" id="batch_nr" value="<?php echo $batch?>" size="2" maxlength="3"></td>
           </tr>
           <tr>
             <th align="left" bordercolor="#6E0067" bgcolor="#2A00AA" scope="row"><span class="style12">バッチ人数</span></th>
             <td align="center"><input name="person_nr" type="text" id="person_nr" value="<?php echo $person?>" size="2" maxlength="3"></td>
           </tr>
         </table>

        <p class="style10">上記の条件でバッチメンバー登録表作成します </p>
         <p>
           <input type="submit" name="submit1" value="実行">
        </p>
      </form>



      
      
    </div>



    <div id="tbl01">
    <?php
        
    if (isset($_POST['submit1'])) {
        
    $person = (int)$person;
            if(
    $batch==''||$person=='')
                echo 
    "<strong class='err'>Please ensure that \"Person nr.\" is a number</strong>";
            else {
    ?>
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <p class="style10">バッチメンバー登録表 </p>
      <span class="style10">「名前」、「ニックネーム」、「配属先会社コード」を入力してください</span>
          
        <table width="730" height="99" border="1" cellpadding="2" cellspacing="1" bordercolor="#2A0000" id="tab02">
        <tr>
          <th width="100" scope="col"><span class="style14">研修生番号</span></th>
          <th colspan="3" scope="col"><span class="style14">名前</span></th>
          <th width="108" scope="col"><span class="style14">ニックネーム</span></th>
          <th width="158" scope="col"><p class="style14">配属先会社コード</p></th>
        </tr>
        <tr>
          <td align="center"><span class="style16">Trainee code</span></td>
          <td width="95" align="center"><span class="style16">Sur Name</span></td>
          <td width="122" align="center"><span class="style16">Given Names</span></td>
          <td width="102" align="center"><span class="style16">Middle Name</span></td>
          <td align="center"><span class="style16">Nickname</span></td>
          <td align="center"><span class="style16">Company Code</span></td>
        </tr>
      
            
    <?php

                
    for($i=1;$i<=$person;$i++) {
                    echo 
    "
                        <tr>
                            <td align='center'><input type='text' name='tc' value='
    {$batch}00{$i}' size='3' maxlength='3'></td>
                            <td align='center'><input type='text' name='sn' size='7'></td>
                            <td align='center'><input type='text' name='gn' size='7'></td>
                            <td align='center'><input type='text' name='mn' size='7'></td>
                            <td align='center'><input type='text' name='nn' size='7'></td>
                            <td align='center'><input type='text' name='cc' size='4'></td>
                        </tr>
                    "
    ;
                }
            echo 
    "
            
      </table>
            
            
            <input type='submit' name='submit2' value='Preview'>
            
            </form>
            
            "
    ;
            
            }
        }
        
        if (isset(
    $_POST['submit2'])) {
            
    // How to insert all the data from the texboxes in the mysql table???
            
    echo "
            
            I need here to list the data before inserting in the MySQL database table
            
            
            
            
            
            
            <table width='730' height='99' border='1' cellpadding='2' cellspacing='1' bordercolor='#2A0000' id='tab02'>
        <tr>
          <th width='100' scope='col'><span class='style14'>研修生番号</span></th>
          <th colspan='3' scope='col'><span class='style14'>名前</span></th>
          <th width='108' scope='col'><span class='style14'>ニックネーム</span></th>
          <th width='158' scope='col'><p class='style14'>配属先会社コード</p></th>
        </tr>
        <tr>
          <td align='center'><span class='style16'>Trainee code</span></td>
          <td width='95' align='center'><span class='style16'>Sur Name</span></td>
          <td width='122' align='center'><span class='style16'>Given Names</span></td>
          <td width='102' align='center'><span class='style16'>Middle Name</span></td>
          <td align='center'><span class='style16'>Nickname</span></td>
          <td align='center'><span class='style16'>Company Code</span></td>
        </tr>
        
        
        
        
    </table>

            
            
            
            
            
            
            
            "
    ;        
     }

    ?>
        

    </body>
    </html>
    Can you help me a little please?

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Location
    Scotland
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    for($i=1;$i<=$person;$i++) {
                    echo "
                        <tr>
                            <td align='center'><input type='text' name='tc' value='{$batch}00{$i}' size='3' maxlength='3'></td>
                            <td align='center'><input type='text' name='sn' size='7'></td>
                            <td align='center'><input type='text' name='gn' size='7'></td>
                            <td align='center'><input type='text' name='mn' size='7'></td>
                            <td align='center'><input type='text' name='nn' size='7'></td>
                            <td align='center'><input type='text' name='cc' size='4'></td>
                        </tr>
                    ";
                }

    The problem is with your main form (above)
    With the above code, although you're going to get the right number of lines in your form/table, the text field in each column is going to have an identical name, as they're all named tc, sn, gn...etc.

    You need to change the code so the input names form an array, so instead of 'sn', you need 'sn[$rownumber]'. However, what value you put in $rownumber, will be upto you to decide.
    You could use the $i value, or you could put the Trainee Code into a variable and use that for easier identification for reading values back out of the array.
    Both options will work, just the coding will be a bit different.

    Then once the form has been submitted, you can use a foreach loop to read the values back out the array, display the values, and generate the required SQL to update the database.
    Last edited by m_c; Feb 17, 2009 at 05:35. Reason: typo correction

  10. #10
    SitePoint Member TAViX's Avatar
    Join Date
    Feb 2009
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by m_c View Post
    You need to change the code so the input names form an array, so instead of 'sn', you need 'sn[$rownumber]'. However, what value you put in $rownumber, will be upto you to decide.
    You could use the $i value, or you could put the Trainee Code into a variable and use that for easier identification for reading values back out of the array.
    Both options will work, just the coding will be a bit different.

    Then once the form has been submitted, you can use a foreach loop to read the values back out the array, display the values, and generate the required SQL to update the database.
    Tnks. I see what I can do...


Tags for this Thread

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
  •