The context
I have the following code:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
It renders:
<ul>
</ul>
When there are $users, everything works well. However, when there aren't, the <ul> is rendered with blank spaces in it, making it not attributable with the CSS selector :empty.
I managed to solve the problem by doing this:
<ul><?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?></ul>
It renders:
<ul></ul>
Without the like breaks in the code, the list is now empty (even empty of blank spaces), but I'm afraid to leave it that way and some other programmer fix the indentation and break it.
My question
Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
My question is: Is there a way to add line breaks to the code without adding blank spaces or breaks to the rendered html element?
You could use HTML comments
<ul><!--
<?php foreach($users as $user){ ?>
--><li><!-- …
This is a technique also used to fight the space between inline block elements, if you want to keep the HTML code readable.
Or you could of course use output buffering, and then replace those spaces before you return the output to the client (rather complex, if you want to do it right.)
And probably the most simple solution of all to your actual problem:
Why output the ul element at all, if there are not going to be any list items?
Surround the whole block with an if that checks how many elements the array contains – and just don’t output any of that stuff if there aren’t any.
Do a test before :
<?php if (isset($users) && count($users) > 0){ ?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php } ?>
With that way, <ul> won't be created if there's no users, so no empty spaces.
Don't tab your php statements:
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
PHP will remove everything starting at the <?php and ending at the ?> including the newline character. This is leaving the tab you have before your opening php tag.
You can break into a new line using:
echo "\n" //this will break into a new line
But pay atention that you must use double quotes. Single quotes won't work:
echo '\n' //this will output \n
The full solution will be
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } echo "\n" ?></ul>
I tested and it works. But if you are worried about future development and wan't to make things clear, I would use
if(isset($users)){?>
<ul>
<?php foreach($users as $user){ ?>
<li>
<?php echo $user->name ?>
</li>
<?php } ?>
</ul>
<?php }else{ //comment explaining that this prevents CSS selector ?>
<ul></ul>
<?php } ?>
I would go with this notation :
<ul><?php
foreach($users as $user){
?><li><?php echo $user->name ?></li><?php
}
?></ul>
It's still pretty readable, and HTML output contains no spaces that aren't needed, whether $users is empty or not!
Related
feel like i have tried every possible way..right now it is just skipping over the if statement and posting out the html.
<? if ($departmentID == 1 : ?>
<h4>Outreach Questions <?= $departmentID ?><h4>
<h4>Why Interested</h4>
<ul>
<strong>Why are you interested in volunteering as an outreach docent? </strong> <?php echo $whyInterested ?>
</ul>
<h4>Wildlife Issues</h4>
<ul>
<strong>What’s an environmental or wildlife issue you feel passionately about, and why? </strong> <?php echo $wildlifeIssue ?>
</ul>
<h4>Public Speaking</h4>
<ul>
<strong>Do you have prior experience speaking to the public? Please describe. </strong> <?php echo $publicSpeaking ?>
</ul>
<h4>Wildlife Groups</h4>
<ul>
<strong>Do you belong to any animal rights groups (PETA, The Humane Society, etc.)? If so, which ones? </strong> <?php echo $wildlifeGroup ?>
</ul>
<h4>Contributions</h4>
<ul>
<strong>What do you think you’d bring to the outreach volunteer team?</strong> <?php echo $bringToTeam ?>
</ul>
<? endif; ?>
</div>
the idea is to do an else if with id ==2 and so on..but it is pushing out all the HTML regardless of the departmentID.
thanks in advance.
<?php if ($departmentID == 1) {
echo " <h4>Outreach Questions = '$departmentID' </h4> #all html below
</ul>";} # ends block and closes echo
?>
</div>
Try this it uses php to echo out all of the html if the case is true otherwise no html gets echoed
Isn't the pretty first line wrong? You didn't close it:
Change the first line:
<?php if ($departmentID == 1) : ?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
currently I'm developing a application using Codeigniter framework and PHP. In there I need to include below mentioned PHP code in between HTML tags.
Can anyone provide a solution or an guidance for this?
PHP code :
if(isset($_SESSION['email']))
{
echo '<a href="http://localhost/ci/myads_view">';
echo 'MY ADS' ;
echo '</a>';
}
HTML code:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"/>
</nav>
You can come in and out of PHP at will:
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } ?>
In the above, if the PHP if statement evaluates to true, the HTML within the conditional will execute, otherwise it will not.
In your particular case, I can only assume this is what you're trying to do:
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<li>
<?php if(isset($_SESSION['email'])) { ?>
MY ADS
<?php } else { ?>
OTHER LINK
<?php } ?>
</li>
</ul>
</nav>
Or something along the lines of the above?
PHP is pretty cool, you can stop/resume it mid-document.
For example like this, I hope this is the solution to what you were trying to do:
<?php
if(isset($_SESSION['email']))
{ /* this will temporarily "stop" php --> */ ?>
<a href="http://localhost/ci/myads_view">
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
</a>
<?php /* <-- php resumes now */
}
?>
So what's going on?
First, you write your IF statement and the curly brackets, the code inside of which would usually be executed if the IF statement evaluates to TRUE.
<?php
if(something==true)
{
echo "write something to the page";
}
?>
But instead of using PHP commands, you quickly jump out of PHP by closing the PHP tag ?> as you would do at the end of your PHP code anyway.
<?php
if(something==true)
{
?>
Now you can continue in plain HTML without all the usual related issues of PHP commands as escaping characters like ' and " and \ watching your "qoute level" and so on. Instead, you write bog standard HTML.
When you're done entering the HTML that should be output in case the IF statement is true, just open another PHP tag and close the IF statement's curly brackets and, if that's the end of your PHP, close the PHP tag as well.
<?php
}
?>
What you have done is correct, additionally you can write as follows:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
echo $str;
Reference - http://php.net/manual/en/language.types.string.php
To put the link inside the navigation, you can do it like this, all inside the same PHP file.
<?php
if(isset($_SESSION['email'])) {
$navLink= 'MY ADS';
}
?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu"><?php echo($navLink); ?>
This should work:
<?php if(isset($_SESSION['email'])): ?>
<nav class="main-navigation dd-menu toggle-menu" role="navigation">
<ul class="sf-menu">
<?php endif; ?>
Here I am writing the code fully to explain my problem. the links are generated but the list is not.
what could be the problem? thanks..
require 'database.php';
$query="SELECT subject.subjectName, subject.subjectId FROM course,subject where subject.subjectId=course.subjectId
and course.memberId=1";
$courses=$db->query($query);
?>
<!doctype html>
<html>
<head>
<title>foreach</title>
</head>
<body>
<?php foreach ($courses as $course):?>
<a href="#">
<?php echo $course['subjectName'];?>
</a>
<br>
<?php endforeach; ?>
<ul>
<?php foreach ($courses as $course):?>
<li>
<?php echo $course['subjectName'];?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
Your second code has a quote inside before the closing PHP tag.
Before
<?php foreach($courses as $course):?> <a href"<?php echo $course['courseName'] "?> </li>
<?php endforeach;?>
After
<?php foreach($courses as $course):?> ???
<?php endforeach;?>
And for easier to read code
<?php
foreach($courses as $course) {
echo 'A Link';
}
?>
No, foreach does not modify the object. You are probably overriding $courses variable between two foreach blocks
Just not sure if its mistake while type code here or you have exactly same code in your script but pls check this in second for each
Here you are missing "=" after href should be like this
is missing
Did you check page source? It might be hidden by css as you have different HTML tags in each block
Hi everyone I finally got the answer for my problem and here it is:
In my code $courses is a PDOstatement Object and foreach loop calls upon fetch() method and fetch() method frees the result set, that means at end of foreach loop there are no more rows left. its all freed, so we can not use the same $courses again in loop since there is nothing left in it...
hope I am not missing anything ...
I've created a simple query to create a list of dates and descriptions from my calendar database.
The idea is that I have a PHP webpage that show a text list that I can quickly copy and paste into an email or text message.
My problem is that, although the text shows up correctly on the web page, when I paste the info into a text editor (Word, email, whatever) I'm getting tabs between each column.
How can I format the text in PHP so that it pastes correctly?
This is my code:-
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li>
<?php echo htmlentities($row_AvDates['Month']);?>
<?php echo "-";?>
<?php echo htmlentities ($row_AvDates['the_days']);?>
</li>
<?php
}
?>
</ul>
<?php
}
?>
This gives me output that looks correct, but pastes like this...
(Month[tab]"-"[tab] Dates)
How do I loose the tabs?
You're closing php and letting the HTML output be rendered. See tab chars below:
[tab]<li><?php echo htmlentities($row_AvDates['Month']);?>
[tab]<?php echo "-";?>
[tab]<?php echo htmlentities ($row_AvDates['the_days']);?></li>
Solution:
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities ($row_AvDates['the_days']); ?>
Darren.
Try to use this code, it should solve your problem:
<?php if (mysql_num_rows($AvDates) > 0) : ?>
<ul>
<?php while ($row_AvDates = mysql_fetch_assoc($AvDates)) : ?>
<li><?php echo htmlentities($row_AvDates['Month'])."-".htmlentities ($row_AvDates['the_days']);?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Hope it helps.
you can have the whole output in one single like this
<?php echo htmlentities($row_AvDates['Month']) . '-' . htmlentities($row_AvDates['the_days']);?>
instead of separate php tags
If you want to separate code across multiple lines, not all code in one line:
if(mysql_num_rows($AvDates) > 0){
?>
<ul>
<?php
while ($row_AvDates = mysql_fetch_assoc($AvDates)){
?>
<li><?php
echo htmlentities($row_AvDates['Month']);
echo "-";
echo htmlentities($row_AvDates['the_days']);
?></li>
<?php
}
?>
</ul>
<?php
}
?>
My goal is to create a dynamic list of links next to each other (not on top of each other). So far I have the links outputting correctly using Simplepie and an Atom datasource but for some reason my output has a linebreak between each new item in the for each statement. Is it possible to skip the line break and put the output items next to each other instead? I am not getting any errors, just not getting my goal output. Please help this is making me age at an exponential rate :)
<body>
<?php
foreach ($feed->get_items() as $item):
?>
<div class="item"><?php echo $item- >get_title(); ?></div><?php endforeach; ?>
</body>
Put the links in a <ul> (Unordered list element) with style of float:left like so
<body>
<ul>
<?php
foreach ($feed->get_items() as $item):?>
<li class="item"><?php echo $item- >get_title(); ?>
</li>
<?php endforeach; ?>
</ul>
</body>