Gzyms Posted July 21, 2016 Share Posted July 21, 2016 PHP Tree Hi Guys, I am a beginner so my question may seem simple I have a "tree", I want to make it possible to change tree's size when someone enters number of lines into the box "Send". Code: PHP Code: <html> <body> <form method="GET" action="tree.php"> <label for="numberofLines" value="shownumberofLines"> <input type="text" name="numberofLines"> <input type="submit" value="Send"> </form> <table> <?php $numberofLines = !empty($_GET['numberofLines']) ? $_GET['numberofLines'] : 5; var_dump($numberofLines); $a = '<td style="background-color:white"> </td>'; $b = '<td style="background-color:green"> </td>'; $c = '<td style="background-color:brown"> </td>'; $d = '<td style="background-color:yellow"> </td>'; for ($i = 0; $i < 4; $i++) { echo $a; } for ($i = 0; $i < 1; $i++) { echo $d; } for ($i = 0; $i < 2; $i++) { echo $a; } echo "<tr>"; for ($i = 0; $i < 2; $i++) { echo $a; } for ($i = 0; $i < 5; $i++) { echo $b; } echo "</tr>"; echo "<tr>"; for ($i = 0; $i < 0; $i++) { echo $a; } for ($i = 0; $i < 9; $i++) { echo $b; } echo "</tr>"; echo "<tr>"; for ($i = 0; $i < 4; $i++) { echo $a; } for ($i = 0; $i < 1; $i++) { echo $c; } echo "</tr>"; ?> </table> </body> Thank You in advance! Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/ Share on other sites More sharing options...
Psycho Posted July 21, 2016 Share Posted July 21, 2016 (edited) I really don't understand what you are trying to do, nor what your problem is. What IS the code doing now and what do you WANT it to do differently? EDIT: What is the "numberoflines" variable supposed to do? Is it supposed to make the "tree" wider, taller, or what? Edited July 21, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534804 Share on other sites More sharing options...
ginerjm Posted July 21, 2016 Share Posted July 21, 2016 What a mess of code. And why do you not have a tr tag to start your first set of outputs? So - what kind of a table structure is this? Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534805 Share on other sites More sharing options...
Gzyms Posted July 21, 2016 Author Share Posted July 21, 2016 Hi, Ok, I'm on a course and the guy told us to create a tree (and well he accepted this mess or maybe he was just fed up with me^^). $numberofLines is suppose to make the tree bigger. So for example when you enter "10", tree would be 10 lines. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534806 Share on other sites More sharing options...
mac_gyver Posted July 21, 2016 Share Posted July 21, 2016 ^^^ that's not specific enough. 10 lines of what exactly? will there always be a row with one yellow element at the top and a row with one brown element at the bottom and the entered number of rows is the number of total rows or it it the number of green rows, and will the green rows always be 4 more elements than the proceeding row? you have to define the rules before you can write the code. you would then be calculating the number of elements to output, based on the current row number being output, rather than having the numbers and blocks of code hard-coded. also, your four variables should have meaningful names, and if you use str_repeat() instead of for(){} loops, your code will be clearer to read. your current code would look like this - $white = '<td style="background-color:white"> </td>'; $green = '<td style="background-color:green"> </td>'; $brown = '<td style="background-color:brown"> </td>'; $yellow = '<td style="background-color:yellow"> </td>'; echo "<tr>"; echo str_repeat($white,4); // the 4 would be a calculated value echo str_repeat($yellow,1); echo "</tr>"; echo "<tr>"; echo str_repeat($white,2); // the 2 and 5 in this block, for green rows, would be calculated values echo str_repeat($green,5); echo "</tr>"; echo "<tr>"; echo str_repeat($white,0); // all the green rows would be output inside of a loop, this block of code wouldn't exist echo str_repeat($green,9); echo "</tr>"; echo "<tr>"; echo str_repeat($white,4); // the 4 would be a calculated value echo str_repeat($brown,1); echo "</tr>"; you would basically be calculating the number of white and green elements in each row and the green row(s) would be output inside of a loop. Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534807 Share on other sites More sharing options...
ginerjm Posted July 21, 2016 Share Posted July 21, 2016 I can't imagine a real-world example of using an html table to do this. Should someone really need a tree I think it would be better accomplished using CSS instead of that. For an instructor to be teaching you to do it this way is a sign he is past his time/prime. Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534808 Share on other sites More sharing options...
Gzyms Posted July 22, 2016 Author Share Posted July 22, 2016 Thank you Guys! Hope soon I will be better in PHP:) Quote Link to comment https://forums.phpfreaks.com/topic/301538-problem-with-a-quite-simpe-code/#findComment-1534810 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.