Jump to content

need help with loops and tables


marklarsen85

Recommended Posts

I will start off by saying , I am a NooB. i understand concepts , but have trouble with figuring out the proper way to code thing out. I am trying to use an HTML form that the user can input two numbers, and use those numbers to create a table to be echoed back.

 

echo "<form action='action.php' method='post' name='columns'>

Enter number of columns and rows to be displayed:<br /> Columns:<input type='text' name='colnums' size=3 maxlength=6 />Rows :<input type='text' name='rownums' size=3 maxlength=6 />

<br /><input type='submit' value='submit' /></form>" ;

 

This is my form. I have made a loop that will display but not in the fashion i would like it to.

 

 

for  ($createColumns=$_POST['colnums']; $createColumns<=10 ; $createColumns++) {

echo "<td>" . $createColumns . "</td>"  ;

}

 

I am looking to write a script that only needs the user input for the number of rows and columns to be displayed.

I think I need a range function or possibly an array for the possible numbers that can be input, but I'm not sure.

The loop I've made has a top limit and an increment that i don't think i need.

If you have any ideas I would greatly appreciate it.

Link to comment
Share on other sites

I'm not really sure about the exact issue you're having, but to make a table with the inserted amount of rows/columns (assuming they will remain the same for each iteration,) I would do a nested loop, like this:

 

<table id="myTable">
for ($createRows=1; $createRows<=$_POST['rownums'] ; $createRows++) {
   echo "<tr>";
   for ($createColumns = 1; $createColumns <= $_POST['colnums']; $createColumns++) {
       echo "<td>" . $createColumns."/".$createRows . "</td>"  ;
       }
   echo "</tr>";
   }
</table>

 

This should show each TD with column number/row number.  One problem you might have had in your earlier loop is that you set $createColumns to colnums to start the loop which means that if a user entered "10" for the number of columns, it would only iterate once as it then checks and sees that, yes, the second condition is true, but only once as they were set to the same value.

 

Is this what you meant?

 

By the way, I haven't tested that code so there may be a whoopsie, but the general idea is there.

Link to comment
Share on other sites

Thank you so much! This is what I was wanting to do. My only other question would how could I get the output table to display on the same page as the form that the user used. I understand that I have it set up to interact with the second page, but that is the only way I know how to code it.

Link to comment
Share on other sites

Well, there is an easy way and a hard way (hard if you don't know AJAX).  The easy way would be to do something like this:

 

<form action='[b]thisfile[/b].php' method='post' name='columns'>
Enter number of columns and rows to be displayed:<br /> 
Columns:<input type='text' name='colnums' size=3 maxlength=6 />
Rows :<input type='text' name='rownums' size=3 maxlength=6 /><br />
[b]<input type='hidden' name='actionTaken' value='submitted' />[/b]
<input type='submit' value='submit' />
</form>
<?php if (isset($_POST)):
   if (isset($_POST['actionTaken'])):
      if ($_POST['actionTaken'] == 'submitted'): ?>
<table id="myTable">
<?php
for ($createRows=1; $createRows<=$_POST['rownums'] ; $createRows++) {
   echo "<tr>";
   for ($createColumns = 1; $createColumns <= $_POST['colnums']; $createColumns++) {
       echo "<td>" . $createColumns."/".$createRows . "</td>"  ;
       }
  echo "</tr>";
   }
</table>
<?php endif; endif; endif; ?>

 

I bolded out the portion you need to change, you need to put the filename of the file that this very script is in.  Yes, the form will point back to itself.  If you want the form to NOT display after they have already used it, you can put an easy

<?php if (isset($_POST)): ?>

before the form and then put this immediately following the form:

<?php endif; ?>

 

You can do it without the two isset ifs but it will generate a warning when they first visit the page - as long as you don't have errors set to display, it shouldn't matter.  Anyway, the hard way would be to use AJAX to send the post data over and then grab the table display data from a php file on the server and display it - but that is more of a pain than it is worth.  There is a third way but it involves changing dom elements of the table with javascript and changing css styles dynamically with javascript to change a visibility attribute away from hidden.  Anyway, the solution above should do you.

 

Also, I noticed you were echoing a bunch of the HTML that you didn't need to echo.  If it comes after a conditional statement like an if or foreach or for loop (etc), you can use the alternate method of using conditionals which is (for an if):

<?php if (condition): ?>

and any HTML after that will display as normal HTML (only if the condition is met).  After that you can use the other statements like elseif the same way:

<?php elseif (condition): ?>

and then

<?php endif; ?>

 

So anyway, the above can be contained in one php file that is displayable by the user.

Link to comment
Share on other sites

i have a new issue that i am stuck on. i have created the table and loops that i was needing. (with a little help from my friends) now i am trying to take the table and loops i have created and decrement my countdown starting at a new number in each column.

 

echo "<tr>" ;

for ($table=1 ; $table<=3 ; $table++) {  // create a 3 cell 1 row table //

echo "<td>" ;

for ($countdown=50 ; $countdown>=40 ; $countdown--) { // loop for vertical 50 - 40 count //

if (($countdown-10)%3) //  test for countdown divided by 5 if there is a remainder echo countdown loop //

echo "<br />" . $countdown . "<br />" ;

else

for ($countover=1 ; $countover<=10 ; $countover++) // loop for horizontal 1- 10 count //

echo $countover ; } // when if test has no remainder echo countover loop //

}

echo "</tr></table>" ;

 

this will display 3 columns with a 50 - 40 countdown in each column, but i would like it to display 50 - 40 in the first column, 40 - 30 in the second, and 30 - 20 in the last column. i believe i need to make a function to do this , but i haven't used functions very much yet. if you know any solutions please explain!!

thanks

mark

 

Link to comment
Share on other sites

are you try

<?php
echo '<table border="3">';
for($row = 4; $row >= 0; $row--){
    echo '<tr>';
    for( $col = 10; $col >0; $col--) echo '<td>', $row * 10 + $col, '</td>';
    echo "</tr>\n";
}
echo '</table>';
?>

Link to comment
Share on other sites

i understand what you have done there, but it's not quite what i was trying to do. i have attached a file showing the output i have so far. in row 1 i would like the second column to start at 11 and count to 20, and in the third column to start at 21 and count to 30. in row 2 i would like to count in the opposite direction 50 - 40 in the first column, 41 - 30 in the second, and 31 - 20 in the third.

do i need to create a function to achieve this or am i going at it from a wrong direction?

thanks

  mark

 

[attachment deleted by admin]

Link to comment
Share on other sites

is it what you look for

<?php
echo '<table border="3">';
for($row = 1; $row < 5; $row++){
    $direction = 1 - ($row % 2) * 2;
    $staart = ($row % 2) * 49 + 1;
    echo "<tr>\n";
    for($col = 0; $col < 3; $col++){
        echo "\t<td>";
        for($i = 0; $i < 10; $i++) {
            echo $staart, ' ';
            $staart+=$direction;
        }
        echo "</td>\n";
    }
    echo "</tr>\n";
}
echo '</table>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.