#1

  1. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    Question Square of Asterisks


    I am learning loops in php now a day.
    i have some confusion in my assignment, I tried to find solution from google, did not find anything.

    code is :

    PHP Code:
    <?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();
                                    }
                                }
                                
                        
    ?>

    here If I give input of 5 ; square should be of 5.

    what the error. not same output
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Location
    Saint-Petersburg, Russia
    Posts
    236
    Rep Power
    28
    If you do want square, it should obviously consist of 25 stars, not of 5, so you can not do it in this way, with a single loop.

    And by the way if you wrote exactly what are you trying to do and what exactly you see instead of expected input - your chances for getting help grows.
  4. #3

  5. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    find solution


    the code as
    PHP Code:
    <?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
                            
    for ($a=$a<= $inputs[0]; $a++)
                            {
                            
    $x $inputs[0];
                            
    $y 1;
                            while (
    $y <= $x)
                            {
                                echo 
    "*";
                                if(
    $y==$x)
                                {
                                    echo 
    "<br />";
                                }
                                
    $y++;
                            }
                            }
                        
    ?>
    it was correct code, i have resolved it. but

    now my teacher gave me command to make a hollow square


    I made this code

    PHP Code:
    <?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
                            
    for ($a=$a<= $inputs[0]; $a++)
                                {
                                    
    $x $inputs[0];
                                    
    $y 1;
                                    while (
    $y <= $x)
                                        {
                                            echo 
    "*";
                                            if(
    $y==$x)
                                            {
                                                echo 
    "<br />";
                                                for (
    $b=1$b<= $inputs[0]-2$b++)
                                                    {
                                                    echo 
    "<pre>*       *</pre><br /> ";
                                                    }
                                        }
                                            
    $y++;
                                }
                                
                            }
                        
    ?>
    now here confuse how to create it. im creating logic
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    189
    Rep Power
    0
    "Your teacher gave you ...."

    It's called homework - perhaps you are supposed to do it yourself in order to learn something. Stop avoiding doing real work and learn what your teacher is trying to teach you.
  8. #5

  9. Join Date
    Sep 2013
    Location
    Lahore, Pakistan
    Posts
    74
    Rep Power
    1

    okay


    Originally Posted by jimmyg999
    "Your teacher gave you ...."

    It's called homework - perhaps you are supposed to do it yourself in order to learn something. Stop avoiding doing real work and learn what your teacher is trying to teach you.
    okay; i'll remind it
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I'm pretty sure your teacher wants you to get familar with integer division and the modulo operator.

    Use one loop and simply number the items: 0, 1, 2, ...

    Going back to your first assignment, which items start a new line? That's 0, 5, 10, 15, ..., right? What those items have in common is that they're all divisible by the width of the box, that is, the remainder is 0.

    So the code would be

    PHP Code:
    for ($item 0$item $width $height$item++)
    {
        
    // add a line break before the beginning of each line except the first line;
        // the beginning of a line is determined by the remainder of $item ÷ $width being 0 (in our case, that's 5, 10, 15, ...)
        
    if ($item $width == && $item 0)
            echo 
    '<br />';

        echo 
    '*';

    Now apply this to your second assignment: Which items must be displayed as a star? Ignoring the special first and last line, that's 0, 5, 10, 15, ... as well as 4, 9, 14, 19, ..., right? What's do the numbers of the first sequence have in common? What's with the second sequence?
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo