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 ?>
"; 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'; ?>
$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]