SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: Final issue with Delete problem?
-
Mar 7, 2007, 04:01 #1
- Join Date
- Feb 2005
- Posts
- 737
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
[resolved] Final issue with Delete problem?
Dear All,
I'm nearly there with a search and paginated search return, but I’m trying to figure out in php how to make my listings deleting page more advanced, so I’m trying to create a search page with paginated results that lets you delete the results found after the search:
http://www.thevillager.co.uk/search_test/search.php
So if you do:
- A search for nothing it says you must enter some data - works
- A search for the word ‘frog’ and there is term frog it says it cant’t find that word - works
- A search for the letter f it returns results and paginates them- works
…but it’s the deleting issue I have a problem with.
Please try doing a search on the letter f again and delete a record by hitting a checkbox and hitting delete.
It deletes the entry - great
…but instead of saying there are now x entries left and showing the search results left it defaults to Sorry, but you did not enter any text to search on
It confuses me as to why it’s doing this:
PHP Code:<?php
// Start the connection to the database
include('**********************');
$can_i_connect = db_connect(); // by db_connect function is in my include file
if(!$can_i_connect)
{
$msg = "Could not connect to the database";
}
// End the connection to the database
// Start IF the Subit Button is hit delete a record and the html table from the database
if(isset($_POST['Submit']))
{
$total = $_POST['total'];
$td = 0;
$i = 0;
for($i = 0; $i <= $total; $i++)
{
if(isset($_POST["d$i"]))
{
mysql_query("DELETE FROM cms_stories WHERE story_id=".$_POST["d$i"]);
$td++;
}
$msg = "$td record(s) deleted!";
}
if($msg)
{
echo'<table width="100%" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFF00">
<td>'.$msg.'</td>
</tr>
</table><br/>';
}
}
// END IF the Subit Button is hit delete a record and the html table from the database
if($_SERVER["REQUEST_METHOD"]=='POST')
{
$section = trim($_POST['section']);
$searchstring = ($_POST['searchstring'] != "") ? $_POST['searchstring'] : false;
}
else
{
$section = trim($_GET['section']);
$searchstring = trim ($_GET['searchstring'] != "") ? $_GET['searchstring'] : false;
}
if($searchstring === false){
die ("<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#666666'>
<tr bgcolor='#0033FF'>
<td bgcolor='#0033FF'><font color='#FFFFFF'><strong><font color='#FFFF00'>>></font>
No Results Found...</strong></font></td>
</tr>
<tr>
<td bgcolor='#eeeeee'><div align='center'><strong><font color='#0033FF'>Sorry,
but you did not enter any text to search on</font></strong><br>
Please either <a href='search.php'>modify</a> your search term, or visit
our <a href='edit_list.php'>recent stories</a> to find what you are looking
for</div></td>
</tr>
</table>");
}
// START Query to get Records from the database (ie when page loads this is the first thing it will do as delete is not hit)
$sql = "SELECT *, DATE_FORMAT(published_web_date, '%d-%m-%Y') as formatted_date FROM cms_stories WHERE section LIKE '%$section%' AND ( headline LIKE '%$searchstring%' OR body_text LIKE '%$searchstring%' ) order by story_id desc";
$result = mysql_query($sql) or die(mysql_error());
$total_results = mysql_num_rows($result);
$total_per_page = 4;
$number_of_pages = ceil($total_results / $total_per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $number_of_pages)
$page = $_GET['page'];
else
$page = 1;
$pagelinks = 'Page(s): ';
for ($x = 1; $x <= $number_of_pages; $x++) {
if ($x == $page) $pagelinks .= '<b>'.$x.'</b> ';
else $pagelinks .= "<a href='{$_SERVER['PHP_SELF']}?page=$x§ion={$_GET['section']}&searchstring={$_GET['searchstring']}&Submit=Submit'>$x</a> ";
//
}
$start_record = (($total_per_page * $page) - $total_per_page);
$sql .= ' limit '.$start_record.', '.$total_per_page;
$n = 0;
if($total_results <= 0)
{
echo "<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#666666'>";
echo "<tr bgcolor='#0033FF'>";
echo "<td bgcolor='#0033FF'><font color='#FFFFFF'><strong><font color='#FFFF00'>>></font>";
echo " No Results Found...</strong></font></td>";
echo "</tr>";
echo "<tr>";
echo "<td bgcolor='#eeeeee'><div align='center'><strong><font color='#0033FF'>Sorry,
we found no records in the database</font></strong></div></td>";
echo "</tr>";
echo "</table>";
}
else
{
echo "<b><font color='#0066CC'>>></font></b> Here are the <strong>".$total_results."</strong> most recent stories added to the database <br /><br />Here are those results, listed in ascendng order. <br /><br />";
echo "<form name='form1' method='post' action=''>";
echo "<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#666666'>";
echo "<tr>";
echo "<td height='20' colspan='7' bgcolor='#0066CC'><font color='#FFFFFF'><strong><span class='story_listing'>:: Please the latest stories added to the site below...</span></strong></font></td>";
echo "</tr>";
echo "<tr bgcolor='#FFFFCC'>";
echo "<td width='10%'><strong><font color='#666666'><div align='center'><span class='story_listing'>#</span></strong></div></font></td>";
echo "<td width='20%'><strong><font color='#666666'><div align='center'><span class='story_listing'>Publish Date</span></strong></div></font></td>";
echo "<td width='40%'><strong><font color='#666666'><span class='story_listing'>Headline</span></strong></font></td>";
echo "<td width='10%'><strong><font color='#666666'><div align='center'><span class='story_listing'>Picture</span></strong></div></font></td>";
echo "<td width='10%'><strong><font color='#666666'><div align='center'><span class='story_listing'>Video</span></strong></div></font></td>";
echo "<td width='10%'><strong><font color='#666666'><div align='center'><span class='story_listing'>Sound</span></strong></div></font></td>";
echo "</tr>";
}
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_object($result)) {
echo "<tr>";
echo "<td width='10%' bgcolor='#EEEEEE'><input type='checkbox' name='d$n' value='". $row->story_id."'></td>";
echo "<td width='20%' bgcolor='#EEEEEE'><span class='story_listing'><div align='center'>$row->formatted_date</div></span></td>";
echo "<td width='40%' bgcolor='#FFFFFF'><span class='story_listing'><a href='../dffdfd/edit.php?story_id=". $row->story_id."'>". stripslashes($row->headline) ."</a></span></td>";
echo "<td width='10%' bgcolor='#FFFFFF'><span class='story_listing'><a href='..fdedit_picture.php?story_id=". $row->story_id."'><img src='../../images/add-image.png' width='80' height='15' class='no_border'></a></span></td>";
echo "<td width='10%' bgcolor='#FFFFFF'><span class='story_listing'><a href='..dfdfedit_video.php?story_id=". $row->story_id."'><img src='../../images/add-video.png' width='80' height='15' class='no_border'></a></span></td>";
echo "<td width='10%' bgcolor='#FFFFFF'><span class='story_listing'><a href='../fdfdedit_sound.php?story_id=". $row->story_id."'><img src='../../images/add-sound.png' width='80' height='15' class='no_border'></a></span></td>";
echo "</tr>";
$n++;
}
echo "</table>";
echo "<br />";
echo "<input type='submit' name='Submit' value='Delete' class='button'>";
echo "<input name='total' type='hidden' id='total' value='$n'>";
echo "</form>";
echo ''.$previouslink;
if ($nextlink != '') echo ' - ';
echo $nextlink.'<br/>'.$pagelinks.''."\n";
?>
Thanks
ChrisLast edited by 7724; Mar 7, 2007 at 06:02.
-
Mar 7, 2007, 05:28 #2
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
Morning Chris
it's because you aren't re-submitting your search query. although it says in the URL that it is, it isn't!
PHP Code:if(isset($_POST['Submit']))
{
$total = $_POST['total'];
$td = 0;
$i = 0;
for($i = 0; $i <= $total; $i++)
{
if(isset($_POST["d$i"]))
{
mysql_query("DELETE FROM cms_stories WHERE story_id=".$_POST["d$i"]);
$td++;
}
$msg = "$td record(s) deleted!";
}
if($msg)
{
echo'<table width="100%" cellpadding="0" cellspacing="0">
<tr bgcolor="#FFFF00">
<td>'.$msg.'</td>
</tr>
</table><br/>';
}
header("Location: results.php?". $_SERVER['QUERY_STRING']);
exit();
}
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Mar 7, 2007, 05:52 #3
- Join Date
- Feb 2005
- Posts
- 737
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi Mike,
Thanks again for helping.
I thought about re-directing them as you have suggested before, but I'm getting the Cannot modify header information error as you can see by searching and deleting on the term f again.
Some things in php I pick up quite easily and implement them well (although sometimes with a bit of help), but I must admit I find pagination and deleting quite hard!!
Thanks
Chris
-
Mar 7, 2007, 05:55 #4
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
It gets easier as time goes on!
On the very first line of the script put
PHP Code:ob_start();
Mike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Mar 7, 2007, 06:01 #5
- Join Date
- Feb 2005
- Posts
- 737
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Excellent! Not sure what that does, but i'll google it so I know for the future! But it now works thanks!
-
Mar 7, 2007, 06:17 #6
- Join Date
- Aug 2004
- Location
- Manchester UK
- Posts
- 13,807
- Mentioned
- 158 Post(s)
- Tagged
- 3 Thread(s)
lol, it sends any output to a buffer rather than to the browser
www.php.net/ob_startMike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Mar 7, 2007, 06:19 #7
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Fundamentally, do you have to show a message on that page that the records have been deleted? (That's what's causing the error message). If yes then why not have a link the user clicks after it is done?
PS - the header('Location') function does the redirect: it seems perverse to show a message for milliseconds and then redirect to another page. If you MUST do the redirect, why not echo out the refresh meta tag and set the delay to, say, 5 seconds?
PPS - ob in ob_start() means Output Buffering - i.e. put all the data into memory before sending it to the browser.
Edit:
Mike beat me to that answer
-
Mar 7, 2007, 06:35 #8
ob_start
ob_start is one of the output capturing functions. It and a couple others can be used to capture things that would normally be echo'd and put them in a variable.
http://us2.php.net/manual/en/ref.outcontrol.php
Bookmarks