Here's what I'm trying to do. You select how many categories you wish to add, then click submit. Then you type the category name along with the description into the provided fields. Then it will submit it all to the database. I've been having trouble with this logic all morning, I have some of the same if statements scattered around just to see if it would work. I can't figure it out, I think it may be easier to store this data in a session, but I don't know. Don't worry about the the DB operations, if you could just get it to echo, all the values, it would be great. I'll worry about the DB later as I have more to do on this code. Here's my code as of now: <?php if(isset($_POST['submit_num'])) { if (isset($_POST['submit_cat'])) { $number = $_POST['number']; echo $number; while ($i < $number) { echo $number; echo $i; $i++; } } else { if (empty($_POST['submit_cat'])) { if(is_numeric($_POST['number'])) { $number = $_POST['number']; echo"<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">"; while($i < $number) { echo"<input type=\"text\" name=\"$i\" value=\"Category Name!\" /> <br /> <input type=\"text\" name=\"desc_$i\" value=\"Category Description!\" /><br /><br />"; $i++; } echo'<input type="submit" name="submit_cat" /></form>'; } else { echo"The number of categories must be numeric!"; } } elseif (isset($_POST['submit_cat'])) { $number = $_POST['number']; echo $number; while ($i < $number) { echo $number; echo $i; $i++; } } } } elseif ( (empty($_POST['number'])) && (empty($_POST['submit_cat']))) { ?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="amount"> <input type="text" value="Number of Categories" name="number" /> <input type="submit" name="submit_num" /> </form> <?php } ?> PHP: Rep to anyone that can help me!
Why not just loop straight from the $_POST['submit_cat'] like ... if (!isset($_POST['submit_num'])) {?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="amount"> <input type="text" value="Number of Categories" name="number" /> <input type="submit" name="submit_num" /> </form> <? } else { $i = 0 ;?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="all_categories"> <input type="hidden" name="total_categories" value="<?= $_POST['submit_num'] ?>" /> <? while ($i < $_POST['submit_num']) {?> <input type="text" name="Category_Name_<?= $i ?>" /> <input type="text" name="Category_Desc_<?= $i ?>" /> <hr> <? $i ++ ; }?> <input type="submit" name="submit_num" /> </form><? }?> PHP: Typed on a whim and did not review for accuracy but the above is how I would handle it. Please notice the hidden variable ['total_categories']. This will help your loop on the processing side when sticking in database. Hope this helps.
Thats good. The code doesn't work.. Just so you know. But I modified my code, and it still doesn't output it properly. <?php if(isset($_POST['submit_num'])) { if (empty($_POST['submit_cat'])) { if(!is_numeric($_POST['number'])) { echo"The number of categories must be numeric!"; exit; } echo"<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">"; while($i < $number) { echo"<input type=\"text\" name=\"$i\" value=\"Category Name!\" /> <br /> <input type=\"text\" name=\"desc_$i\" value=\"Category Description!\" /><br /><br />"; $i++; } echo"<input type=\"hidden\" name=\"total\" value=\"{$_POST['number']}\" /> " . '<input type="submit" name="submit_cat" /></form>'; } else { if (isset($_POST['submit_cat'])) { $total = $_POST['total']; $i = 0; while ($i < $total) { echo $total; echo $i; $i++; } } } } elseif ( (empty($_POST['number'])) && (empty($_POST['submit_cat']))) { ?> <form action="<?=$_SERVER['PHP_SELF'];?>" method="post" name="amount"> <input type="text" value="Number of Categories" name="number" /> <input type="submit" name="submit_num" /> </form> <?php } ?> PHP:
Please give us some error codes or something other than it doesn't work. It output a new form when I tested (c4yourself), but that is as far as I went because you said not to worry about inputting into db stuff. Please provide what you were expecting please because I think I might be confused.
It's not outputting the forms that's an issue, it's outputting the data. Take a look at my code: if (isset($_POST['submit_cat'])) { $total = $_POST['total']; $i = 0; while ($i < $total) { echo $total; echo $i; $i++; } } PHP: This section is where it will be entering into the DB. For now, I am trying to output the data to mimic inputing it into the DB. That doesn't work. I can't figure out why.