Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/263386-using-switch-statement-to-shorten-code/
Share on other sites

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.