dodo22 Posted September 11, 2016 Share Posted September 11, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/ Share on other sites More sharing options...
lovephp Posted September 11, 2016 Share Posted September 11, 2016 Something like this it should be if($record["rstatus"] == "0"){ echo 'waiting'; }elseif($record["rstatus"] == "1"){ echo 'shipping'; } else { echo 'delivered'; } Its just an example, you can use elseif to add more. Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537365 Share on other sites More sharing options...
Solution Psycho Posted September 11, 2016 Solution Share Posted September 11, 2016 (edited) 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 September 11, 2016 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537368 Share on other sites More sharing options...
Jacques1 Posted September 11, 2016 Share Posted September 11, 2016 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”. Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537384 Share on other sites More sharing options...
dodo22 Posted September 12, 2016 Author Share Posted September 12, 2016 Thank you all help! Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537413 Share on other sites More sharing options...
benanamen Posted September 12, 2016 Share Posted September 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537442 Share on other sites More sharing options...
Jacques1 Posted September 12, 2016 Share Posted September 12, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537448 Share on other sites More sharing options...
Psycho Posted September 12, 2016 Share Posted September 12, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302149-order-item-status-problem/#findComment-1537453 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.