I have an array, $ scans. I want to query MySQL with all the values in this array and return the results to the array. For example, sample data in a scan will be as follows:
E1234
E2244
E3654
The MYSQL table PARTShas fields part, size, length, plate, side, type.
I want to end with $output["E1234"][0]as a result of the size for this part, 1 is the length, etc., and I want the array to be sorted by the MYSQL query. (order SIDE desc, PLATE asc).
Right now, I'm just stepping over the array $SCANSand making a query after the query, but I cannot sort all the results correctly.
Is it possible? This is what I am doing now, but obviously, since each query returns a single row, which is then output, there is no sorting. I want to be able to execute one query, sort the results in an array and then output this data after sorting.
foreach ($scans as $currentscan) {
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$query = "SELECT french, length, plate, side, type FROM parts WHERE part = '$partselect' ORDER BY side DESC, plate ASC LIMIT 1";
$result = mysql_query($query);
$error = mysql_error();
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
echo 'BAD PART: '.$currentscan.' '.$partselect.' error is '.$error.'<br \>
';
} else {
$row = mysql_fetch_array($result);
print $partselect.' is a '.$row['french'].'/'.$row['length'].' - '.$row['type'].' - '.$row['plate'].'('.$row['side'].')<br \>';
}
};
EDIT: this is the code as it now follows some suggestions here:
$scans = explode("\n",$_POST['scans']);
foreach ($scans as $currentscan) {
if ($currentscan[0] == "E") {
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$tempQuery .= 'part = "'.$partselect.'" OR ';
};
};
$tempQuery = substr($tempQuery, 0, -3);
$tempQuery .= ") ORDER BY side DESC, plate ASC LIMIT 1";
$query = "SELECT french, length, plate, side, type FROM parts WHERE ".$tempQuery;
$result = mysql_query($query);
echo $result;
while($row = mysql_fetch_array($result)){
print $row['french']." / ".$row['length'];
}
result:
Warning: mysql_fetch_array (): the provided argument is not a valid MySQL result resource in /home/foo/bar/sort.php on line 35
Line 35 while ($ row = mysql_fetch_array ($ result)) {
COMPLETION:
It works.
if (!$_POST) {
print '
<html>
<body>
Scans:
<form action="index.php" method="post">
<textarea rows="20" cols="6" name="scans" id="scans">
</textarea>
<br />
<input type="submit" value="Submit" />
</body>
</html>
';
} else {
mysql_connect(SQLSERVER, SQLUSER, SQLPASSWORD) or die("Can not connect to DB server.");
mysql_select_db(DATABASE) or die("Can not connect to Database.");
$scans = explode("\n",$_POST['scans']);
foreach ($scans as $currentscan) {
if ($currentscan[0] == "E") {
$partselect = substr(mysql_real_escape_string($currentscan), 0, 5);
$count{$partselect}++;
$tempQuery .= 'part = "'.$partselect.'" OR ';
};
};
$tempQuery = substr($tempQuery, 0, -3);
$tempQuery .= ") ORDER BY side DESC, plate ASC";
$query = "SELECT part, french, length, plate, side, type FROM parts WHERE (".$tempQuery;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
for ($i = 0; $i < $count{$row['part']}; $i++) {
print $row['part']." ".$row['french']." / ".$row['length']." ".$row['plate']."(".$row['side'].")<br>";
};
};
};
?>