Page 1 of 1

Cleaning up code with IF ?

Posted: Tue Oct 07, 2003 9:15 am
by sergei
Deletenews.php file

Code: Select all

<?php
				$relative_script_path = '.'; 
				include("$relative_script_path/config/config.php"); 
				$connection = mysql_connect($DBHOST,$DBUSER) 
					or die("Connection unsuccessful: " . mysql_error());    								
				
				mysql_select_db($DBASE) 
				or die("Connection to database unsuccessful: " . mysql_error()); 
				
				$sql = "SELECT * FROM news_table"; 

				$result = mysql_query($sql,$connection); 

				echo "<TABLE BORDER=2 align=center width=472>";
				echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";
				
				while ($row = mysql_fetch_array($result))
				{
					echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
					echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
					echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
					echo "<TD>".$row["shortstory"]."</TD></TR>";
				}
				echo "</TABLE>";			

				mysql_close($connection);

?>
Delete.php file

Code: Select all

<?php

				$relative_script_path = '.'; 
				include("$relative_script_path/config/config.php"); 
				$connection = mysql_connect($DBHOST,$DBUSER) 
					or die("Connection unsuccessful: " . mysql_error());    								


				mysql_select_db($DBASE) 
				or die("Connection to database unsuccessful: " . mysql_error()); 
				
				$sql = "DELETE FROM news_table WHERE id=$id"; 
				mysql_query($sql,$connection) or die("Message could not be deleted: " . mysql_error()); 
					echo("Your selected row was successfully deleted. Thank you.<br /><br />");
							
				mysql_close($connection);

?>
Can someone help me combine these two file into one so that when I delete the record it returns to the initial view / delete section.

Thanks.

Posted: Tue Oct 07, 2003 9:29 am
by Nay
I think you mean shownews.php rather than delete.php for the first one, since I don't see any deleting anywhere but outputting everywhere.

Maybe something like:

Code: Select all

<?php

$action = $_GET['action'];

if(isSet($action) {

if($action=="delete") {
// delete the news
}

else {
// show the news
}

}

else {
die("Action not set");
}

?>
Is that what you meant?

-Nay

Posted: Tue Oct 07, 2003 10:01 am
by sergei

Code: Select all

<?php

$relative_script_path = '.'; 
include("$relative_script_path/config/config.php"); 
mysql_connect($DBHOST,$DBUSER) 
	or die("Connection unsuccessful: " . mysql_error());    								

mysql_select_db($DBASE) 
or die("Connection to database unsuccessful: " . mysql_error()); 

$action = $_GET['action']; 

if(isSet($action) { 

if($action=="delete")  { // delete the news 
	$sql = "DELETE FROM news_table WHERE id=$id"; 
	mysql_query($sql) or die("Message could not be deleted: " . mysql_error()); 
		echo("Your selected row was successfully deleted. Thank you.<br /><br />");

	$query = "SELECT * FROM news_table"; 

	$result = mysql_query($query); 

	echo "<TABLE BORDER=2 align=center width=472>";
	echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";
	while ($row = mysql_fetch_array($result))
	{
		echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
		echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
		echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
		echo "<TD>".$row["shortstory"]."</TD></TR>";
	}
	echo "</TABLE>";
} 

else { // show the news 

	$result = mysql_query("SELECT * FROM news_table LIMIT 5");  //$start,$limit");  

	echo "<TABLE BORDER=2 align=center width=472>";
	echo"<TR><TD><B>Title</B></TD><TD><B>Author</B></TD><TD><B>Short Story</B></TD></TR>";

	while ($row = mysql_fetch_array($result))
	{
		echo "<TR><TD>".$row["title"]."<br />".$row["author"]."</TD>";
		echo "<TD><a href="viewnews.php?id=".$row[id]."">View</a>&nbsp; ";
		echo "<a href="delete.php?id=".$row[id]."">Delete</a></TD>";
		echo "<TD>".$row["shortstory"]."</TD></TR>";
	}
	echo "</TABLE><br />";			

}



?>
Like this it gives me the following error: Parse error: parse error, unexpected '{' in C:\Apache2\htdocs\metering\deletenews.php on line 343

line 343 equals

Code: Select all

if($action=="delete")  {

Posted: Tue Oct 07, 2003 1:14 pm
by jason
The problem is here:

Code: Select all

if(isSet($action) {

Posted: Tue Oct 07, 2003 6:03 pm
by Nay
Oh yeah, of course. Sorry dude, and thanks Jason. It should be:

Code: Select all

if(isSet($action)) {
-Nay

Posted: Wed Oct 08, 2003 2:43 am
by sergei
Thanks for the change. I've done and it doesn't give me the error again.

Now nothing displays. Is this because the if statement is resolving to TRUE ?

How can I have the else statement display first and when you click to delete a record, the if statement displays ?

Thanks

Posted: Wed Oct 08, 2003 2:48 am
by Paddy
Change

if($action=="delete")

to

if($action!="delete")

Although I can't see how this would be your problem. And obviously switch your code blocks around.

Should

$row[id]

be

$row["id"]

?

I can't see any errors in the rest of your code.

Posted: Wed Oct 08, 2003 3:09 am
by sergei
Works with this code.

Code: Select all

if(!isSet($action)) { 
								
								if($action!="delete")  {
Thanks for the help.