John Davidson

php - Showing all results also when not present in 2nd optional database

0 comments
Message:


An explaination of how my code works:
I have a pure select on a database that is filled through a different program. I am selecting data to check up on orders. Now I want to give a ready status to the order, which cannot be inserted into the database I am selecting from, so I created a small different database to insert the ordernumber (from the previous selection) into that database and insert a 1. This can be updated to a 0 by pressing the button again.


Now I want to show my orders as the following:


An order can have the status, but doesn't have to have a status.
If it doesnt have a status it should show in the list.
With my current code (below) it only shows orders that are present in the 2nd optional database, I understand why this is happening. But I can't manage to work out a solution to show everything even if an ordernumber is not present in the 2nd optional database.


while( $row = sqlsrv_fetch_array( $stmt1, SQLSRV_FETCH_ASSOC) ) {
$statusorder = $row['ordernr']; //Ordernumber selected in primary database
$sqlstatus = "SELECT status from status WHERE Ordernr = '$statusorder'"; //selecting the status from optional database
$result = $conn2->query($sqlstatus);
while($row2 = $result->fetch_assoc()) {
<insertmyhtmlhere>
}
}


My Answer:
"SELECT * FROM status_table WHERE ordernr = '$statusorder'"; //Selecting status from 2nd optional database_

$stmt2 = sqlsrv_query( $conn2, $sqlstatus ); //Executing query on 2nd optional database_

$row2 = sqlsrv_fetch_array( $stmt2, SQLSRV_FETCH_ASSOC ); //Fetching status from 2nd optional database_

if($row2) {
//Order has a status, display it
echo "Order Number: " . $row['ordernr'] . " - Status: " . $row2['status'] . "
";
} else {
//Order does not have a status, display it as well
echo "Order Number: " . $row['ordernr'] . " - Status: Not Set
";
}
}

In this code, we first select the order numbers from the primary database. Then, we check if each order number has a corresponding status in the 2nd optional database. If it does, we display the order number and its status. If it doesn't, we display the order number with a message indicating that the status is not set.

This way, all orders will be displayed, regardless of whether they have a status or not.

Rate this post

5 of 5 based on 7785 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us