PHP foreach - show line breaks in source code - php

I'm currently using the following PHP foreach code below. When I view the source code, it's a giant block of list items.
My question is, how can I edit this so each list item in the source code is on it's own line for easier debugging?
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">';
echo '<strong>', $event->title, '</strong>';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>';
echo $event->location;
echo '</a></li>';
}

Like this:
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">';
echo '<strong>', $event->title, '</strong>';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>';
echo $event->location;
echo '</a></li>' . "\n";
}
Just add . "\n" to the last line in your loop.

Use new line '\n' or PHP_EOL at end of each line

Try modifying the final line to include a line break:
echo '</a></li>' . PHP_EOL;

Append all of your strings with either "\n" (make sure you use double-quotes), or PHP_EOL.
echo '</a></li>' . "\n";
// or
echo '</a></li>' . PHP_EOL;

Use \n.
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">' . "\n";
echo '<strong>', $event->title, '</strong>' . "\n";
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>' . "\n";
echo $event->location . "\n";
echo '</a></li> . "\n"';
}

I would write the code like this:
<?php foreach($xml->Event as $event) { ?>
<li>
<a href="<?php echo $event->link; ?>">
<strong><?php echo $event->title; ?></strong>
<span><?php echo $event->beginDate; ?> at <?php echo $event->beginTime; ?></span>
<?php echo $event->location; ?>
</a>
</li>
<?php } ?>

Add a '\n' at the end of each line you want to break.
foreach($xml->Event as $event) {
echo '<li><a href="', $event->link, '">\n';
echo '<strong>', $event->title, '</strong>\n';
echo '<span>', $event->beginDate, ' at ', $event->beginTime, '</span>\n';
echo $event->location;
echo '</a></li>' . "\n";
}

Related

How can I put a link around my excerpts in PHP?

I'm trying to put a link around my excerpts like on my titles but i'm getting a parse-error on this line:
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
Here the whole code of my post-element:
<div class="post-header">
<h3 class="title">
<?php the_title(); ?>
</h3>
<span class="meta-author"><?php the_author_posts_link(); ?> </span>
<span class="meta-category"> | <?php the_category(', '); ?> </span>
<span class="meta-comment-count"> | <a href="<?php comments_link(); ?>">
<?php comments_number( esc_html__( 'No Comments','salient'), esc_html__( 'One Comment','salient'), '% '. esc_html__( 'Comments','salient') ); ?></a>
</span>
</div>
<?php
$excerpt_length = ( !empty( $nectar_options['blog_excerpt_length'] ) ) ?
intval( $nectar_options['blog_excerpt_length'] ) : 30;
echo '<div class="excerpt">''' . nectar_excerpt($excerpt_length) . '</div>';?>
<div class="meta-tags"> <?php the_tags(''); ?> </div>
<div class="tags-divider"></div>
In PHP, text strings are merged using the dot character. So if you want to connect them together, you should do this:
$a = "text1";
$b = "text2";
echo ($a . $b); // prints "text1text2"
Or in your case like this:
echo "text1" . function() . "text3"; // prints text1text2text3
And if you are using function like string, you don't use the ";" character at the end, because it will end the whole line of code.
echo "text1" . "text2"; . "text3"; // wrong
echo "text1" . "text2" . "text3"; // correct
echo "text1" . function(); . "text3"; // wrong
echo "text1" . function() . "text3"; // correct
So, just add the dots and remove the semicolon, and it should work.
echo '<div class="excerpt">' . '' . nectar_excerpt($excerpt_length) . '</div>';?>
Give this a try?
echo '<div class="excerpt">' . nectar_excerpt($excerpt_length) . '</div>';

How to insert HTML using foreach() without echo

