Search code examples
phpnested-loops

I am not getting any errors and my $results is not showing a value


I know there are better ways of doing this. I have done it using built in functions. I am trying to gain a better understanding of using a for loop within another for loop. The end result is to count how many times each unique character shows up in the original string. I dont see where my logic is flawed yet its not working so it must be my logic.

    <!DOCTYPE >
<html>
<head>
<title><?php echo $page_title; ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<link rel="stylesheet" type="text/css"  href="styles.css"/>
</head>

<body>

<div id="header">

<p>This is for testing yeay</p>

</div>

<div id="content_wrapper">
<?php
error_reporting (E_ALL | E_STRICT); 
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//original input turned into an array and is split either at space or comma
$user_input_num = preg_split("/[\s,]+/",$_POST['user_input_num']);
//array holding unique characters from original input
$temp_array =  array_unique($user_input_num);
//length of original input
$len = sizeof($user_input_num);

//length of array that hold only original characters
$len2 = sizeof($temp_array);

//temp string to add results to when passing through loop
$results = "";

//outer loop cycles through unique characters
    for($lcv = 0; $lcv < $len2-1; $lcv++)
    {
    $temp_unique_variable = $temp_array[$lcv];

        //inner loop cycles through the original input to count how many times the unique characters are in there
        for($lcv2 = 0; $lcv2 < $len; $lcv2++)
        {
        $count = 0;
            //
            if($temp_unique_variable == $user_input_num[$lcv2])
            {
                $count++;
            }
            if($lcv2 == $len)
            {
            $results .= $temp_unique_variable . " ". "(".$count . ")";
            }
        }
    }
    echo "Here is the original input : " . implode($user_input_num) ;
        echo'<br />';
        echo "Here are the unique characters in original input : " . implode($temp_array);
        echo '<br />';
        echo " Here is what is in result : " . $results;
}
?>
<div>

    <form action="#" method="post">

    <label for="user_input_num">Enter numbers here to find duplicates please use a space in between each number : </label>
    <input type="text" name="user_input_num"  id="user_input_num" placeholder="1 2 3 45 71 " value="<?php if(isset($_POST['user_input_num'])) echo $_POST['user_input_num'];?>"/>

    <input type="submit" name="submit" value="submit" />

    </div>
    </div>

<footer>
<p>This is the footer great job.</p>
</footer>

</body>
</html>

Solution

  • The for loop breaks when $lcv2 == $len

     for($lcv2 = 0; $lcv2 < $len; $lcv2++)
    

    and the if statement is checking for $lcv2 == $len

    so

    $results .= $temp_unique_variable . " ". "(".$count . ")";
    

    will never happen