puglover Posted October 6, 2009 Share Posted October 6, 2009 im a first time poster so i apologize if i do it wrong!! I am trying to make a multiplication table with php and it won't work. any suggestions will be greatly appreciated!! <?php $rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink"); ?> <html> <head> <title> </title> </head> <body> Create a multiplication table<p> <form method="get" action="<?php $_SERVER['php_self'];?> "> Enter number of rows<br /> <input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p> Enter number of columns<br /> <input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p> Select a color<br /> <select name="color"> <?php for ( $i = 0; $i < count( $rowcolors ); $i++ ) { echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n"; } ?> <input type="hidden" name="do_php" value="true"> <p><input type="submit" value="Create Table"> </form> <?php if( isset( $_GET['do_php'] ) ) { echo "<table width=\"50%\" border=\"3\">\n"; echo "<tr><td width='$cellwidth'> </td>"; for ( $j = 1; $j <= $_GET['colnum']; $j++ ) { echo "<th>" .$j . "</th>\n"; } for ($i = 1; $i <= $_GET['rownum']; $i++ ) { echo "<tr>"; echo "<td>".$i."</td>\n"; } echo "</tr>"; for ( $j = 1; $j <= $_GET['colnum']; $j++ ) { echo "<td>".$i * $j."</td>\n"; } } echo "</table>"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/176732-help/ Share on other sites More sharing options...
puglover Posted October 6, 2009 Author Share Posted October 6, 2009 did i post this wrong?? is there a reason no one can help me?? please let me know what i can do to get some help. The part of the program not working is the table it comes out all messed up. Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931816 Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 Going through your code, your HTML markup is alllllll wrong =P Have to close some TR tags, P tags, etc. What about it isn't working? Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931846 Share on other sites More sharing options...
puglover Posted October 6, 2009 Author Share Posted October 6, 2009 the table comes out wrong 1 2 3 4 5 1 2 3 4 5 1 6 2 12 3 18 4 24 5 30 thats what it looks like when the program is run Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931848 Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 Here is something to put you in the right direction. First thing you always want to do (especially when it's a display issue) is validate your HTML/CSS. <?php $rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink"); var_dump($_GET); ?> <html> <head> <title> </title> </head> <body> Create a multiplication table<p> <form method="get" action="multiply.php" > Enter number of rows<br /> <input type="text" name="rownum" size="10" /><br/> Enter number of columns<br /> <input type="text" name="colnum" size="10" /><br/> Select a color<br /> <select name="color"> <?php for ( $i = 0; $i < count( $rowcolors ); $i++ ) { echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n"; } ?> </select> <?php if(!empty($_GET)) { echo "<table border='1'>"; $i=1; while($i<=$_GET['colnum']) { echo "<tr>"; $c=1; echo "<td>".$i."</td>"; while($c<=$_GET['rownum']) { echo "<td>".$c * $i."</td>"; $c++; } $i++; echo "</tr>"; } echo "</table>"; } ?> <input type="hidden" name="do_php" value="true"> <input type="submit" value="Create Table"> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931868 Share on other sites More sharing options...
MatthewJ Posted October 6, 2009 Share Posted October 6, 2009 Not a display issue... a logic issue with the for loops. I hope this isn't a homework assignment that I just did for you <?php $rowcolors = array ("purple", "red", "yellow", "orange", "blue", "pink"); ?> <html> <head> <title> </title> </head> <body> Create a multiplication table<p> <form method="get" action=""> Enter number of rows<br /> <input type="text" name="rownum" size="10" value="<?php echo $_GET['rownum'];?>"><p> Enter number of columns<br /> <input type="text" name="colnum" size="10" value="<?php echo $_GET['colnum'];?>"><p> Select a color<br /> <select name="color"> <?php for ( $i = 0; $i < count( $rowcolors ); $i++ ) { echo "<option value=\"" . $i . "\">" . $rowcolors[$i] . "</option>\n"; } ?> <input type="hidden" name="do_php" value="true" /> <p><input type="submit" value="Create Table" /></p> </form> <?php if( isset( $_GET['do_php'] ) ) { //Create table echo "<table width=\"50%\" border=\"3\">\n"; //start first row with a blank space echo "<tr><td width='$cellwidth'> </td>"; //finish first row for($i=1; $i<=$_GET['colnum']; $i++) { echo "<td>".$i."</td>\n"; } //Close first row echo "</tr>\n"; //Start second row for($j=1; $j<=$_GET['rownum']; $j++) { echo "<tr>"; //Add 1st column values echo "<td>".$j."</td>\n"; //Add multiplied values for($k=1; $k<=$_GET['colnum']; $k++) { echo "<td>".$j * $k."</td>\n"; } } //Close the row echo "</tr>\n"; } //Close the table echo "</table>\n"; ?> </body> </html> I at least left the row color portion for you to figure out Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931910 Share on other sites More sharing options...
nafetski Posted October 6, 2009 Share Posted October 6, 2009 Well, he could of debugged things easier if the output was being put out correctly I feel the same way tho, I have a feeling we just did someones homework hah Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-931915 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 yes technically it was a homework assignment. but my teacher believes in using your resources to find the answers..not memorization. but anyways none of those suggestions actually worked..it made my table worse. Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933844 Share on other sites More sharing options...
MatthewJ Posted October 9, 2009 Share Posted October 9, 2009 My code worked fine... the fact that you don't know how to implement it is not my fault Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933847 Share on other sites More sharing options...
MatthewJ Posted October 9, 2009 Share Posted October 9, 2009 Looks like a multiplication table to me (see attachment)... [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933849 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 yes but you used 3 loops. i was told it only needed 2. i almost have it but its not quite there yet. Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933854 Share on other sites More sharing options...
MatthewJ Posted October 9, 2009 Share Posted October 9, 2009 Sorry, I didn't know your homework assignment... and you didn't tell me that... on that note though, your first try wasn't even close Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933861 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 ive gotten closer but now my table looks like this 1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 4 4 8 12 16 20 5 5 10 15 20 25 i need a row on top with the numbers to...next to the blank box...but everything i try doesnt work...any suggestions??? Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933863 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 oh derr perhaps posting my current code would work..lol...ill just post the table portion. <?php if( isset( $_GET['do_php'] ) ) { echo "<table width=\"50%\" border=\"3\">\n"; echo "<tr><td> </td></tr>"; for ($i = 1; $i <= $_GET['rownum']; $i++ ) { echo "<tr>"; echo "<td>".$i."</td>"; for ( $j = 1; $j <= $_GET['colnum']; $j++ ) { echo "<td>".$i * $j."</td>"; } echo "</tr>"; } echo "</table>"; } ?> Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933867 Share on other sites More sharing options...
lemmin Posted October 9, 2009 Share Posted October 9, 2009 How about this? <style> .nums { width:5%; float:left; } .head { background-color:#CCC; } </style> <?php for ($i=0;$i<11;$i++) echo "<div class=\"nums head\">$i</div>"; echo "<br />"; for ($i=1;$i<11;$i++) { echo "<div class=\"nums head\">$i</div>"; for ($j=1;$j<11;$j++) echo "<div class=\"nums\">" . $i*$j . "</div>"; echo "<br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933868 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 im gonna be honest..i have no clue what u just wrote...lol...im a beginner with php. Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933869 Share on other sites More sharing options...
lemmin Posted October 9, 2009 Share Posted October 9, 2009 Here, I added comments. The <style> part is just to make the divs display in the right format; that isn't php, just HTML. <style> .nums { width:5%; float:left; } .head { background-color:#CCC; } </style> <?php //Draw ten divs with numbers 0 through 11 with the header style for the top for ($i=0;$i<11;$i++) echo "<div class=\"nums head\">$i</div>"; //make a new line echo "<br />"; //for numbers 1 through 10... for ($i=1;$i<11;$i++) { //Echo those numbers for the left header column echo "<div class=\"nums head\">$i</div>"; //For numbers 1 through 10... for ($j=1;$j<11;$j++) //Multiply the two numbers together and display them echo "<div class=\"nums\">" . $i*$j . "</div>"; //new line echo "<br />\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933871 Share on other sites More sharing options...
puglover Posted October 9, 2009 Author Share Posted October 9, 2009 i do know css...just an fyi..lol...also after i get this damn table working correctly..i need to use css to color every other row...thats why i have the variable row colors...but when i open php in css to put in the variable..it doesnt work Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933873 Share on other sites More sharing options...
lemmin Posted October 9, 2009 Share Posted October 9, 2009 How about if I do it without HTML formatting: <?php //"\t" makes a tab and "\n" makes a new line //Draw the top row //This is the same as just echoing "1\t2\t3\t4\t5\t6\t7\t8\t9\t10"; for ($i=0;$i<11;$i++) echo $i."\t"; //Go to the next line echo "\n"; //for numbers 1 through 10... for ($i=1;$i<11;$i++) { //Echo those numbers for the left header column echo $i."\t"; //For numbers 1 through 10... for ($j=1;$j<11;$j++) //Multiply the two numbers together and display them echo $i*$j."\t"; //new line echo "\n"; } ?> You can just view source to see it formatted as a table. Here is the code without the comments if it is easier to read: <?php for ($i=0;$i<11;$i++) echo $i."\t"; echo "\n"; for ($i=1;$i<11;$i++) { echo $i."\t"; for ($j=1;$j<11;$j++) echo $i*$j."\t"; echo "\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/176732-help/#findComment-933878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.