Square of asterisks

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
same2cool
Forum Newbie
Posts: 11
Joined: Tue Sep 24, 2013 5:05 am
Location: Lahore, Pakistan
Contact:

Square of asterisks

Post by same2cool »

now a day I am learning php with loops.

I am solving my assignments how to create square of asterisks.

something is wrong in my code. what is this

Code: Select all

 <?PHP
    $inputs = array();
    if (isset($_POST['submit'])) { //to check if the form was submitted
        $value = isset($_POST['input0'])? $_POST['input0'] : null;
        for($count = 1; $value != null; $count++){
            array_push($inputs, $value);
            $value = isset($_POST['input'.$count])? $_POST['input'.$count] : null;
        }
    }
    var_dump($inputs);
?>    

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                        <!-- to take multiple inputs copy the below line and change name="input" attribute -->
                        Enter input 1:&nbsp;<input type="text" name="input0" value="" /><br />
                        <input type="submit" name="submit" value="Submit" />
                    </form>
                

  <?php // MY Loop code starts from here  
                            for ($x = 1; $x<=$inputs[0]; $x++)
                            {
                                echo "*";
                                $x = $inputs[0];
                                if ($x == $inputs[0])
                                {
                                    echo "<br />";
                                }
                                else
                                {
                                    die();
                                }
                            }
                            
                    ?> 
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Square of asterisks

Post by Celauran »

same2cool wrote:something is wrong in my code.
Could you be a little more specific?
antorizz
Forum Newbie
Posts: 8
Joined: Sat Nov 03, 2012 12:03 pm

Re: Square of asterisks

Post by antorizz »

I'm just throwing this out there, there are a few things that I see if not wrong, difficult to understand...

I'll just point out the for loop you have. You start out with $x being assigned the integer 1, but then you are assigning $inputs[0] to $x which is breaking the for loop.
Try changing $x to $i in the for loop

Code: Select all

for ($i = 1; $i<=$inputs[0]; $i++)
{
    echo "*";
    $x = $inputs[0];
    if ($x == $inputs[0])
    {
        echo "<br />";
    }
    else
    {
        die();
    } // end if
} // end for
Also, the if statement is pointless because you are assigning $inputs[0] to $x, so the if statement will be true.
same2cool
Forum Newbie
Posts: 11
Joined: Tue Sep 24, 2013 5:05 am
Location: Lahore, Pakistan
Contact:

Re: Square of asterisks

Post by same2cool »

I used it and did complete code.

Code: Select all

<?php
						for ($a=1 ; $a<= $inputs[0]; $a++)
						{
						$x = $inputs[0];
						$y = 1;
						while ($y <= $x)
						{
							echo "*";
							if($y==$x)
							{
								echo "<br />";
							}
							$y++;
						}
						}
					?>
Post Reply