Guest Posted October 19, 2009 Share Posted October 19, 2009 Hi guys, I would like to know if there is a way to echo multiple lines of html codes. I intend to echo a form if a variable is set and nothing if it is not set. And right now, I echo each line of html code, which works well but is a lot to do for such a thing. I am pretty sure there should be a better way to do it but I can't find how... Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2009 Share Posted October 19, 2009 It depends on if there are any php variables or other php statements in the code. If it is just a block of HTML, without any php in it, it is usually easier to put a closing ?> php tag, put the HTML, then put an opening <?php tag. You can also put multiple lines in a single echo statement, there is no need to put each line into a single echo statement. Which method you use depends on what you are doing. Without seeing what you are actually doing, it is not possible to suggest the best method for the specific situation. Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939695 Share on other sites More sharing options...
Guest Posted October 19, 2009 Share Posted October 19, 2009 Sorry, I should have put the code I am using now. Here it is: <?php if (!isset($_GET['actor'])) { } else { echo "<form action=\"edit_actor.php?actor=" . urlencode($sel_actor['id']) . " method=\"post\">"; echo "<p>First name:<br />"; echo "<input type=\"text\" name=\"first_name\" value=\"" . $sel_actor['first_name'] . "\" id=\"first_name\"></input></p>"; echo " "; echo "<p>Last name:<br />"; echo "<input type=\"text\" name=\"last_name\" value=\"" . $sel_actor['last_name'] . "\" id=\"last_name\"></input></p>"; echo "</p"; echo "<p>Visible:"; echo "<input type=\"radio\" name=\"visible\" value=\"0\""; if ($sel_actor['visible'] == 0) { echo " checked"; } echo "/> No <input type=\"radio\" name=\"visible\" value=\"1\""; if ($sel_actor['visible'] == 1) { echo " checked"; } echo "/> Yes"; echo "</p>"; echo "<p>Content:<br />"; echo "<textarea id=\"content\" name=\"content\" rows=\"5\" cols=\"60\">" . $sel_actor['accents_dialects'] . "</textarea>"; echo "</p>"; echo "<input type=\"submit\" name=\"submit\" value=\"Edit Actor's page\" />"; echo " "; echo "<a href=\"delete_actor.php?actor=" . urlencode($sel_actor['id']) . "¤tpage=" . $currentpage . " onclick=\"return confirm('Are you sure you want to DELETE this ACTOR's page?');\">Delete Actor's page</a>"; echo "</form>"; echo "<br />"; echo "<a href=\"content_actor.php?currentpage=" . $currentpage . "\">Cancel</a>"; } ?> There are variables in the form, that's why I was using echo... Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939702 Share on other sites More sharing options...
Kaboom Posted October 19, 2009 Share Posted October 19, 2009 Could this work: <?php if (!isset($_GET['actor'])) { } else { ?> <html><body><form action=\"edit_actor.php?actor=" . urlencode($sel_actor['id']) . " method=\"post\"> <p>First name:<br /> <input type=\"text\" name=\"first_name\" value=\"" . $sel_actor['first_name'] . "\" id=\"first_name\"></input></p> <p>Last name:<br /> <input type=\"text\" name=\"last_name\" value=\"" . $sel_actor['last_name'] . "\" id=\"last_name\"></input></p> </p> <p>Visible: <input type=\"radio\" name=\"visible\" value=\"0\" <?php if ($sel_actor['visible'] == 0) { echo " checked"; ?> /> No <input type=\"radio\" name=\"visible\" value=\"1\" <?php if ($sel_actor['visible'] == 1) { echo " checked"; ?> /> Yes </p> <p>Content:<br /> <textarea id=\"content\" name=\"content\" rows=\"5\" cols=\"60\"><php? " . $sel_actor['accents_dialects'] . " ?></textarea>"; </p> <input type=\"submit\" name=\"submit\" value=\"Edit Actor's page\" /> <a href=\"delete_actor.php?actor=<?php " . urlencode($sel_actor['id']) . "¤tpage=" . $currentpage . " ?> onclick=\"return confirm('Are you sure you want to DELETE this ACTOR's page?');\">Delete Actor's page</a>"; </form> <br />"; <a href=\"content_actor.php?currentpage=" . $currentpage . "\">Cancel</a> <?php } ?> Nopt sure but wouldn't something like that work>? Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939706 Share on other sites More sharing options...
Guest Posted October 19, 2009 Share Posted October 19, 2009 Nope, it does not work Kaboom, the form does not show up, even though the variable is set... Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939723 Share on other sites More sharing options...
Kaboom Posted October 19, 2009 Share Posted October 19, 2009 Nope, it does not work Kaboom, the form does not show up, even though the variable is set... leave the }else{ ?> part then do your html code using <?php PHP CODE HERE ?> inside the html then at the bottom put <?php } ?> to close the whole thing and it should work. I didn't do that on all your code Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939738 Share on other sites More sharing options...
PFMaBiSmAd Posted October 19, 2009 Share Posted October 19, 2009 Because you have php functions, variables, and logic, I would use printf - <?php if (!isset($_GET['actor'])) { } else { $format = '<form action="edit_actor.php?actor=%s" method="post"> <p>First name:<br /> <input type="text" name="first_name" value="%s" id="first_name"></input></p> <p>Last name:<br /> <input type="text" name="last_name" value="%s" id="last_name"></input></p> </p> <p>Visible: <input type="radio" name="visible" value="0" %s /> No <input type="radio" name="visible" value="1" %s /> Yes </p> <p>Content:<br /> <textarea id="content" name="content" rows="5" cols="60">%s</textarea> </p> <input type="submit" name="submit" value="Edit Actor\'s page" /> <a href="delete_actor.php?actor=%s¤tpage=%s" onclick="return confirm(\'Are you sure you want to DELETE this ACTOR\'s page?\');">Delete Actor\'s page</a> </form> <br /> <a href="content_actor.php?currentpage=%s">Cancel</a>'; printf($format, urlencode($sel_actor['id']), htmlentities($sel_actor['first_name']), htmlentities($sel_actor['last_name']), ($sel_actor['visible'] == 0) ? " checked='checked'" : "", ($sel_actor['visible'] == 1) ? " checked='checked'" : "", htmlentities($sel_actor['accents_dialects']), urlencode($sel_actor['id']), $currentpage, $currentpage); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939743 Share on other sites More sharing options...
Guest Posted October 19, 2009 Share Posted October 19, 2009 It seems to work PFM, thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/178229-solved-echo-multiple-lines-of-html-code/#findComment-939813 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.