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>
Related
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!
How do I show PHP list in Intel XDK ListView?
<ul class="list widget uib_w_2 d-margins" data-uib="app_framework/listview" data-ver="1">
<li class="widget uib_w_8" data-uib="app_framework/listitem" data-ver="1">List Item</li>
</ul>
Here is my PHP:
<?php
for($i=0;$i<10;$i++)
{
?>
<li>List Item<?php echo $i;?></li>
<?php
}
?>
It's really unclear what you're trying to do, but, if you just want to make a simple unordered list in HTML with PHP, you're missing the <ul> and </ul> tags (use <ol> instead for a numbered list):
<ul>
<?php for ($i=0; $i<10; $i++): ?>
<li>List Item <?php echo $i;?></li>
<?php endfor; ?>
</ul>
Edit
It was misspelled in your post, but I think maybe you're looking for an "Intel XDK ListView"? If so, this guide seems to explain it pretty well, and here is a sample app using one. The ListView HTML you had pasted wasn't showing up because StackOverflow was interpreting it as HTML. If that code works and displays the single list item, just put the <ul class="..." ...> and </ul> tags outside of the for() block and repeat the inner <li> tag inside the loop adding in your <?php echo $i; ?>.
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 have recently decided to upgrade my web app to implement the new jQuery Mobile Panel widget.
After changing to 1.3.0, a <p>element on a page in my app is being forced to the bottom. When I reverted back to 1.2.0, the element stayed at the top of the listview like I want it to.
A snippet of my source:
<?php include ('header.php'); ?>
<?php print ' <p style="text-align: center;">Welcome, <span style="font-weight:bold; color: orange;">'.$_SESSION['username'].'</span>! Your user ID is <span style="font-weight:bold; color: orange;">'.$_SESSION['user_id'].'</span>.</p>'; ?>
<div data-role="content" id="assignment_list">
<!-- create 2 foreach loops, 1 for classes to be listed and organized by id in list dividers and another for listing individual assignments within each class -->
<ul data-role="listview">
<!--classes loop-->
<?php foreach ($userclasses as $class) : ?>
<li data-role="list-divider"><?php echo $class['class_name']; ?></li>
<?php $assignments=get_assignments_by_class($class['class_id']); ?>
<?php foreach ($assignments as $assignment) : ?><!--assignments loop-->
<?php print'<li>'.$assignment['assign_title'].'</li>';?>
<?php endforeach; ?> <!--/assignments loop-->
<?php endforeach; ?> <!--/classes loop-->
</ul>
</div><!--/data-role="content"-->
<?php include 'footer.php'; ?>
My goal is to display the user with a message that greets them and signifies they have logged in successfully. I want the message to appear above the listview that displays assignments. It worked fine, but When I changed to jQuery Mobile 1.3.0, the message is now displayed at the bottom of my page, after the listview.
I have tried putting the message inside of the content div but it appears cut off.
Any thoughts? Thanks!
why don't you print the <?php print ' <p style="text-align: center;">Welcome, <span style="font-weight:bold; color: orange;">'.$_SESSION['username'].'</span>! Your user ID is <span style="font-weight:bold; color: orange;">'.$_SESSION['user_id'].'</span>.</p>'; ?> line inside the content div before the listview?
I hope that will solve the issue.
Also I hope you are using jQuery 1.9.X?
I have created a multi option attribute so that I show an image for each option but i can not get it to work.
I have used this code from another post on here to get a list of the options to show.
The code I used is:
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<ul><li><?php
$_comma = ",";
$_list = "</li><li>";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</li></ul>
<?php endif; ?>
So this now shows a list of the options, one of on top of each other.
As I said I would like an image to be shown for each option.
I thought the best way would be to have divs and assign an image to each div.
I was hoping that I could get the output to be:
<div class="option1"></div>
<div class="option2"></div>
<div class="option3"></div>
<div class="option3"></div>
instead of the output that the code above has:
<ul>
<li>option1</li>
<li>option2</li>
<li>option3</li>
<li>option4</li>
</ul>
Change your code to
<?php if($_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)): ?>
<h4>Suitable for:</h4>
<div class="<?php
$_comma = ",";
$_list = "\"></div><div class=\"";
echo str_replace($_comma,$_list,$_product->getResource()->getAttribute('suitable_for')->getFrontend()->getValue($_product)) ?>
</div>
<?php endif; ?>