1

So im fairly new in coding HTML CSS and PHP and now im focusing PHP. Our professor asked us to incorporate PHP codes in our website (no database) and im trying to print a an array using a for loop and echo.

the problem is that the echo doesn't show in the webpage and I am now lost

below is my codes:

<html>
<head>
<title>PHP Loops and Sorts</title>
<body>
<h1>PHP Loops and Sorts</h1>

<div class="container">
<?php
$dogs=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$cats=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$birds=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$fishes=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");

function countsize($array,$size){
    $size = count($array);
    return $size;
}

if(isset($_POST['btnShow']) )
    {
        $arraypick=$_POST['formAnimal'];
        $arrsize = countsize($arraypick,$size);
        for(&x = 0,$x<$arrsize,$x++){
            echo $arraypick[$x] . "<br>";
        }
    }
?>
Breeds of different kinds of animals. Select what animal's breed to be shown:<br>
    <select name="formAnimal">
  <option value="">Choose animal</option>
  <option value="dogs">Dog</option>
  <option value="cats">Cat</option>
  <option value="birds">Bird</option>
  <option value="fishes">Fish</option>
</select><br><br>
<div style="margin:auto,text-align:center;text-align:center">
<INPUT TYPE = "Submit" Name = "btnShow" VALUE = "Show List">&emsp;
<INPUT TYPE = "Submit" Name = "btnAsc" VALUE = "Show Ascending">&emsp;
<INPUT TYPE = "Submit" Name = "btnDes" VALUE = "Show Descending">
</div>


<?php

echo $size

?>

</div>
</body>
</html>
2
  • Your syntax for the for loop is incorrect. php.net/manual/en/control-structures.for.php Commented Jan 17, 2018 at 4:18
  • Please update your for loop as for($x = 0;$x<$arrsize;$x++){ echo $arraypick[$x] . "<br>"; }. Also add ; in line 47. ie echo $size; Commented Jan 17, 2018 at 4:24

5 Answers 5

2

You need to close your head tag before your body tag.

You also don't need to pass in size to your method count size.

function countsize (&array) {
    return count(&array);
}
Sign up to request clarification or add additional context in comments.

Comments

1

if(isset($_POST['btnShow']) ) are you sure it is set ?

From what we have in your code you can't even POST, you are missing your form tags

Try wrapping your inputs with :

<form method="POST">
    <!--inputs-->
</form>

2 Comments

Thank you very much. Ultimately, this was the piece of code i don't have thats why it cant run.
Awesome ! Glad I could help :D
1

Your syntax for the for loop is incorrect. Check out the php documentation

Also the $size variable is never declared in the scope you are using it.

Comments

1

Didn't get how you're using $arrsize but you used &x instead of $x while initializing loop.

for($x = 0,$x<$arrsize,$x++){
  echo $arraypick[$x] . "<br>";
}

Comments

1

change this lines of code:

$dogs=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$cats=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$birds=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$fishes=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");

function countsize($array,$size){
$size = count($array);
return $size;

}

$arraypick=$_POST['formAnimal'];
    $arrsize = countsize($arraypick,$size);
    for(&x = 0,$x<$arrsize,$x++){
        echo $arraypick[$x] . "<br>";
    }

To :

$animalArray['dogs']=Array("Labrador Retriever","German Shepherd","Bulldog","Golden Retriever","Poodle","Beagle","Rottweiler","Yorkshire Terrier","Siberian Husky","Dachshund","Chihuahua","Pug","Great Dane","Dobermann","Shih Tzu");
$animalArray['cats']=Array("Persian","Siamese","Maine Coon","Ragdoll","Sphynx","British Shorthair","Abyssinian","Bengal","Scottish Fold","Himalayan","Russian Blue","Siberian","Munchkin");
$animalArray['birds']=Array("Canaries","Budgies","Finches","Cockatiels","Quaker Parakeets","Pionus Parrots","Poicephalus Parrots","Amazon Parrots","Pyrrhura Conures","Peach-Faced Lovebirds");
$animalArray['fishes']=Array("Koi","Fantail","Oranda","Comet","Black Telescope","Butterfly Tail","Ryukin","Goldfish","Lionhead","Mirror Carp");

 function countsize($index){
  return count($animalArray[$index]);
}
    $arraypick=$_POST['formAnimal'];
    $arrsize = countsize($arraypick);
    for($x = 0;$x<$arrsize;$x++){
        echo $animalArray[$arraypick][$x] . "<br>";
    }

1 Comment

I think your goal here is to display the animals listed on each variable on the top. so ill edit it once more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.