Page 1 of 1

PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 7:35 pm
by TAViX
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 ( :oops: ) to easily understand what I'm asking...:

Image

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.

Re: PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 8:08 pm
by John Cartwright
Whats your question? Remember, we aren't here to write code for you :wink:

Try to narrow down your "problem" into specific questions instead of a big generalization.

Re: PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 10:37 pm
by TAViX
John Cartwright wrote:Whats your question? Remember, we aren't here to write code for you :wink:
The question is "how to do that"? If you cannot help me with code, can you help me by posting some links on where can I
find some code examples/tutorials/etc ??
Tnks.

Re: PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 10:49 pm
by John Cartwright
Like I suggested, break this down into specific questions instead of describing larger application implementations.

So you are asking how to insert records from an html form into a mysql database? and afterwards listing the contents of the said database?

In any case, we usually ask you to post what you have tried so far so we can build on that. I'm more than happy to walk you through the steps, but it is important to remember our purpose is to teach people php, and not help them with a task where the user hasn't learned.

Here is a good link to start at, which describes inserting content from a form.

Re: PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 11:31 pm
by TAViX
John Cartwright wrote:Like I suggested, break this down into specific questions instead of describing larger application implementations.

So you are asking how to insert records from an html form into a mysql database? and afterwards listing the contents of the said database?

In any case, we usually ask you to post what you have tried so far so we can build on that. I'm more than happy to walk you through the steps, but it is important to remember our purpose is to teach people php, and not help them with a task where the user hasn't learned.

Here is a good link to start at, which describes inserting content from a form.
No, no, no.

Ok. First question.

How to create in my table (which have only 1 row - the title), using PHP scrip, extra rows? The number of rows is inputed in the "Persons nr." textbox.

Second question.

How to display in the first column "trainee code" , the "batch number" displayed and next to it the incrementig value. (That value is given by the text box "persons nr."

Like I said, after I'm introducing those 2 values in the textboxes, and press the CREATE button, that table will apear:

Image

Re: PHP creating extra table rows with a given value.

Posted: Thu Feb 12, 2009 11:47 pm
by John Cartwright
Okay I understand now.

Assuming that you have already created your form, there are only a few simple steps needed to accomplish this.

Firstly, you would need to detect whether the form has been submitted (to show whether to display the form or not).

I personally use the following method to detect a post,

Code: Select all

 
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') {
 
   //a post request has been submitted
 
}
Secondly, we need to access the information within the form to determine how many rows to create in the table. When using the post method, the input fields are accessible through $_POST, with GET they are accessible as $_GET, otherwise $_REQUEST encompasses all request vars.

Code: Select all

 
//make sure value personnr exists, if no cast as integer to avoid any bad data, otherwise 0
$personnr = isset($_POST['personnr']) ? (int)$_POST['personnr'] : 0; 
$batchnr = isset($_POST['batchnr']) ? (int)$_POST['batchnr'] : 0; 
 
Now that we have our two numbers that we need, it's a simple matter of iterating the amount of times equal to the value of $personnr.

Code: Select all

 
 
if ($personnr > 0) {
   for ($x=0; $x<=$personnr; $x++) {
      //we need to pad $x with 0's if it's less than 3 digits long
      echo $batchnr . str_pad($x, 3, "0", STR_PAD_LEFT) .'<br />';
   }
}
 
 
Obviously this isn't a complete solution, but it certainly enough to demonstrate what's involved. Atleast as far as I go without seeing you trying to figure out a solution for yourself.

Re: PHP creating extra table rows with a given value.

Posted: Sun Feb 15, 2009 7:05 pm
by TAViX
Ok.

I've managed to do a little something in PHP with the help of a friend from another forum, but I don't know how to create the text boxes in the rows/columns when the table rows are created...

Code: Select all

 
<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:149px;
    top:77px;
    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}
#Main_layer {
    position:absolute;
    left:22px;
    top:25px;
    width:727px;
    height:246px;
    z-index:3;
}
 
.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;
}
#Tbl_create {
    position:absolute;
    left:21px;
    top:285px;
    width:729px;
    height:611px;
    z-index:4;
}
 
-->
</style>
</head>
 
 
<div id="Main_layer">
  <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">Batch no.</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">Person no</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="Tbl_create">
 
<?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 cellspacing="1" id="tab2">
    
        <tr>
            <th>trainee code</th>
            <th>name</th>
            <th>col3</th>
            <th>col4</th>
            <th>col5</th>
        </tr>
        
<?php
            for($i=1;$i<=$person;$i++) {
                echo "
                    <tr>
                        <td><input type='text' name='tc[]' value='{$batch}00{$i}'></td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                ";
            }
        echo "
        
  </table>
        
        
        <input type='submit' name='submit2' value='Insert'>
        
        </form>
        
        ";
        
        }
    }
    
    if (isset($_POST['submit2'])) {
        // MySql stuff
        echo "bla bla bla";
    }
    
   ?>
 
 
</div>
</body>
</html>
But 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 I'm having a very hard time on uploading all the data from the textboxes into the database. I can only do this for 1 user only, but not for all...

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.