Jump to content

If statement inside an echo?


flexybash

Recommended Posts

Hello guys im new to php i been coding for like a week now, i  need some help here i have been stuck for like 6 hours XD,is it possible to write an IF Statement inside an echo?

 

//this is what i want to do 
// echo a table and a delete button
// and if you click on the delete button it echoes out "DELETED"


echo "

<table width='528px'>
<tr>
<td>
</td>
<td>
<center><font size='5'>$tittle</font></center><br>
</td>
</tr>
<tr>
<td>
</td>
<td>
$message
</td>
</tr>
<tr>
<td>
</td>
<td>
<font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font>
<input id='delete' name='delete' type='submit' value='Delete' >

if ($_POST['delete']) {  echo "DELETED";     } 


<td>
</td>
</tr><br><br>
</table>

";


Link to comment
https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/
Share on other sites

Don't use one big echo, break it up:

<?php

echo "
<table width='528px'>
<tr>
<td>
</td>
<td>
<center><font size='5'>$tittle</font></center><br>
</td>
</tr>
<tr>
<td>
</td>
<td>
$message
</td>
</tr>
<tr>
<td>
</td>
<td>
<font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font>
<input id='delete' name='delete' type='submit' value='Delete' >";
if ($_POST['delete']) {  echo "DELETED";     }
echo "<td>
</td>
</tr><br><br>
</table>

";
?>

 

Ken

Thanks for the quick replies :D

 

 

@Ken i tried breaking the echo´s up like you just did, but it still is not working :S

 

@Rifts , i think the line of code giving a problem here its

if ($_POST['delete']) {  echo "DELETED";     } 

that is why  i am asking if its possible to write an if inside an echo :)

 

 

EDIT:

 

 

i just tho  its not working because this line is inside the echo

(1)

<input id='delete' name='delete' type='submit' value='Delete' >

 

and this like its outside the echo

(2)

if ($_POST['delete']) {  echo "DELETED";     } 

 

 

and since they are related(line 2 asks line 1) maybe thats why they dont work? (just an idea) XD

 

 

 

 

What's not working? What's it supposed to do? We may need to see more of your code.

 

Ken

 

Ill explain again :D

 

 

 

 

Okey i echoed out a table  and at the end of the table i echoed a delete button

and when i click on that delete button, its supposed to echo "DELETED"

 

this is how i have the code at the moment with modifications made

 

echo "

<table width='528px'>
<tr>
<td>
</td>
<td>
<center><font size='5'>$tittle</font></center><br>
</td>
</tr>
<tr>
<td>
</td>
<td>
$message
</td>
</tr>
<tr>
<td>
</td>
<td>
<font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font>
<input id='delete' name='delete' type='submit' value='Delete' > ";

if(isset($_POST['delete'])) {echo("DELETED";} 

echo "
<td>
</td>
</tr><br><br>
</table>

";

try this

 

i cleaned up your table also

<?php
your php stuff here
?>



<table width='528px'>
<tr>
	<td></td>
	<td><center><font size='5'><?php=$tittle;?></font></center><br></td>
</tr>
<tr>
	<td></td>
	<td><?php=$message;?></td>
</tr>
<tr>
	<td></td>
	<td>
		<font size='1'>Posted By:<font color='green'><?php=$author;?></font> on <font color='gray'><?php=$date;?></font> at <font color='gray'><?php=$time;?></font></font>
		<input id='delete' name='delete' type='submit' value='Delete' >

							<?php
								if(isset($_POST['delete']))
									{
										echo "DELETED";
									}
							?>
	</td>

	<td></td>
</tr>
<br><br>
</table>

 

thanks rifts but i cant do that in some cases because i have to have php tags, ill just pass you the entire code , i hope u have time to give it a fast check :D thanks!

 

 

<?php 

$tittle=$_POST['tittle'];
$author=$_POST['author'];
$message=$_POST['message'];

if ($_POST['submitnews']){



$currentdate= date("y-m-d");
$currenttime=date("H:i:s",strtotime("-6 hours"));

$post=mysql_query("INSERT INTO news VALUES('','$tittle','$author','$message','$currentdate','$currenttime')");
echo"Posted!";
}




$select=mysql_query("SELECT * FROM news ORDER BY id DESC");

while ($row= mysql_fetch_assoc($select))

{
$id=$row['id'];
$tittle=$row['tittle'];
$author=$row['author'];
$message=$row['message'];
$date=$row['date'];
$time=$row['time'];



if ($_SESSION['admin'])

{
echo "

<table width='528px'>
<tr>
<td>
</td>
<td>
<center><font size='5'>$tittle</font></center><br>
</td>
</tr>
<tr>
<td>
</td>
<td>
$message
</td>
</tr>
<tr>
<td>
</td>
<td>
<font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font>
<input id='delete' name='delete' type='submit' value='Delete' >
<td>
</td>
</tr><br><br>
</table>
";

//this is what i want to do 
// echo a table and a delete button
// and if you click on the delete button it echoes out "DELETED"


}




}

?>

sorry i can not test it but this should work

<?php 

$tittle=$_POST['tittle'];
$author=$_POST['author'];
$message=$_POST['message'];

if ($_POST['submitnews']){



$currentdate= date("y-m-d");
$currenttime=date("H:i:s",strtotime("-6 hours"));

$post=mysql_query("INSERT INTO news VALUES('','$tittle','$author','$message','$currentdate','$currenttime')");
echo"Posted!";
}




$select=mysql_query("SELECT * FROM news ORDER BY id DESC");

while ($row= mysql_fetch_assoc($select))

{
$id=$row['id'];
$tittle=$row['tittle'];
$author=$row['author'];
$message=$row['message'];
$date=$row['date'];
$time=$row['time'];



if ($_SESSION['admin'])

{
echo "

<table width='528px'>
<tr>
<td>
</td>
<td>
<center><font size='5'>$tittle</font></center><br>
</td>
</tr>
<tr>
<td>
</td>
<td>
$message
</td>
</tr>
<tr>
<td>
</td>
<td>
<font size='1'>Posted By:<font color='green'>$author</font> on <font color='gray'>$date</font> at <font color='gray'>$time</font></font>
<form method='post' action=\"<?php echo $_SERVER['PHP_SELF']; ?>\">
<input id='delete' name='del' type='submit' value='Delete' >
</form>
<td>
</td>
</tr><br><br>
</table>
";
if(isset($_POST['del']))
{
	echo "DELETED";
}
//this is what i want to do 
// echo a table and a delete button
// and if you click on the delete button it echoes out "DELETED"


}




}

?>

 

within echo or string concatenation use the trenary operator instead.

 

which works like a if...else statement

 

so

echo "You have {$amt} item";
if($amt>1)
   echo 's';

 

can be written with a trenary operator as

echo "You have {$amt} item". ($amt>1?'s':'');

 

if lookes like this: {expression} ? {true result} : {false result}

 

 

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.