This question may have been answered, but I have been Googling and searching this site for over a week now and have not come across anything that applies to this situation. Yes, this is a homework assignment, and I have exhausted my resources (the entire class is confused). Any help or links to relevant resources would be appreciated.
Problem: I have a program where the user can submit "bug reports". These bug reports are all written to the same text file and are each treated as an array. Each report is also displayed with a "Details" link. I need to write a hyperlink that will essentially assign the link to the index value of the array without hardcoding it (because the link will need to work for future updates) and then have that specific report displayed in a separate page. I'm submitting a photo of the display table with the link in it so that there's a visual of this allow with the specific code for the display table.
Image of display table
$count = count($ReportArray);
for($i = 0; $i < $count; ++$i) {
$CurReport = explode("~", $ReportArray[$i]);
//bug description line and details link
echo "<tr>";
echo "<td><p style='text-align: center;'><strong>Bug Description: </strong></td>";
echo "<td colspan='6' style='text-align: center;'>".htmlentities($CurReport[0])."<br/></td>\n";
//$CurArray = implode("~", $CurReport);
//assign details link an array - no idea how
$num = count($ReportArray); //total number of reports
echo '<td style="text-align: center;">';
echo '<p>Details</p>';
echo "</td></tr>\n";
//other bug report fields
echo "<tr>";
echo "<td></td>";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[1])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[2])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[3])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[4])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[5])."<br /></td>\n";
echo "<td style=\"text-align:center;\">".htmlentities($CurReport[6])."<br /></td>\n";
echo "<td></td>";
echo "</tr>\n";
//display updates
echo "<tr>";
$line_count = count($ReportArray);
if($line_count > 7) {
echo "<tr>";
echo '<td style="text-align:center; text-weght:bold;">Update</td><td>';
echo htmlentities($CurReport[$line_count - 1]).'<br/>\n';
echo "</td>";
echo "</tr>\n";
}
}
So here's how the program works:
bug_List.php: Has the form for bug reports and then a table to
display the submitted and validated reports. These reports all
written and read from the same text file. BugReportCreate.php:
This file validates the input and writes it the text file. BugReport.php: This file is used to display each individual report and allows the user to submit updates for the report currently displayed. BugReportUpdate.php: writes/appends the update to the specific report from BugReport and displays the updated report. bug_List.php will also display the updated report(s)
In my program, the href redirects to another page with the $id, and I am able to edit the row with that id from that page (signoutteacher.php). No I also want to be able to transfer the location from the input box, but I need a button to do so. Does anyone have an idea of how I could do both using only one button?
while ($row = mysqli_fetch_array($result)){
$id = $row['id'];
if ($row['status']== "In"){echo "<tr class='w3-hover-green w3-large'>";
echo "<td width='10%'>".$row['name']."</td>";
echo "<td width='10%'>".$row['surname']."</td>";
echo "<td width='5%'>".$row['status']."</td>";
echo "<td width='20%'><form action='Teacher.php' method='post'><input type='text' name='location'></form></td>";
$location=$_POST['location'];
echo "<td width='10%'>"."
<a href='signoutteacher.php?myId=".$id."&myLocation=".$location."' class = 'w3-btn w3-input w3-green w3-round-jumbo'>Sign Out</a>
</td>";
echo "</tr>";
The table heading have already been printed out, and everything else works fine when not considering the location. The error message says that the index 'location' is not defined. I know that I need to submit the input, but how?
Instead of including the $id in the url, I created a form where the $id is a hidden input and sent that to the form along with the location. Now everything works fine.
I'm coming here after some head banging. Still learning php/mysql and coming across an issue which is bothering me for a while.
Currently I print a table with all the data without any condition. The values below the lower values and above are printed in this table. The snippet of my code is below:
while ($result = mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>" .$result['id']. "</td>";
if ($result['time'] > $limit[lower]){
echo "<td bgcolor=#ff0000>" $result['time']. "</td>";
} else {
echo "<td>" .$result['time']. "</td>";
}
echo "<td>" .$result['name']. "</td>";
echo "</tr>";
}
I want to create a href link so that by dafault only values above the lower are printed and on clicking the link, say detailed_view will give the table as it does above i.e. all value including below and above the lower value. Shall I create two methods and use them in href separately. In that case there will be two while loops which I want to avoid. Any pointer on this?
The name of the script is display.php, so before the loop I tried using:
echo "<a href='display_jobid.php?view=$num'>Show All</a>";
It does create a hyperlink Show All but how to get the functionality working, am not sure.
Thanks
I have this codes so far:
This form is generated during a query loop
while ($row = mysql_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['breed'] . "</td>";
if($row['neuteredOspayed']=="1"){
echo "<td>" . "neutered" . "</td>";
}else
echo "<td>" . "spayed". "</td>";
echo "<td>" . $row['priceFee'] . "</td>";
if($row['housebroken']=="1"){
echo "<td>" . "yes" . "</td>";
}else
echo "<td>" . "no". "</td>";
if($row['goodwithdogs']=="1"){
echo "<td>" . "yes" . "</td>";
}else
echo "<td>" . "no". "</td>";
if($row['goodwithcats']=="1"){
echo "<td>" . "yes" . "</td>";
}else
echo "<td>" . "no". "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
Now, is there a way to put a link saying "delete" next to every result? For example next to the status field?
Here is what it's looking like:
To get this deleted I guess that I need to spot the record somehow. What I need is to take the name of the animal. For example, how can I get the value "Sparky" from the table and assign it to a variable? If I have the name I would be able to make the checks and run a query witch will delete the record.
You will have to use Javascript and AJAX.
Basically, you'll want to put a button in a cell, one per row, and when it's clicked, pass the id of the record to be deleted back to a PHP script via AJAX, then remove the record from the database. It should also hide the row when it is clicked.
I'd recommend using jQuery's .ajax(), as raw XHR is painful at best.
There is no way to do this with just PHP, because HTTP is stateless. Once your web page is loaded, the HTML and PHP parts are done, and fixed. You'll HAVE to use Javascript to make consecutive requests.
Alternatively, as bcmcfc points out, you can also just have a hyperlink to a script that will delete a record from your database.
You'd need something like this in your while loop:
echo '<td>Delete</td>';
Using the table's primary key would be better than the name though - is the name unique in the db? Assuming there is a PK and it's called id:
echo '<td>Delete</td>';
Php just is for page generation, once you have generated that table you cannot modify it.
You could, however, make a new get request, specifying a parameter with the row name to delete, you have to change your server php code to take in account this parameter, though.
The best, according to me, is using javascript: you assign a td id to each row and then you write a simple function in which you delete that row.
You have to submit the form and do this action....
<input type="submit" name="submit" value="Delete" />
After submit the form will redirected to some page. In that page you got all the posted values. In that you can delete the record by using the record id otherwise you use the name for appropriate record.
$query = "DELETE FROM table_name WHERE name='$_POST['name']'";
(or)
$query = "DELETE FROM table_name WHERE id='$_POST['id']'";
After this execution you have to redirect the URL to that page.
(or)
Delete
In this file you have to written like this...
$id = $_REQUEST['id'];
$query = "DELETE FROM table_name WHERE id='$id'";
//Add this form before the end of the while loop
<form action="#" method="post">
<input type="hidden" name="name" value="<?php echo $row['name']?>">
<input type="submit" name="delete" value="delete">
</form>
//Add this at the end of the coding
<?php
if(isset($_POST['delete']))
{
//database connection
$sql="DELETE FROM `table_name` WHERE `name`='{$_POST['name']}'";
$queryEXE=mysql_query($sql);
}
?>
You add a "elemID" attribute to each of your <tr>s and a new class that would be individual for each row (for example, elem+the id):
<tr elemId="12" class="elem12">...</tr>
Then, for the Delete link, you use AJAX to call the page that deletes the row from your DB, passing the elemID as an argument to this function:
function deleteRow(thisElemID) {
$.ajax({
url: yourURL,
data: {'elemID', thisElemID },
success: function() {
$('.elem'+thisElemID).remove();
}
});
}
More on $.ajax here.
Create another cell in your table and call ajax onclik of that button. Inside your ajax call delete the pecific row
Use a 'delete' form for each row.
The best way would be to add, for each row, a form with a submit button. Each form has a hidden field containing the id (or name, if you must) of the record to delete. Set the method of the form to 'post'.
Reasons why:
A delete link (a href) can be indexed and bookmarked easily. You
don't want pets to be deleted everytime Google drops by or a user is
browsing through their history.
A method='get' form also can cause
the url to be bookmarked and/or show up in the history. The method
with a form will work without Javascript altogether.
Apart from that,
Any solution (both forms and links) can easily be 'upgraded' to a solution with Ajax for a better user experience, but that's always an extra and no core functionality.
If you like, you can style the submit button to look like a link.
Use the post-redirect-get pattern if you implement this solution.
That is, assuming you want to actually remove the record from the database. If you just want to alter the HTML, a Javascript soliution would make more sense.
I'm currently facing a strange issue whereby I did not get any errors from my debugging page. My table consists of several rows and only the first row of the table can't be deleted.
Sample form:
$DB = new PDO('sqlite:database/Sample.db');
$result = $DB->query("select * from staff");
foreach ($result as $row)
{
$StaffNo= $row['StaffNo'];
$Name= $row['Name'];
$TelNo= $row ['TelNo'];
echo "<tr>";
//Go to remove.php to remove
echo "<form action=\"Remove.php\" method=\"post\">";
echo "<input type=\"hidden\" name=\"StaffNo\" value=\"$StaffNo\">";
echo "<input type=\"submit\" onclick=\"return confirm('Yes/No')\"/>";
echo "</form>";
echo "</td>";
echo '<td data-column-name="Name" char="data">'.$Name.'</td>';
echo '<td data-column-name="TelNo" char="data">'.$TelNo.'</td>';
</tr>
}
Remove.php:
$StaffNo= $_POST["StaffNo"];
$DB = new PDO('sqlite:database/Sample.db');
$DB->query("DELETE FROM Staff WHERE StaffNo=".$StaffNo);
#header("location:view.php");
From my code above, I can delete all my sample records except for the first row. It doesn't get deleted... Kindly advise if i did wrong somewhere....
I've tried your code and apart from the broken table code, everything seems fine. Make sure your table is correct (<table><tr><td>Content</td></tr></table>). In your question, you're missing an opening <td> on line 9 of the first file, as well as missing <table> tags. Some browsers don't handle broken tables very well and that might mess up your form.
Your query will also break if $StaffNo is an empty string, so double check that.
You can also try removing the header() call and print out errors using $DB->errorInfo().
To inject your variable i the hidden field you should type
".$StaffNo."
instead of
"$StaffNo".
probably it doesn't delete the first row of your table becouse it's the only one with a StaffNo defined.