I am getting data from a mysql DB using PHP and then displaying it inside a DIV on a web page.
the data Im fetching is a list of task and I want the users to be able to delete a task once completed.
Image here:
http://www.picpaste.com/Tasks-esmvwRPX.jpg
(apparently I need 10 reputation points before I can embed a picture in my post!)
This is my PHP code used to display the date:
<section class="tasks">
<div id="all-tasks">
<fieldset class="tasks-list">
<?php
$strSQL = "SELECT Tasks.bolComplete, Tasks.strTask, Tasks.datDueDate FROM Tasks WHERE Tasks.idPeople ='" . $_SESSION['UserID'] . "' AND Tasks.bolComplete ='0' ORDER BY Tasks.datDueDate";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo '<label class="tasks-list-item left">';
if ($row['bolComplete'] == 1) {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" checked>';
}
else {
echo '<input type="checkbox" name="task_1" value="' . $row['bolComplete'] . '" class="tasks-list-cb" unchecked>';
}
echo '<span class="tasks-list-mark"></span>';
echo '<span class="tasks-list-desc">' . $row['strTask'] . '</span>';
// Convert Date format
$originalDate = $row['datDueDate'];
$newDate = date("d-m-y", strtotime($originalDate));
echo '<span class="tasks-list-date">' . $newDate . '</span>';
echo '</label>';
$i++;
}
?>
</fieldset>
</div>
</section>
How can I capture the index of the row displayed so I can do my delete query against the task that is selected?
I have a primary key on idTask (not displayed in the task list) which I need to capture for my delete statement.
Any help is greatly appreciated.
Thanks in advance.
M.
you can assign you idTask to your lable i.e.
<label id="idTaskXXX" class="tasks-list-item left">
...
<lable>
then where ever your code is which is checking for what row is clicked read the id field and use that for deletion
here is an example of how to get id from element: click here
<label id="task1" value="Change" onclick="get_id(this.id)">
Task1
</label>
<label id="task2" value="Change" onclick="get_id(this.id)">
Task2
</label>
javascript to get id:
function get_id(id){
alert('My Id is ' + id);
}
Related
I have a long list of over 1000 parts, I then display a list of related parts depending on the make and model that was selected.
After selecting the parts by providing the number of items per part it get stored as $_SESSION['var55'] = 1;
On refresh of the page for whatever reason I want to rebuild the form but this time adding the number of items/part per part by allocating the var value to the html value parameter.
ex.
if(isset($_SESSION['var55']) {echo 'value="' . $_SESSION['var55'] . '"'
Should then provide:
<input id="55" name="55" value="1" style="color:blue" type="number" min="0" max="5">
I have tried to construct an array but then again had difficulty with selecting the correct id using the $variables.
I cant seem to populate the correct id using variables to match the $_SESSION['var'] to the input ID while running the for eachloop that draws the info from the DB.
$partsQuery = "SELECT
partID,
partNo,
AFN,
partDesc,
brand,
partURL
FROM
lu_part
WHERE
makeID = $makeID AND modelID = $modelID
";
$parts = $db->query($partsQuery);
<?php foreach($parts->fetchAll() as $part):?>
<div class="partItemBox imagehighlight">
<span>
<center><img class="partItems" src="<?php echo $part['partURL'];?>" alt="part">
<label style="margin:16px" for="<?php echo $part['partID']; ?>">PART NO.: <?php echo $part['partNo'];?> / <?php echo $part['AFN'];?><br>DESCRIPTION: <?php echo $part['partDesc'];?><br>BRAND: <?php echo $part['brand'];?></label>
<input id="<?php echo 'var' . $part['partID'];?>" name="<?php echo 'var' . $part['partID'];?>"
<?php if(isset($_SESSION["'var" . $part['partID' . "'"]])) {
echo 'value="' . $_SESSION['\"var\" . $part[\'partID\']'] . '"'; } else {$_SESSION[$part['partID']];} ?>
style="margin-top:0px" type="number" min="0" max="5">
</center>
</span><br>
</div>
<?php
endforeach;
?>
I'm in the middle of making a simple inventory system for keeping track of equipment going in and out of our doors. The inventory is stored in MYSQL, with a table looking like this: id name storage used location_storage location
This is all fun and games when I create a simple form with PHP, so it stays dynamic with the content from the server. I can update all values with no problem.
But for the sake of simplicity I'm looking into having a drop down menu, with a button, that creates/shows input fields in a form. The reason being is that I will have many rows in my table in the upcoming time. As the forms earlier have been made from server information, I will also need the scripts to be dynamic. Right now I'm stuck thinking about what I should do.
As of now, my code for the bits look like this:
"Static" PHP form:
<form action="<?php $_PHP_SELF ?>" method="POST">
<?php
//conn stuff
$sql = "SELECT id, name, storage, used, location FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo ' ' . $row["id"]. ' ' . $row["name"]. '<input type="text" name="newamount[' . $row["id"]. ']" />';
echo '<br>';
}
} else {
echo "0 results";
}
$conn->close();
?>
<input type="submit" name="checkout" value="Check out"/>
<input type="submit" name="checkin" value="Check in"/>
</form>
Check in PHP (check out is identical except for change of + and minus):
if(isset($_POST['sjekkinn'])){
//conn stuff
mysql_select_db( 'experimental' );
$newamount = $_POST['newamount'];
foreach($newamount as $key => $value){
$sql = "UPDATE inventory ". "SET storage = (storage + $value), used = (used - $value)". "WHERE ID = $key " ;
if (empty($value)) continue;
$retval = mysql_query( $sql, $conn );}
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully!<br>";
header("Refresh:1");
mysql_close($conn);
}
Those are all working great, but I would like to use something like the code below for a neater setup... Anyone got any advice?
Drop down list generated from MYSQL:
<form action="#" method="post">
<select name="selectinventory">
<?php
//conn stuff
$sql = "SELECT * FROM inventory";
$result = $conn->query($sql);
while ($row = $result->fetch_array()){
echo '<option value="' . $row["id"]. '">' . $row['name'] . '</option>';
}
?>
</select>
<input type="submit" name="submit" value="Add line">
</form>
Okay, so I found my own solution.
I ended up using my table-populated drop down list as I showed you in the last code box of the question. What came to my mind was that I could simply use the jQuery show/hide function.
What I did was to make a script that told (in "readable") div 'x' to show and move when option 'x' was selected and button clicked. I will show you my complete code in the end.
That way I could have my input fields each created inside a div from my table (the same that populated the drop down list), so I could manage them easier (probably possible to do it easier - but if it ain't broken, don't fix it). These were created outside the form. When I then click the previously mentioned button, the div will move inside the form. The next one I select will end up UNDER the previous one, instead of just showing up in table-order.
Feel free to shout any questions!
Here's the complete code:
<form action="<?php $_PHP_SELF ?>" method="post">
<select name="selectinventory" id="selectinventory">
<option selected="selected">Choose one</option>
<?php
//conn stuff
$sql = "SELECT id, name FROM inventory";
$result = $conn->query($sql);
$sql = "SELECT * FROM inventory";
$result = $conn->query($sql);
while ($row = $result->fetch_array()){
echo '<option value="' . $row["id"]. '">' . $row['name'] . '</option>';
}?>
</select>
<div id="btn">Add line</div>
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("#" + $("#selectinventory").val()).show();
$("#" + $("#selectinventory").val()).appendTo($(".selecteditems"));
});
});
</script>
<?php
//conn stuff
$sql = "SELECT id, name FROM inventory";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<div style="display:none" id="' . $row["id"]. '"> ' . $row["id"]. ' ' . $row["name"]. '<input type="text" name="newamount[' . $row["id"]. ']" /><br></div>
';
}
} else {
echo "0 results";
}
$conn->close();
?>
<form action="<?php $_PHP_SELF ?>" method="POST" name="isthisthearrayname">
<div class="selecteditems">
</div>
<input type="submit" name="checkout" value="Check out"/>
<input type="submit" name="checkin" value="Check in"/>
</form>
I'm in need of a bit help. I'm trying to find out how to associate a specific query (deletion of a record) with not the id of a record, but the record with which another query (selection of a record) is echoed out.
This line of code totally works when the id is specified, but again I need it for the record that gets called, where the id can skip numbers if I delete a record.
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
I've got a table in my phpmyadmin database with columns 'id', 'pagetitle', 'toevoeging' (addition in Dutch) , 'message'. First one is an INT, rest are varchars/text.
This may be a stupid question, I'm sorry for that. I'm still new to PHP, and to programming in general.
Here is the code. I've commented on lines code to clarify. Thanks you!.
<?php
if (isset($_SESSION['email'])) //if the admin is active, forms can be written out.
{
echo '</nav>
<br><br> <div class="inlogscript">
<form action="verstuurd.php" method="post">
<input type="text" placeholder="Titel" method="POST" name="pagetitle" /><br><br>
<input type="text" placeholder="Toevoeging" method="POST" name="toevoeging" /><br><br>
<textarea class="pure-input-1-2" placeholder="Wat is er nieuws?" name="message"></textarea><br>
<input type="submit" value="Bevestigen" />
</form></div>';
}
?>
<div class="mainContent">
<?php
include_once("config.php"); //this is the database connection
$query = "SELECT * FROM paginas "; //selects from the table called paginas
$result = mysqli_query($mysqli, $query);
while($row = mysqli_fetch_assoc($result))
{
$pagetitle = $row['pagetitle'];
$toevoeging = $row['toevoeging'];
$message = $row['message'];
echo '<article class="topcontent">' . '<div class="mct">' . '<h2>' . "$pagetitle" .'</h2>' . '</div>' . "<br>" .
'<p class="post-info">'. "$toevoeging" . '</p>' . '<p class="post-text">' . '<br>'. "$message" . '</p>' .'</article>' . '<div class="deleteknop">' . '<form method="post">
<input name="delete" type="submit" value="Delete Now!">
</form>' . '</div>' ;
} //This long echo will call variables $pagetitle, $toevoeging and &message along with divs so they automatically CSS styled,
//along with a Delete button per echo that has the 3 variables
$querytwo = "DELETE FROM `paginas` WHERE id = 5";
if (isset($_POST['delete'])) //Deletes the query if 'delete' button is clicked
{
$resulttwo = $mysqli->query($querytwo);
}
?>
</div>
</div>
Also here is the Insert INTO query of the records. Thanks again!
$sql = "INSERT INTO paginas (pagetitle,toevoeging, message)
VALUES ('$_POST[pagetitle]','$_POST[toevoeging]','$_POST[message]')";
//the insertion into the table of the database
if ($MySQLi_CON->query($sql) === TRUE) {
echo "";
} else {
echo "Error: ". $sql . "" . $MySQLi_CON->error;
}
This won't be sufficient but, to begin with your echo :
echo '<article class="topcontent">
<div class="mct">
<h2>' . $pagetitle .'</h2>
</div><br>
<p class="post-info">'. $toevoeging . '</p>
<p class="post-text"><br>'.$message.'</p>
</article>
<div class="deleteknop">
<form method="post">';
// you ll want to use $_POST["id"] array to delete :
echo '<input type="hidden" name="id" value="'.$row['id'].'">
<input name="delete" type="submit" value="Delete Now!">
</form>
</div>' ;
I'm working on a form that allows users to apply for several award categories. They can apply for all several categories, but can only apply for each category once.
The award categories are stored in this table:
When a user applies for a category, a row is entered into the following table with the user's nominee_id and award_category_id:
I then is this code to show the user which awards they have applied already, which works fine.
$result = mysql_query("
SELECT
award_subsection.*,
award_subsection.title AS subsection_title,
award_nomination_category.*
FROM
award_subsection INNER JOIN
award_nomination_category
ON
award_subsection.id=award_nomination_category.award_category_id
WHERE
award_subsection.active='1' AND
award_subsection.award_id='$id' AND
award_subsection.additional_function='award category' AND
award_nomination_category.active='1' AND
award_nomination_category.award_id='$id' AND
award_nomination_category.nominee_id='$nominee_id'
");
$category_count = mysql_num_rows($result);
if($category_count > 0) {
echo'
<div id="column_full" class="reverse_margin">
<div id="subsection_heading">
<h3>You have already selected the following award categories:</h3>
</div><!--
--><div id="subsection_content">
<!--
';
while ($row = mysql_fetch_array($result)) {
echo '
--><div id="column_third">
<h3>' . $row['subsection_title'] . '</h3>
</div><!--
';
}
echo'
-->
</div>
</div>
';
}
But my problem is when I also want to render a drop down menu that only shows the categories the user have not applied to. The following code works with the user has only applied for 1 category previously, but when the user applies for more than 1, the drop down menu displays all categories. I suspect this is because since there are 2 rows of application from the same user, the script is running through the table twice and getting all of the categories.
$result = mysql_query("
SELECT
award_subsection.*,
award_subsection.id AS subsection_id,
award_subsection.title AS subsection_title,
award_nomination_category.*
FROM
award_subsection JOIN
award_nomination_category
ON
award_subsection.id!=award_nomination_category.award_category_id
WHERE
award_subsection.active='1' AND
award_subsection.award_id='$id' AND
award_subsection.additional_function='award category' AND
award_nomination_category.active='1' AND
award_nomination_category.award_id='$id' AND
award_nomination_category.nominee_id='$nominee_id'
GROUP BY
award_subsection.id
");
while($row = mysql_fetch_array($result)) {
echo '
<option value="' . $row['subsection_id'] . '">' . $row['subsection_title'] . '</option>
';
}
Is there a way for the php to know which categories were selected in the first query and remove them in the second query? Thx in advance!
I figured it out!!
Here's the code if anyone's interested:
// Check for applied categories
$result = mysql_query("
SELECT
award_subsection.*,
award_subsection.id AS subsection_id,
award_subsection.title AS subsection_title,
award_nomination_category.*
FROM
award_subsection INNER JOIN
award_nomination_category
ON
award_subsection.id=award_nomination_category.award_category_id
WHERE
award_subsection.active='1' AND
award_subsection.award_id='$id' AND
award_subsection.additional_function='award category' AND
award_nomination_category.active='1' AND
award_nomination_category.award_id='$id' AND
award_nomination_category.nominee_id='$nominee_id'
");
$category_count = mysql_num_rows($result);
if($category_count > 0) {
echo'
<div id="column_full" class="reverse_margin">
<div id="subsection_heading">
<h3>You have already selected the following award categories:</h3>
</div><!--
--><div id="subsection_content">
<!--
';
// Create array of IDs that the nominee has already applied for
$selected_categories = array();
while ($row = mysql_fetch_array($result)) {
$selected_categories[] = $row['subsection_id'];
echo '
--><div id="column_third">
<h3>' . $row['subsection_title'] . '</h3>
</div><!--
';
}
echo'
-->
</div>
</div>
';
}
echo'
<form method="post" action="award-nomination-submit.php" data-parsley-validate>
<div id="column_full" class="reverse_margin">
<input type="hidden" name="award_id" value="' . $id . '">
<input type="hidden" name="nominee_id" value="' . $nominee_id . '">
<input type="hidden" name="step" value="' . $step . '">
<h3>Select an Award Category:</h3>
<select class="visual_form" name="award_category" required>
<option value="" selected>Please Select</option>
';
// Build award categories drop down
$result = mysql_query("
SELECT
*
FROM
award_subsection
WHERE
active='1' AND
award_id='$id' AND
additional_function='award category'
");
while($row = mysql_fetch_array($result)) {
// If ID exists in array, don't echo out in drop down
if(in_array($row['id'], $selected_categories)) {
echo'';
}
else {
echo '
<option value="' . $row['id'] . '">' . $row['title'] . '</option>
';
}
}
echo '
</select>
</div>
';
Basically, I created an array out of the IDs that were output from the first sql query. Then in the second sql query, I compared ID with IDs in the array and only echo out the ones that doesn't match :)
I have created a while loop
while($row = mysql_fetch_Array($result)) {
echo '<tr class="record"> <td></td><td>'
. $row['company'] .'</td><td>'
. $row['project'].'</td><td>'
. $row['assignee']. '</td><td>'
. $row['current_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
. $row['next_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['next_date']))
.'</td></tr>' ;
}
This displays correctly but my question is:
How can the user select an individual row so he/she can edit it. I was hoping that the user can select which row they wanted and they would be taken to another screen where they can make the changes. I can figure out how to do the part where I can update the data. But it is the selecting the row and collecting correct data I dont understand how to do.
Embed an <a href='update.php?id=> somewhere in there which carries the record id to the update script as a parameter in the query string:
while($row = mysql_fetch_Array($result)) {
echo '<tr class="record"> <td></td><td>'
// Insert a link with an id parameter.
// Use whatever column value uniquely identifies your row
. ' Edit this! '
// etc...
// etc...
// etc...
.'</td></tr>' ;
}
And retrieve it on the update script via:
// Assuming an integer id was passed...
// if it is some other string value, escape and quote it accordingly.
$id = intval($_GET['id']);
And you will want to check that the user making the edit has permission to edit that record. In other words, verify that the record is owned by the user is logged in, since anyone could technically visit the URL with any id, whether or not it belongs to them. As in:
UPDATE yourtable SET col1 = 'whatever', col2 = 'whatever' WHERE id = $id AND user_id = 'logged_in_user'
Create some sort of primary key (a new field that will be different for each row/entry) in your database. Example: an ID field
Create a separate page for updating that accepts a GET parameter id. Ex: update.php
Validate and sanitize the id parameter (ensure it is a valid id, not an SQL injection attempt, and the user has the privileges to edit this id)
Load info from database for that id. Ex: SELECT fields FROM table WHERE id = $id
Display a form so user can edit the information.
Validate form input and update the table. Ex: UPDATE table SET field1=newVal WHERE id = $ID
First of all, you need to create a primary key in the table, suppose primary_key in this case, so as to access the particular row having a definite and unique ID.
Secondly, add one more column to your table:
while($row = mysql_fetch_Array($result)){
echo '<tr class="record"> <td></td><td>'
. $row['company'] .'</td><td>'
. $row['project'].'</td><td>'
. $row['assignee']. '</td><td>'
. $row['current_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['start_date'])).'</td><td>' ..'</td><td>'
. $row['next_milestone'] . '</td><td>'
. $date=date("d-m-Y", strtotime($row['next_date']))
.'</td>' ;
?>
<td>
<form action='update.php' method='post'>
<input type="hidden" name="id" value="<?php echo $row['primary_key']; ?>">
<input type="submit" value="Edit">
</form>
</td>
<?php
echo "</tr>";
}
?>
And now you know which row's details you need to display in the form at update.php
The code on update.php goes like:
<?php
$id = $_POST['id'];
$result = mysql_query("SELECT * FROM table WHERE primary_key = '$id'");
$info = mysql_fetch_array($result);
?>
<form action='target.php' method='post'>
Company: <input type="text" name="company" value="<?php echo $info['company']; ?>">
Project: <input type="text" name="project" value="<?php echo $info['project']; ?>">
.
.
<input type="hidden" name="id" value="<?php echo $info['primary_key']; ?>">
<input type="submit" value="Save Changes">
</form>
And then on target.php all you have to do is use the command:
mysql_query("UPDATE table SET company='".$_POST['company']."' AND project='".$_POST['project']."' WHERE primary_key='".$_POST['id']."');