Jump to content

Using Switch Statement to shorten code.


AibZilla

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.