flexybash Posted December 1, 2010 Share Posted December 1, 2010 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> "; Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/ Share on other sites More sharing options...
Rifts Posted December 1, 2010 Share Posted December 1, 2010 what part isnt working Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141920 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141921 Share on other sites More sharing options...
flexybash Posted December 1, 2010 Author Share Posted December 1, 2010 Thanks for the quick replies @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 Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141923 Share on other sites More sharing options...
Rifts Posted December 1, 2010 Share Posted December 1, 2010 you need to replace if ($_POST['delete']) { echo "DELETED"; } with if(isset($_POST['delete'])) { echo "DELETED"; } Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141927 Share on other sites More sharing options...
kenrbnsn Posted December 1, 2010 Share Posted December 1, 2010 What's not working? What's it supposed to do? We may need to see more of your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141928 Share on other sites More sharing options...
JakeTheSnake3.0 Posted December 1, 2010 Share Posted December 1, 2010 Even still, there's something called the ternary operator. An example would be: $judgement = true; echo 'This is ' . ($judgement ? 'Heaven' : 'Hell') . '.'; Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141931 Share on other sites More sharing options...
flexybash Posted December 1, 2010 Author Share Posted December 1, 2010 What's not working? What's it supposed to do? We may need to see more of your code. Ken Ill explain again 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> "; Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141934 Share on other sites More sharing options...
Rifts Posted December 1, 2010 Share Posted December 1, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141938 Share on other sites More sharing options...
flexybash Posted December 1, 2010 Author Share Posted December 1, 2010 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 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" } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141941 Share on other sites More sharing options...
Rifts Posted December 1, 2010 Share Posted December 1, 2010 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" } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141945 Share on other sites More sharing options...
laffin Posted December 1, 2010 Share Posted December 1, 2010 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} Quote Link to comment https://forums.phpfreaks.com/topic/220382-if-statement-inside-an-echo/#findComment-1141949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.