php processing string and echoing it out in a ul [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 7 years ago.
Improve this question
so i have got this string:
<?php
$elements = "first, second, third, fourth, fifth";
?>
And i'd like it to be echoed out like this:
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
<li>fourth</li>
<li>fifth</li>
</ul>

Try this, it may help you issue;
<?php
$elements = "first, second, third, fourth, fifth";
$numbers = explode(", ", $elements);
echo "<ul>";
foreach ($numbers as $number) {
echo "<li>{$number}</li>";
}
echo "</ul>";
?>

$elements = "first, second, third, fourth, fifth";
$arrayval=explode(',',$elements);
print_r($array);?>
<ul><?php
foreach($arrayval as $value){ ?>
<li><?php echo $value; ?></li>
<?php } ?>
</ul>

Related

Foreach Loop in PHP only Once [closed]

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 2 years ago.
Improve this question
I have a foreach loop but i want level only print once like example below.
https://prnt.sc/wgm877
But when i try foreach loop it print again and again.
https://prnt.sc/wgmazh
<?php foreach($rooms as $room) { ?>
<tr>
<td><?php echo $room->Level; ?></td>
<td><?php echo $room->Type; ?></td>
<td><?php echo $room->Dimension; ?></td>
</tr>
<?php } ?>
I would solve it like this. I.e. "remember" the last room level for each iteration. If it's changed, then print the new room level, otherwise print an empty string:
<?php
$lastlevel = "";
foreach($rooms as $room) {
if ($lastlevel != $room->Level) {
$level = $room->Level;
$lastlevel = $level;
} else {
$level = " ";
}
$nextrow = <<<EOD
<tr>
<td>{$level}</td>
<td>{$room->Type}</td>
<td>{$room->Dimension}</td>
</tr>
EOD;
echo $nextrow;
}
?>
You can either change the structure and do a foreach on the levels instead of the rooms, or use a new variable to know if the level has already been displayed
<? foreach ($actions as $action): ?>
<option value="<?= $action ?>"><?= $action ?>
<? endforeach; ?>
If you want to implement php on the frontend with html, I recommend doing it in the following way.

Echo out values in foreach loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this code:
if(!empty($item['criteria'])){
foreach ($item['criteria'] as $item2){
echo "$item2[description]<br/>".PHP_EOL;
}
}
}
Now i need to modify this code to swing open the $item2[description]. I tried it like that:
if(!empty($item['criteria'])){
foreach ($item['criteria'] as $item2){
echo ''Click
<div id="', $item2['id'], '" style="display: none">', $item2['description'], '</div>';
}
}
}
I think there is a mistake in some signs.
if(!empty($item['criteria'])){
foreach ($item['criteria'] as $item2){
echo 'Click';
echo '<div id="'. $item2['id'].'" style="display: none">'. $item2['description'].'</div>';
}
}
}
you have some syntactical mistakes check this once
Use the . to concat strings
if(!empty($item['criteria'])){
foreach ($item['criteria'] as $item2){
echo 'Click'.
'<div id="'.$item2['id'].'" style="display: none">'.$item2['description'].'</div>';
}
}

How do I display RSS feeds from other web sites [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am trying to display a blog: http://miletich2.blogspot.co.uk/ on my clients Wordpress site and as I have been looking around people have been recommending simple pie and their demo works great, but their Wordpress plugin hasn't been updated in 2 years and has loads of bugs.
Does any one know of another plugin that has the same functionality? Any help would be appreciated!
You can use fetch_feed function.
E.g. to show 5 posts with the provided blog:
<?php
// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('http://miletich2.blogspot.com/feeds/posts/default?alt=rss');
$maxitems = 0;
if (!is_wp_error($rss)) { // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5);
// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
}
?>
<ul>
<?php if ($maxitems == 0) : ?>
<li><?php _e('No posts found', 'text-domain'); ?></li>
<?php else : foreach ($rss_items as $item) : ?>
<li>
<a href="<?php echo esc_url($item->get_permalink()); ?>">
<?php echo esc_html($item->get_title()); ?>
</a>
<span>(<?php echo $item->get_date(get_option('date_format')); ?>)</span>
</li>
<?php endforeach; endif; ?>
</ul>

Clean HTML and PHP For Loop Not Displaying Anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
<?php for($x = 0; $x <= count($slides); $x++):?>
<li data-target="#main-carousel" data-slide-to="<?php echo($x);?>'" class="active"></li>
<?php endforeach; ?>
Not exactly sure what the error is everything seems right.
You start yourself with a for loop, while closing it with a foreach loop. These are two very different things and cannot be matched like that.
You simply need to replace
<?php endforeach; ?>
with
<?php endfor; ?>
Alternatively, you can use curlybrackets { instead, having something like
<?php for($x = 0; $x <= count($slides); $x++) { ?>
<!-- do HTML here -->
<?php } ?>

Else status in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I would need some help, since this code does not work (I have lack of knowledge, but I need to have it in that format)
$user_goals is array()
$user_count is just a number like 15
<?php foreach ($user_goals as $number): ?>
<?php if ($number < $user_count): ?>
<h5 class="text-center"><strike><?php echo htmlspecialchars($number); ?> users</strike></h5>
<?php else: ?>
<h5 class="text-center"><?php echo htmlspecialchars($number); ?> users</h5>
<?php endif; ?>
<?php endforeach; ?>
Any help will be apreciated :)
result that I want:
$user_count = 15;
$user_goals = array(
10,
15,
20,
etc,
);
so I want to get:
<strike>10</strike><br>
<strike>15</strike><br>
20
Solved it myself
<?php
foreach ($user_goals as $number) {
if ($number <= $users_count) {
echo "<h5 class='text-center'><strike>$number users</strike></h5>";
}else{
echo "<h5 class='text-center'>$number users</h5>";
}
}
?>
If you are looking to add a strike through when the goal is less than or equal to, you need to use that comparison operator.
if ($number <= $user_count)

Categories