I have this working code in my view:
<?php
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo '<div class="task">';
echo '<span class="task_id">'.($task_num+1).'.'.'</span>';
echo '<div class="task_time">';
echo '<span class="task_time_start">'.$task->getStartTime().'</span>';
echo '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
echo '</div>';
echo ''.$task->name.'';
echo 'Start';
echo 'Finish';
echo '<div class="status_round '.$task->status.'"></div>';
echo '</div>';
$task_num++;
}
?>
Is there any way to get rid of 'echo'?
P.S. and is the way to insert HTML with HTML helper more correct even if it takes more space?
You can drop echo completely and use native HTML syntax:
<?php $task_num = 0 ?>
<?php foreach ($curent_day->getTasksList() as $task): ?>
<div class='task'>
<span class='task_id'><?= ++$task_num ?></span>
<div class='task_time'>
<span class='task_time_start'><?= $task->getStartTime() ?></span>
<span class='task_time_finish'><?= $task->getFinishTime() ?></span>
</div>
<a href='/' class='task_name'><?= $task->name ?></a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round <?= $task->status ?>'></div>
</div>
<?php endforeach ?>
This will give you better syntax highlighting and autoformatting support in your IDE/editor.
There are several different ways that you can concatenate the string by using single quotes and double quotes, or by using a variable and the ".=" operator to append text onto the end of a string. Just google php strings & concatenation and it will have more than you need to figure this out.
But using you example here is a method:
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
echo
'<div class="task">' .
'<span class="task_id">' . ($task_num+1) . '.'.'</span>' .
'<div class="task_time">' .
'<span class="task_time_start">' . $task->getStartTime() . '</span>' .
'<span class="task_time_finish">' . $task->getFinishTime() . '</span>' .
'</div>' .
''.$task->name.'' .
'Start' .
'Finish' .
'<div class="status_round '.$task->status.'"></div>' .
'</div>';
$task_num++;
}
You have to echo to output you data to the browser.
You need not use string concatenation or multiple echo statements.
Alternative
$task_num = 0;
foreach ($curent_day->getTasksList() as $task){
$task_num++;
echo
"<div class='task'>
<span class='task_id'>{$task_num}</span>
<div class='task_time'>
<span class='task_time_start'>{$task->getStartTime()}</span>
<span class='task_time_finish'>{$task->getFinishTime()}</span>
</div>
<a href='/' class='task_name'>{$task->name}</a>
<a href='/' class='btn task_start btn_disabled'>Start</a>
<a href='/' class='btn task_finish btn_disabled'>Finish</a>
<div class='status_round {$task->status}'></div>
</div>";
}
You can concatenate your html in one variable and then you can use it with one time echo statement
<?php
$task_num = 0;
$html = '';
foreach ($curent_day->getTasksList() as $task){
$html .= '<div class="task">';
$html .= '<span class="task_id">'.($task_num+1).'.'.'</span>';
$html .= '<div class="task_time">';
$html .= '<span class="task_time_start">'.$task->getStartTime().'</span>';
$html .= '<span class="task_time_finish">'.$task->getFinishTime().'</span>';
$html .= '</div>';
$html .= ''.$task->name.'';
$html .= 'Start';
$html .= 'Finish';
$html .= '<div class="status_round '.$task->status.'"></div>';
$html .= '</div>';
$task_num++;
}?>

while looping w3-quarter in w3-row-padding

Here is the problem that while looping the php in while loop in w3-row-padding of w3 responsive layout . The layout breaks
Here is the source code
<?php
$r=0;
while($r<ceil($fetch_row_count/4))
{ ?>
<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">
<?php
while($row=mysqli_fetch_array($res))
{
?>
<div class="w3-quarter">
<img src="admin/uploads/<?php echo $row['image']; ?>" alt="noodles" style="width:50%">
<h3><?php echo $row['title']; ?></h3>
<p><?php echo $row['description']; ?> </p>
</div>
<?php
}
$r++;
}
?>
</div>
Thanks for reply and comments in advance
That bottom div was not being added for each of your padded containers.
The way the code is written you are adding a padded container and then adding your w3-quarter div for each of your result sets and then repeating that a bunch of times with what looks like to me the same set of data in each one.
What you are probably trying to accomplish is just making one padded div and then echo out your result set with the w3-quarter divs inside of it.
Here is your original way with the bottom div corrected:
<?php
$r=0;
while($r<ceil($fetch_row_count/4)) {
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
}
$r++;
echo
'</div>';
}
?>
Here is the way I think you are trying to do it: (Just guessing)
<?php
echo
'<div class="w3-row-padding w3-padding-16 w3-center" style="clear:both" id="food">';
$r = 0;
while($row=mysqli_fetch_array($res)){
echo
'<div class="w3-quarter">' .
'<img src="admin/uploads/' . $row['image'] . '" alt="noodles" style="width:50%">' .
'<h3>' . $row['title'] . '</h3>' .
'<p>' . $row['description'] . '</p>' .
'</div>';
$r++;
//I would not actually try to control how many results you add like this.
//I would get the results from a better formatted query.
if($r < ceil($fetch_row_count/4)){
break;
}
}
echo
'</div>';
?>

