Jump to content

if ordering


onthespot

Recommended Posts

Ok so this is my code for my news section.

I need it to check if the subject is entered first, then the comment and is of adequate length, then if the image is entered, if all these pass, it will run the query and display the echo.

I need some help with the ordering, i think i have broken it! please help

 

	if (!empty($_POST))
{

	if(!$subject || strlen($subject = trim($subject)) == 0)
		echo "Subject not entered";
	else if(!$comment || strlen($comment = trim($comment)) == 0)
		echo "Comment not entered";
	else if(!$comment || strlen($comment = trim($comment)) < 10)
		echo "Comment too short, must be 10 characters at least";
		else if (isset ($_FILES['new_image']))
			{

				$imagename = $subject . '.jpg';
				$source = $_FILES['new_image']['tmp_name'];
				$target = "images/news/".$imagename;
				move_uploaded_file($source, $target);

				$imagepath = $imagename;
				$save = "images/news/" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 150;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;

				$save = "images/news/sml_" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 80;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;


			}
			else { 
			echo "image needed";

		}
				else if (!$comment || strlen($comment = trim($comment)) > 10)
	{
		echo "".$_SESSION['username'].", you have added a news piece";
		mysql_query($query);


        }

}

Link to comment
https://forums.phpfreaks.com/topic/169028-if-ordering/
Share on other sites

Think I may have moved this on a little bit

 

	if (!empty($_POST))
{

	if(!$subject || strlen($subject = trim($subject)) == 0)
		echo "Subject not entered";
	else if(!$comment || strlen($comment = trim($comment)) == 0)
		echo "Comment not entered";
	else if(!$comment || strlen($comment = trim($comment)) < 10)
		echo "Comment too short, must be 10 characters at least";
	else if (isset ($_FILES['new_image']))
			{

				$imagename = $subject . '.jpg';
				$source = $_FILES['new_image']['tmp_name'];
				$target = "images/news/".$imagename;
				move_uploaded_file($source, $target);

				$imagepath = $imagename;
				$save = "images/news/" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 150;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;

				$save = "images/news/sml_" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 80;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;


			}

	else if (!$comment || strlen($comment = trim($comment)) > 10)
	{
		echo "".$_SESSION['username'].", you have added a news piece";
		mysql_query($query);	
        }

}

 

Still displays all the php image functions that arent working if the image isnt selected. How can I get around this?

Also is there a way to add an if for the jpeg, so u cant upload anything else or you get a warning?

Link to comment
https://forums.phpfreaks.com/topic/169028-if-ordering/#findComment-891801
Share on other sites

if (!empty($_POST))
{

	if(!$subject || strlen($subject = trim($subject)) == 0)
		echo "Subject not entered";
	else if(!$comment || strlen($comment = trim($comment)) == 0)
		echo "Comment not entered";
	else if(!$comment || strlen($comment = trim($comment)) < 10)
		echo "Comment too short, must be 10 characters at least";
	else if (isset ($_FILES['new_image']))
			{

				$imagename = $subject . '.jpg';
				$source = $_FILES['new_image']['tmp_name'];
				$target = "images/news/".$imagename;
				$file = explode('.', $source);
				$ext = end($file);
				move_uploaded_file($source, $target);

				$imagepath = $imagename;
				$save = "images/news/" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 150;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;

				$save = "images/news/sml_" . $imagepath; //This is the new file you saving
				$file = "images/news/" . $imagepath; //This is the original file

				list($width, $height) = getimagesize($file) ;

				$modwidth = 80;

				$diff = $width / $modwidth;

				$modheight = $height / $diff;
				$tn = imagecreatetruecolor($modwidth, $modheight) ;
				$image = imagecreatefromjpeg($file) ;
				imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

				imagejpeg($tn, $save, 100) ;


			}
			else if ($ext !== 'jpg' || $ext !== 'jpeg')
				echo 'Your file must be a jpeg';

	else if (!$comment || strlen($comment = trim($comment)) > 10)
	{
		echo "".$_SESSION['username'].", you have added a news piece";
		mysql_query($query);	
        }

}

 

Thats my latest attempt, but has the following problems.

If everything is correct, the process isnt a success, the you have added a news piece doesnt show, nothing at all shows.

If i dont add any image at all to the form before submitting, it just brings back tons of errors about the image functions that arent being used.

If I use a gif, it brings back a few of the image functions about the createjpeg etc. All errors.

And the checker I have put in for it being a jpeg doesnt work neither.

Any ideas? Really can't work this out, am trying though

Link to comment
https://forums.phpfreaks.com/topic/169028-if-ordering/#findComment-891827
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.