AibZilla Posted May 30, 2012 Share Posted May 30, 2012 Hi everyone, I just wanted to see if anyone can simplify this for me. I'm new to PHP but I can see that $itemMonster is being called multiple times. If someone can do this with the switch statement and possibly, briefly explain how and why, I would be very grateful. Thanks in advance if(!isset($_GET["itemid"]) && empty($_POST)) { header("Location: /"); } require('includes/item-brains.php'); $itemMonster = new Items(); if (!empty($_POST)) { $updateResult = $itemMonster->UpdateItem($_POST["new-item-label"],$_POST["item-id"]); if ($updateResult) { $currentlistid = $itemMonster->GetListIdByItemId($_POST["item-id"]); header("Location: /view-list.php?listid=$currentlistid&message=item updated"); } } $currentLabel = $itemMonster->GetItemLabelByItemId($_GET["itemid"]); $currentlistid = $itemMonster->GetListIdByItemId($_GET["itemid"]); ?> <html lang="en"> <head> <meta charset="utf-8"/> <title>ToitDoit</title> </head> <body> <?php if ($message) { echo '<p style="color: red;">'.$message.'</p>';} ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="item-updater"> <input type="text" name="new-item-label" value="<?php if ($currentLabel != NULL) { echo $currentLabel; } ?>" maxlength="100" /><label>Enter New Item Name</label> <input type="hidden" name="item-id" value="<?php echo $_GET["itemid"]; ?>" /> <br /> <input type="submit" value="Update Item!"> <a href="/view-list.php?listid=<?php echo $currentlistid;?>">Cancel</a> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/263386-using-switch-statement-to-shorten-code/ Share on other sites More sharing options...
AibZilla Posted May 30, 2012 Author Share Posted May 30, 2012 Am I missing information needed to shorten this? please LMK, i'm using this forum as a learning tool. Quote Link to comment https://forums.phpfreaks.com/topic/263386-using-switch-statement-to-shorten-code/#findComment-1349828 Share on other sites More sharing options...
AibZilla Posted May 30, 2012 Author Share Posted May 30, 2012 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/263386-using-switch-statement-to-shorten-code/#findComment-1349876 Share on other sites More sharing options...
requinix Posted May 30, 2012 Share Posted May 30, 2012 Am I missing information needed to shorten this? Yeah: what you expect a switch to help with. I don't see why you're thinking about using one. Quote Link to comment https://forums.phpfreaks.com/topic/263386-using-switch-statement-to-shorten-code/#findComment-1349917 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.