qTranslate inside PHP

There are a few things that won't translate on my website that are inside PHP. Since it's inside PHP, the normal qTranslate quick tag won't work. Is there a simple code that could help me translate these words? Should it go on the same page where the translations are located?
This is the code for the what I need translated inside the PHP (Location:, Venue:, etc):
<div class="event-text">
<h2 class="event-title">' . get_the_title($post->ID) . '</h2>
<ul class="event-meta">';
if ($event_location != null) {
echo '
<li><span>Location:</span>' . $event_location . '</li>';
}
if ($club != null) {
echo '
<li><span>Venue:</span>' . $club . '</li>';
}
if ($event_allday == 'yes'){
echo '<li><span>Length:</span>All Day</li>';
} elseif ($tstart) {
echo '<li><span>Length:</span>' . $tstart . '';
} if ($tend) {
echo ' – ' . $tend . '</li>';
}
echo '
<li>';
if (get_post_meta($post->ID, 'event_out', true) == 'yes') {
echo '<div class="event-cancel-out"><p>Sold Out</p></div><!-- end .event-cancel-out -->';
} elseif (get_post_meta($post->ID, 'event_cancel', true) == 'yes') {
echo '<div class="event-cancel-out"><p>Canceled</p></div><!-- end .event-cancel-out -->';
}elseif (get_post_meta($post->ID, 'event_free', true) == 'yes') {
echo '<div class="event-cancel-out"><p>Free Entry</p></div><!-- end #event-cancel-out -->';
} else {
echo '<div class="event-tickets"><a href="' . $event_ticket . '" >Buy Tickets</a></div><!-- end #event-tickets -->';
}
echo '</li>
</ul><!-- end ul.event-meta -->';
echo '
' . the_content() . '
</div><!-- end .event-text -->';
Yes, it is a theme file for Wordpress. I fixed these by changing to the following codes:
echo __("<li><span><!--:en-->Location: <!--:--><!--:ja-->場所:<!--:--></span>") . $event_location . '</li>';
echo '<div class="event-cancel-out"><p>' . __('<!--:en-->Sold Out<!--:--><!--:ja-->売り切れ<!--:-->') . '</p></div><!-- end .event-cancel-out -->';

Accessing URL within Twitter XML Tag

I'am currently parsing in a twitter tweets by geocode and displaying them using php. But at the moment unable to access the URL to the tweet and have instead opted to show the users twitter profile.
The URL I'm trying to access is within the link tag
<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961" rel="alternate" >
Its location within the XML file is Feed->Entry->Link.
I have thought about using a regular expression but don't know how to go about using this.
Here is my code.
<h3><?php
if (isset($column1Heading)) {
echo $column1Heading;
}
?></h3>
<p>
<strong>Tweets within 10 miles radius of London Eye</strong></br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=51.5069999695%2C-0.142489999533%2C10.0mi%22london%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</br>
<strong>London Hashtag</strong>
</br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23london&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col2">
<h3><?php
if (isset($column2Heading)) {
echo $column2Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?geocode=40.744544%2C-74.027593%2C10.0mi%22manhattan%2C+ny%22&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
<strong>NYC Hashtag</strong>
</br>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?q=%23nyc&lang=en&rpp=5');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
<li class="col3">
<h3><?php
if (isset($column3Heading)) {
echo $column3Heading;
}
?></h3>
<p>
<?php
$feed = simplexml_load_file ('http://search.twitter.com/search.atom?lang=en&geocode=48.861%2C2.336%2C5.0mi%22paris%2C+fr%22&rpp=5&lang=en');
if ($feed){foreach ($feed->entry as $item) {
echo '<a href=\'' . $item->author->uri. '\'>' . $item->title . '</a>', '</br>' . $item->published, '</br>';
}
}
else echo "Cannot find Twitter feed!"
?>
</p>
</li>
</ul>
<div class="clear"></div>
</div>
<?php require_once 'footer.php'; ?>
If I understand you correctly:
You can access the attribute with the built in attributes() method of SimpleXML.
$item->link->attributes()->href
Otherwise the regular expression should look like this
$string = '<link type="text/html" href="http://twitter.com/pmhigham/statuses/166271863331368961" rel="alternate" >';
preg_match('/href\=\"([^\"]+)\"/i', $string, $matches);

Categories