Display results from count using array

I am creating a number generator. the user needs to insert:

  • Maximum number (The maximum number that the generator can go up to)
  • Generate Rate (How many numbers should be generated)

This is stored into an array, and then stored into a MySQL database.

The field is subsequently taken from the MySQL database and is called upon in sorting.php

This data is then sorted into another array which counts how many times a particular number appears.

I now need to create a list of how many times a number appears. The following example is how I need it to display:

Number 15 displays 10 times
Number 2 displays 3 times
Number 10 displays 2 times

Generator.php
[php]<?php include 'Number_Information.php'; //import the class in this web page ?>

<?php $MINIMUM_NUMBER = 0; //Makes the default value zero //$minimum_number=$_GET['minimum_number']; //Use this if you wish to enable the user to insert any minimum number. If this is being used, ensure that the option above is commented out or removed. $MAXIMUM_NUMBER = $_POST['MAXIMUM_NUMBER']; $GENERATE_RATE = $_POST['GENERATE_RATE']; $OFFSET = 1; //This figure is used to correct the generate rate (Without this the script would generate an extra number for zero). $NUMBERS = array(); $NUMBERS2; echo "Your numbers will be generated from the range of {$MINIMUM_NUMBER} and {$MAXIMUM_NUMBER}.
"; echo "{$GENERATE_RATE} numbers will be created.
"; // This provides visual confirmation to the user. It will be removed. $GENERATE_RATE = $GENERATE_RATE - $OFFSET; //This corrects the generate rate. Rather than create an extra number for zero. //If you are to have a visual confirmation as to how many numbers are to be created, //it needs to be placed below this statement for ($i = $MINIMUM_NUMBER; $i <= $GENERATE_RATE; $i++) { $NUMBERS[$i] = rand($MINIMUM_NUMBER, $MAXIMUM_NUMBER); } $NUMBERS2 = implode( ',', $NUMBERS); //This gets rid of all the garbage that the array creates - so it can be inserted into the table. Use Explode later to put the garbage back in. { $insert="INSERT INTO GeneratedNumber (NUMBERS2) VALUES ('$NUMBERS2')"; } echo "
Contents of array:
"; //Testing reasons - ensure this is removed later on! print_r($NUMBERS2); $ID = $_POST['ID']; $MINIMUM_NUMBER = $_POST['MINIMUM_NUMBER']; $MAXIMUM_NUMBER = $_POST['MAXIMUM_NUMBER']; $GENERATE_RATE = $_POST['GENERATE_RATE']; //$NUMBERS2 = $_POST['NUMBERS2']; $db1 = new Number_Information(); $db1->openDB(); $numofrows = $db1->insert_GeneratedNumber('', $MINIMUM_NUMBER, $MAXIMUM_NUMBER, $GENERATE_RATE, $NUMBERS2); echo "

Success. Number of rows affected: $numofrows"; $db1->closeDB(); ?>





You now need to continue on to sort your data. Click here to do so.[/php]

Sorting.php
[php]<?php include 'Number_Information.php'; ?>

Data Sorting <?php $db1 = new Number_Information(); $db1->openDB();
    $sql = "select * from GeneratedNumber";
    $result = $db1->getResult($sql);

    if (!$_POST) { //page loads for the first time
        ?>

        <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">

            Select the ID (If you have just generated a number, it will be the last one):

            <!--This is the drop down menu-->
            <select name="ID"> 
                <?php
                while ($row = mysql_fetch_assoc($result)) {
                    echo "<option value='{$row['ID']}'> {$row['ID']} </option>";
                }
                ?>
            </select> <br />

            <input type="submit" value="Continue" />
        </form>

        <?php
    } else {
        $ID = $_POST['ID']; //This will contain the value that was selected in the drop down menu above.

        $result = mysql_query("SELECT NUMBERS2 FROM GeneratedNumber WHERE ID='{$ID}'"); //This tells the script to select NUMBERS2 contents which is assosiated with the ID.

        while ($row = mysql_fetch_array($result))
            $NUMBERS2 = $row['NUMBERS2'];

        echo "$NUMBERS2"; //Testing reasons - remove it later.
        echo "<br />"; //same as above
        echo "$ID"; // as above.
        echo "<br />"; //as above
        //Convert NUMBERS2 back into an array when inserted into table from Generator.php
        $NUMBERS = explode(",", $NUMBERS2);


        // Counts how many times a number appears
        //*******************************************************************************************************************
        //IF ALL ELSE FAILS, REVERT BACK TO THIS.
        //print_r(array_count_values($NUMBERS));
        //It will only print the statement, but gives you a good building block when things WILL go wrong.
        //*******************************************************************************************************************

        rsort($NUMBERS);
        print_r(array_count_values($NUMBERS));

        $result = mysql_query("SELECT MAXIMUM_NUMBER FROM GeneratedNumber WHERE ID='{$ID}'");
        while ($row = mysql_fetch_array($result));
        $MAXIMUM_NUMBER = $row['MAXIMUM_NUMBER'];

        <?php
    //call the information for generate rate for the loop

    $result = mysql_query("SELECT GENERATE_RATE FROM GeneratedNumber WHERE ID='{$ID}'");


    //*******************************************************************************************************************
    // work out how to break into the array so 
    // CREATE A LOOP SO THAT IT LOOPS AT GENERATE_RATE (E.G, 15), 
    // IF NUMBERSELECTEDFROMARRAY IS BETWEEN 0-25 THEN INSERT INTO CELL_RATE[1]
    // IF NUMBERSELECTEDFROMARRAY IS BETWEEN 26-50 THEN INSERT INTO CELL_RATE[2]
    // IF NUMBERSELECTEDFROMARRAY IS BETWEEN 51-75 THEN INSERT INTO CELL_RATE[3]
    // IF NUMBERSSELECTEDFROMARRAY IS BETWEEN 76-100 THEN INSERT INTO CELL_RATE[4]
    //end
    //*******************************************************************************************************************
    // send data into mysql database? decide later :@
    //insert link to continue -->
    }
    ?>

</body>
[/php]

(I reached word count)

I have been stuck on this for a while and it is now doing my head in. I’m not looking for a direct answer, but what I need to look at in order to get it to do what I want.

Any help is greatly appreciated!

Create your numbers and put them into the database. Then, use SQL to create your table for you. You can pull the records using count(unique(numbers)). This would pull the number and the count(number of this number).
So, if that is what you want. Then, order this by count-desc and it would give you the highest number first, lowest last. Is this what you want? If so, query something like:

$query=“SELECT number, count(distinct number) FROM numbers ORDER BY count(distinct number) DESC”;

Something like that should return a table from the query containing a number and count of it ordered high to low.
I am not good at SQL, I should study it more, so, I am not exactly sure if it is correct. I could test it for you, but,
thought you might want to do that yourself. If you can’t get it working, let us know… Good luck…

Sponsor our Newsletter | Privacy Policy | Terms of Service