Jump to content

How to extract information from array and display it


Foulish

Recommended Posts

I've been trying to get this to work for a few days now and can't seem to get anywhere with it! Any help is very much appreciated.

 

I am creating a number generator where the user is required to insert the maximum number and generate rate. Maximum number is the highest number that the script will go to, and generate rate is how many numbers should be created.

 

When the script runs, the numbers are stored into a MySQL database. The MySQL database creates a unique ID number so it can be recalled at a later date.

 

The user then needs to go to Sorting.php and selects the record they wish to use. The numbers from this record are then counted and then stored into an array.

 

If the numbers 2,5,5,0,2,2,1,3,5,1 where generated, the output of this new array will be: [5] => 3 [3] => 1 [2] => 3 [1] => 2 [0] => 1  (These arrays can be of any length)

 

I now need to extract this data and show it on screen. I don't know how to approach it.

 

Format example:

 

Number 5 appears 3 times

Number 3 appears 1 times

Number 2 appears 3 times

Number 0 appears 1 times

 

Sorting.php

<?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));
            

 

(The questions when editing posts are hard. I've needed to Google both of them:()

Link to comment
Share on other sites

If you normalized your database, this could all be done in SQL.

 

<?php

$list = '2,5,5,0,2,2,1,3,5,1';
$array = explode( ',',$list );

// This will hold the count of each number
$count = array();

// Loop through numbers
foreach( $array as $number ) {
// Check if there's already a running count for the given number
if( isset($count[$number]) )
	// Increment that count by one
	$count[$number]++;
// Otherwise, this number hasn't been seen before
else
	// Set the count for that number to 1
	$count[$number] = 1;
}

print_r( $count );

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.