Printing 'n' number of text input boxes using PHP

Hello,

I am building my Major Project using PHP and Oracle. Project is basically a replica of phpMyAdmin.

I am building the same kind of admin-panel for oracle using PHP.

I have a doubt…

Like in phpMyAdmin, while creating a new database table it asks for number of columns, I want to implement the same thing. But the problem is, on the next page, how do I print the number of text input fields as per the user’s input. To better explain, I have taken the screenshot of what I want to do-

In following image when I input ‘2’-
1 - http://i54.tinypic.com/1567j21.jpg

I get 2 set’s of input fields to define the column properties as follows-
2 - http://i52.tinypic.com/nzpugm.png

Same thing, I also want to do -

So question is - How can I make such a code in PHP which gives same number of input field set as the user input and also how can I get different variable names to store them?

Example - let’s say I inputed 2 in number of columns field, so I must be presented with 2 set’s of input field sets(including name,type etc.)

Now I want to transfer these two set’s of field to a query that will save the data to Oracle server.
So I need different variables to store each input like name1 for name from field set 1 similarly name2 for name from field set 2 and so on…

Kindly help asap.

Thanks in advance


<?php
error_reporting(-1);
ini_set('display_errors', true);

$rows = empty($_POST['rows']) ? 0 : (int)$_POST['rows'] ;
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="#" method="post">
      <div>
        <p>
          Number of people to add?
        </p>
        <input type="text" name="rows" />
      </div>
      <?php if(0 !== $rows): ?>
        <?php foreach(range(1, $rows) as $row): ?>
          <div>
            <p>Person <?php echo $row; ?></p>
            <input type="text" name="people[<?php echo $row; ?>][forename]" />
            <input type="text" name="people[<?php echo $row; ?>][surname]" />
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
      <input type="submit" value="Add" />
    </form>
  </body>
</html>

Thanks a lot AnthonySterling :slight_smile:

But as I am quite new to understanding PHP… could you kindly explain your code as well? So that I know what part of code does what…

Thanks again for prompt response :smiley:

Sure, I’ve added comments. :slight_smile:


<?php
#increase error reporting
error_reporting(-1);
ini_set('display_errors', true);

#if we have a rows variable and it's not empty
#convert it to an integer, otherwise, use 0.
$rows = empty($_POST['rows']) ? 0 : (int)$_POST['rows'] ;
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="#" method="post">
      <div>
        <p>
          Number of people to add?
        </p>
        <input type="text" name="rows" />
      </div>
      <!-- If the nummber of $rows is not 0 -->
      <?php if(0 !== $rows): ?>
        <!-- create loop which loops the number of times defined in $rows -->
        <?php foreach(range(1, $rows) as $row): ?>
          <div>
            <p>Person <?php echo $row; ?></p>
            <!-- Create a PHP array compatible set of inputs, using $row as the index -->
            <input type="text" name="people[<?php echo $row; ?>][forename]" />
            <input type="text" name="people[<?php echo $row; ?>][surname]" />
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
      <input type="submit" value="Add" />
    </form>
  </body>
</html>

Thanks a lot again…

Just one thing I couldn’t basically understand is the use of following line -


$rows = empty($_POST['rows']) ? 0 : (int)$_POST['rows'] ;

Please explain me this statement in more generalized form :slight_smile:

Oh and is it necessary to include this line? If I do not include it, what will the effect on the code?

Oh and btw… could you also write the code to display the input?

I can then co-relate it with feeding input into table…but I tried to display the input and was not able to go with array part.

Kindly show me a small example of displaying the output as well :slight_smile:

That code is commonly referred to as the Ternary Operator.

Try removing it, see if it still works. :wink:

Why don’t you try that, If you get stuck let me know. :slight_smile:

Hi…

Ok, so I tried to make a small thing out my self but don’t know where am I suppose I am not able to operate with arrays properly. Please have a look. I created 3 files -

test.php(a file consisting just a simple form)


<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <form action="next.php" method="post">
      <div>
        <p>
          Number of people to add?
        </p>
        <input type="text" name="rows" />
      </div>
      <input type="submit" value="Add" />
    </form>
  </body>
</html>

next.php(test.php file has this file’s name in action)
Here I have converted your 2D array to just 1D and tried to pass the value to next page where I’ll show the input content.


<?php
$rows = empty($_POST['rows']) ? 0 : (int)$_POST['rows'] ;
?>
<form action="show.php" method="post">
<?php if(0 !== $rows): ?>
       <?php foreach(range(1, $rows) as $row): ?>
          <div>
            <p>Person <?php echo $row; ?></p>
            <input type="text" name="people[<?php echo $row; ?>]" />
            <input type="text" name="lpeople[<?php echo $row; ?>]"/>
          </div>
        <?php endforeach; ?>
      <?php endif; ?>
	  <input type="submit"/>
      </form>

Show.php(to show the output)
I really have no Idea if this is the right way to print particular array without using a for loop. Please guide me on this :slight_smile:

<?php
$temp=$_POST["people[1]"];
echo $temp;
?>

This is the error I am getting on show.php -

Notice: Undefined index: people[1] in C:\wamp\www\phpora\show.php on line 2

Please let me know, how can I output the content input by user…

nearly there. :wink:

index.php


<?php
$people = empty($_GET['number_of_people']) ? 0 : (int)$_GET['number_of_people'] ;
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <div>
      <form action="#" method="get">
        <label for="number_of_people">
          How many people shall we add?
        </label>
        <input type="text" id="number_of_people" name="number_of_people" />
        <input type="submit" value="create" />
      </form>
    </div>
    <?php if(0 !== $people): ?>
      <div>
        <form action="view.php" method="post">
          <?php foreach(range(1, $people) as $index): ?>
            <div>
              (<?php echo $index; ?>)
              <input type="text" name="people[<?php echo $index; ?>][forename]" />
              <input type="text" name="people[<?php echo $index; ?>][surname]" />
            </div>
          <?php endforeach; ?>
          <input type="submit" value="display" />
        </form>
      </div>
    <?php endif; ?>
  </body>
</html>

view.php


<?php
$people = array();

if(isset($_POST['people']) && is_array($_POST['people'])){
  $people = $_POST['people'];
}

function display($text){
  $flags = defined('ENT_IGNORE') ? ENT_QUOTES | ENT_IGNORE : ENT_QUOTES ;
  echo htmlentities($text, $flags, 'UTF-8');
}
?>
<html>
  <head>
    <title>Demo</title>
  </head>
  <body>
    <?php foreach($people as $index => $person): ?>
      <div>
        (<?php display($index); ?>) <?php display($person['forename']); ?> <?php display($person['surname']); ?>
      </div>
    <?php endforeach; ?>
  </body>
</html>

I’ve placed a demo of this code here for you to take a look at. :slight_smile:

Thanks a lot for doing it all so neatly for me… and specially for your dedicated time :wink:

Hope you won’t mind answering questions, due the course of my project! Really appreciate your help :slight_smile:

Hey Anthony,

Can you help me with the following -

Please eagerly waiting for your reply!

Stuck with my project!