Jump to content

Problem with a (quite simpe) code


Gzyms

Recommended Posts

PHP Tree

 

 

 Hi Guys,

 

I am a beginner so my question may seem simple wink.gif

 

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!

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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!

Link to comment
Share on other sites

^^^ 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.

Link to comment
Share on other sites

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.

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.