mercredi 8 avril 2015

Loop through array, add if needed, get count again


Vote count:

0




I have a set of data that I need to loop through, that contains a number of pieces of information, Quantity, decimalLength (in inches), purchaseLength (in feet).


I set up my loops by first pulling the data from MySQL with a Result>If>While loop, then I use a second one to loop through each section of the first result. What I need to do not is loop through the final set of data and check to see if the value for a given row is smaller than or equal to an item in an array, if not, go to the next item in the array and check again, and if it reaches the end, then it needs to add a new item to the array, and use that value.


I have it started, but I am not sure where to go from here... code as follows:



<?php

$servername = "redacted";
$username = "redacted";
$password = "redacted";
$dbname = "redacted";
$conn = new mysqli( $servername, $username, $password, $dbname );
$sql = "SELECT DISTINCT material FROM temptable WHERE material != 'MISC STEEL'";
$result = $conn->query( $sql );
if ( $result->num_rows > 0 ) {
while ( $row = $result->fetch_assoc() ) {
$material = $row[ 'material' ];
$sql2 = "SELECT cutLengths, material, decimalLength, purchaseLength FROM temptable WHERE material = '$material'";
$result2 = $conn->query( $sql2 );
if ( $result2->num_rows > 0 ) {
echo "<h2>$material</h2><hr/>";
while ( $row2 = $result2->fetch_assoc() ) {
$arr = array( );
array_push($arr, $row2[ 'decimalLength' ]);
for ( $i = 0; $i < $row2[ 'cutLengths' ]; $i++ ) {
echo $row2[ 'decimalLength' ] . "<br/>";
for ( $j = 0; $j < count( $arr ); $j++ ) {
if ( $row2[ 'decimalLength' ] <= $arr[ $j ] ) {
$arr[ $j ] = $arr[ $j ] - $row2[ 'decimalLength' ];
echo $arr[ $j ] . "<br/>";
}
elseif ( $row2[ 'decimalLength' ] > $arr[ $j ] ) {
$arr[] = $row2[ 'decimalLength' ];
}
}
}
}
echo "<h4>(" . count( $arr ) . ") Total " . $row2[ 'purchaseLength' ] . "' Length(s)</h4>";
}
}
}

?>


I know I am on the right track, and I know it can be done, I just don't know what to do next.



asked 3 mins ago







Loop through array, add if needed, get count again

Aucun commentaire:

Enregistrer un commentaire