Jump to content

order item status problem


Go to solution Solved by Psycho,

Recommended Posts

Hello! I am a beginner student, and I have a problem.
I would like to write the order items' status. This code works, so if the order status = 0 the program writes "waiting", else writes "delivered".

But what if I would like to add one more status (for example "shipping") then how can I do that? Because I cannot use code this way:  
while ($record = mysqli_fetch_assoc($table)) { $status= $record["rstatus"] == 0 ? "waiting" : "delivered"; 

I tried the if else code, when the order status=0 write "waiting",  if else order status=1 write "shipping", else write "delivered" but it does not work or I wrote wrong.

(I hope you understand my English. This is not my native language)

<?php
  
    require_once("header.php");
  
    if (isset($_SESSION["auid"])) { 
  
  ?>
      <div class="container">
       <h1 class="text-center">Orders</h1>
 			<table width="100%">
 				<tr style="font-weight: bold;">
 					<td>Order number</td>
 					<td>Date</td>
 					<td>Payment</td>
 					<td>Order status</td>
 					<td> </td>
 				</tr>
 <?php
 				while ($record = mysqli_fetch_assoc($table)) {
 					$status= $record["rstatus"] == 0 ? "waiting" : "delivered";
 					
 					$icon = $record["rstatus"] == 0 ? "glyphicon-ok" : "glyphicon-time";
 					$color = $record["rstatus"] == 0 ? "text-danger" : "";
 ?>
 					<tr class="<?php print $color; ?>">
 						<td><a class="btn btn-link" style="padding-left: 0px;" data-toggle="modal" data-target="#OrderItemsModal" onclick="OrderItemsList(<?php print $record["rfid"]; ?>)"><?php print $record["rfordernumber"]; ?></a></td>
 						<td><?php print $record["rfdate"]; ?></td>
 						<td><?php print $record["fname"]; ?></td>
 						<td><?php print $status; ?></td>
 						<td><a class="btn btn-link" href="index.php?rfid=<?php print $record["rfid"]; ?>"><span class="glyphicon <?php print $ikon; ?>"></span></a></td>
 					</tr>
 <?php
 				}
 ?>
 			</table>
 		</div>
 <?php
 
   }
 
   require_once("footer.php");
 
 


Quick Edit

Link to comment
https://forums.phpfreaks.com/topic/302149-order-item-status-problem/
Share on other sites

  • Solution

Ideally, I would have a separate table with the status descriptions and join it on the orders when querying those records. But, do do it in the code I can think of a couple other solutions in addition to lovephp's response:

 

 

1. Use a switch statement

switch($record["rstatus"])
{
    case 0:
        $status = 'waiting';
        break;
    case 1:
        $status = 'shipping';
        break;
    case 2:
        $status = 'delivered';
        break;
}

2. Use an array. Define the array at the top of the script such as thi

$statusText = array(
    0 => 'waiting',
    1 => 'shipping',
    2 => 'delivered'
);

Then in the code to create the output, use something like this

$status = $statusText[$record["rstatus"]];
Edited by Psycho
  • Like 1

I would actually avoid those magic status numbers altogether, because they make no sense on their own and require you to constantly explain and convert them.

 

Use an ENUM type where you can actually store strings like “waiting” and “delivered”.

Use an ENUM type where you can actually store strings like “waiting” and “delivered”.

 

Are you really advocating using the actual word value instead of doing as Psycho suggested " I would have a separate table with the status descriptions".

 

I am sure you must be aware of the numerous issues with using enum.

Are you really advocating using the actual word value instead of doing as Psycho suggested " I would have a separate table with the status descriptions".

 

Yes, because it's much simpler.

 

By the way, I can guarantee you that the OP has not created an extra table and just keeps using the old magic numbers together with Psycho's code.

 

 

 

I am sure you must be aware of the numerous issues with using enum.

 

And the problem is what, exactly?

I would agree with Jacques1 on this particular post. This is for educational purposes and I highly doubt that the OP is working on DB normalization at this point. In more complex situations the solution won't always be the same. There would be other variables at play that could tip the scale from one solution to another.

 

I personally prefer using an int value for something like a status and using a lookup table for the textual values. That way I can build the logic around those int values and change the textual values if needed. For example, what is "shipping"? Is it "preparing for shipping" or is it "shipped"? I could see changing that value in the future based on user feedback. Plus, if the application needs to be multi-lingual it makes even more sense (IMHO). However, for a simple application that is for educational purposes or a 'real' one that will not require a lot of enhancements I see no problem with an ENUM.

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.