Jump to content

Create boolean to indicate if something is complete


AibZilla

Recommended Posts

Hi, I'm fairly new to PHP and to this website as well.. I'm currently working on a project to make a ToDo list for learning purposes, So far I've created a form that grabs entered text from users and displays it dynamically from my server onto the webpage. The first text that the users submit is the title of the list that they create, the list is then displayed below the text box. Once the new list is displayed the user has the option to update, delete, or add a new item to the list. Once the item is added they can also update and delete that item... The next step which I am having trouble with is adding a checkbox next to the items created from the database and using the checkbox as a way to mark the items as complete. I've created a new column in my 'items' table called IsComplete which is a boolean. I'm just having trouble on how to go about creating the if else statement and how to make my form put a checkbox next to items that I create. I know this is a lot to take in and I might not be explaining myself correctly but help would be appreciated greatly. Here is the code

<?php 
// ERROR HANDLING
if (isset($_GET['message'])) {
$message = $_GET['message'];
}
else {
$message = false;
}	
// LIST MANANGEMENT
require('includes/item-brains.php');
$itemMonster = new Items();
if (!empty($_POST)) {
$itemMonster->CreateItem($_POST["new-item-label"],$_POST["new-item-list-id"]);	
}
$existingItems = $itemMonster->GetAllItems($_GET["listid"]);
?>
<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="/view-list.php?listid=<?php echo $_GET["listid"]; ?>" method="POST" name="item-creator">
<input type="text" name="new-item-label" value="" maxlength="100" /><label>Enter Item Name</label>
<input type="hidden" name="new-item-list-id" value="<?php echo $_GET["listid"]; ?>"/> 
<br />
<input type="submit" value="Add New Item!">
</form>

<ul class="existing-items">
<?php 
if (count($existingItems)) {
foreach ($existingItems as $key => $value) {
	echo "<li>".$value['label'].' <a href="delete-item.php?itemid='.$value['id'].'">Delete</a> <a href="update-item.php?itemid='.$value['id'].'">Update</a></li>';
	}


}
else {
echo "<li>there are no items in DB</li>";
}
?>
</ul>
</body>
</html> 

Link to comment
Share on other sites

I know this is very amateur and if more code is needed for help to be provided please let me know.. This is code from the view-list.php page where I know I must create an indication of whether the items are complete or not. I'm sure it's very easy to do I just need a jump start.. thanks in advance.

 

Aib

Link to comment
Share on other sites

To be honest, I never use Boolean data types in MySQL - It's usually easier to use an ENUM type, with yes and no values instead.  I've had problems with Boolean SQL values in the past, but the enum type works great.

 

If you are setting up your database in phpMyAdmin, set the field type as ENUM, and in the 'Length/Values' box, enter:

'no','yes'

 

I don't quite understand where the checkboxes you want are to go though.  Do you want a checkbox after the Delete and Update links?  One for each item in the $existingItems list?

 

What are the checkboxes to do?  As far as I understand, If it's ticked, you want to set the database field to 'yes', and if not set, you want to set the database field to 'no'?

 

You can do something like:

echo "<input type=\"checkbox\" name=\"c".$value['id']."\" value=\"1\">";

 

then, to read those values, you would do something like:

  for ($i=0; $i<$max_id; $i++) {
    $complete[$i] = ($_GET["c".$i] == 1) ? "yes" : "no";
  }

You would now have an $complete array of yes and no values, which you could put into your database

Link to comment
Share on other sites

Thanks Jamdog, I will try this as soon as I am back at my computer. I want the check boxes to appear dynamically with every created item so once that Item is done the user can use the check mark to mark it as done. The user creates a list and then once the list is created the user has the option to create an item for that list [example:  User Creates: Home List and clicks (Add New List) the list is created dynamically and then once that list appears it also has buttons next to it (view) (update) (delete), when the user clicks (view) for Home List he can then add items for home list like Clean Room after that he presses Submit for Clean Room and that appears dynamically as well with the Options to (update) and (delete), I want the check marks to appear this way with the newly created item so that once the user is done with their chore at home they can mark it as done.]

 

Phew.. hope this made a little more sense, either that or I confused you even more, appreciate the help once again.

 

Aib

Link to comment
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.