PHP line break between content and date - php

I am trying to have the title and date break to two lines but they are currently running in one line.
PHP novice here needs help!
<span class="kb-topic__list-article-title"><?php echo esc_html(get_the_title()); echo nl2br(the_date());?> </span></a>

All you need to do is echo a line break and not use the nl2br() function:
<span class="kb-topic__list-article-title">
<?php
echo esc_html(get_the_title());
echo '<br>';
echo (the_date());
?>
</span>

As long as you're jumping in and out of PHP and HTML, I would separate the dynamic content from the HTML layout. You can accomplish your goal in this fashion with something like the following:
<span class="kb-topic__list-article-title">
<div>
<?php echo esc_html(get_the_title()); ?>
</div>
<div>
<?php echo (the_date()); ?>
</div>
</span>
You can then style the HTML elements however you need to in order to achieve the exact layout you're going for. However the above code will get you the basic effect of putting the output of your two functions on separate lines.

You can also:
<span class="kb-topic__list-article-title">
<?php echo esc_html(get_the_title());?>
<br>
<?php echo (the_date());?>
</span>
have the brake not in php

Related

div markup not on displayed page

In the php file I have the following code
<div class="entry-meta">
<?php echo theme_post_meta(); ?>
</div>
The function theme_post_meta calculates and returns html that has two spans.
<span class="posted-on">Posted 22 hours ago</span> /
<span class="posted-in">News, Important</span>
However when the page is loaded in the browser the outer div is no longer there.
Try to do php cancatenation as:
<?php echo '<div class="entry-meta">'.theme_post_meta().'</div>'; ?>

Which is the better way to insert HTML tags into PHP code?

I see two methods but I don't know which is better, should I insert a div tag like this :
echo "<div>";
or like this ?
?> <div> <?php
There's no right answer to this.
When displaying a lot of HTML, I normally use ?> <div> <?php. This is especially true for when looping over data from a database, I would use:
<?php while ($row == $query->fetchObject() ) : ?>
<div class="row-<?php echo $row->id ?>">
//Some more of the data
</div>
<? endwhile; ?>
However, displaying only a little, I normally just echo it.
<?php echo "Hello, my name is <strong>Joe Doe<strong>"; ?>
I would encourage you to play with it and find what you like best.
The text editor i am using has the color formatting, when you are echoing things, it only all comes out the same color, so if you are outside of PHP you get the benefit of the color co-ordination.

PHP form validation - error log

Anything wrong with this code, it works great, but I don't understand the forth line. Why is closing bracket all by itself? I am a fairly new to PHP and always Google for answers, but I cant figure this one out. Hopefully, I can help others someday. Thanks
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Nothing is wrong with it. You can break in and out of PHP, and that is what this code is doing. Sometimes it's easier to break out of a PHP block to write some HTML, then go back into PHP
It's ending the if statement created on line 2, but line 3 is outputting HTML so php is ended, only to begin on the next line to finish the open statement.
Write it like this you suddenly know:
<div class="errorbox">
<?php
if(isset($error2)) {
echo '<strong class="error">' . $error2 . '</strong>';
}
?>
</div>
Or like so:
<div class="errorbox">
<?php
if(isset($error2)) {
?>
<strong class="error"><?php echo $error2;?></strong>
<?php
}
?>
</div>
This is normal PHP templating. It is ouputting HTML. First bracket is opening, second bracket is closing
There are several ways of doing it:
The best way is as descibed in the question:
<div class="errorbox">
<?php if(isset($error2)){?>
<strong class="error"><?php echo $error2;?></strong>
<?php } ?>
</div>
Another way is by echoing:
echo "<div class="errorbox">";
<?php
if(isset($error2)){
echo "<strong class="error">". $error ."</strong>";
}
?>
echo "</div>";

Php spacing within the code

I have this code:
<?php wpfp_link();the_views(); ?>
On running, it shows:
Add to favorites. Views:6
I want it to show like below, with spaces.
Add to favorites. Views: 6
Any help would be appreciated.
<?php wpfp_link(): ?> blah blah blah <?php the_views(); ?>
You need to add some HTML and CSS,
<span><?php wpfp_link(); ?></span> <span style="float:right"><?php the_views(); ?></span>
Demo: http://jsfiddle.net/E4y7y/1/
Solution 2
<span style="display:inline-block;width:60%"><?php wpfp_link(); ?></span> <span style="display:inline-block;width:35%;" ><?php the_views(); ?></span>
Demo: http://jsfiddle.net/E4y7y/
Instead of "Add to favorites. Views: 6" you want "Add to favorites". Then you would have to take the_views(); out and since wpfp_link(); puts Add to favorites on the page you would have to get in wpfp_link(); and add before "Add" and after "Add". it would make Add portion of the text blue.
If you want to give it a space after wpfp_link(); basically wrap it with a span and give it a margin/padding right, that is the best way of doing that.
Like this;
<?php
echo "<span style='color:blue;padding-right:10px'>";
wpfp_link();
echo "</span>";
the_views();
?>
http://jsfiddle.net/ygkFc/
Its purely a CSS thing.It depends upon the DOM where are you trying to show the out put..
You can put them in separate DOM and apply margin or float and display as like you want..
<div style = "float:left"><?php wpfp_link();?></div>
<div style = "float:right"><?php the_views(); ?></div>

How can I use an associative array inside an <<<_END html tag?

I'm building my own little blogging platform as a practice/fun/exercise in PHP and MySQL. I'm currently using the following code to output the proper formatting (which works perfectly):
$rows=mysql_num_rows($postsresult);
for ($j=0 ; $j < $rows ; ++$j){
$row=mysql_fetch_row($postsresult);
echo <<<_END
<div class="titlebox"> $row[3] </div>
<div class="maincontent"> $row[2]
<div class="postclosercontainer">
<div class="postcloser">Sincerely, <br />
Samuel'<span>Explosion Festival</span>' Shenaniganfest </div>
</div></div>
_END;
}
However, I found that the while($info=mysql_fetch_array($postsresult){ would be easier to code for, as the data is stored by name instead of by array number (which, with any more than a few fields, becomes aggravating to remember).
I tried to update the code with the prior while loop, but found that when I went to pull the data from the array by name, it no longer functioned properly within the <<<_END tags.
For example: <div class="titlebox"> $data['title'] generates an error.
Is there any way to accomplish this within the <<<_END tags, or should I just use the print function for each line? On a another note, is this even proper coding technique? (I'm only an amateur.)
Better is to directly write HTML. This makes it easier to maintain your HTML and you might be able to use features from your IDE such as syntax highlighting or code completion.
Example:
<?php
// your other code
?>
<?php while(($info=mysql_fetch_array($postsresult))): ?>
<div class="titlebox"><?php echo $info['title']; ?> </div>
<div class="maincontent">
<?php echo $info['content']; ?>
<div class="postclosercontainer">
<div class="postcloser">Sincerely, <br />
Samuel'<span>Explosion Festival</span>' Shenaniganfest
</div>
</div>
</div>
<?php endwhile; ?>
I'm using the alternative syntax for control structures. It increases readability when dealing with HTML, especially if you have nested control structures (brackets are much more difficult to spot when embedded in HTML).

Categories