Here I have my print function:
<?php
// little helper function to print the results
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
echo $t['token'] . "/" . $t['tag'];
echo '</span>';
echo " ";
}
}
$tagger = new PosTagger('lexicon.txt');
?>
And here is what I'm outputting from an HTML form:
<?php
if($_POST['submitbutton'] == "Submit") {
//Check whether the form has been submitted
$tags = $tagger->tag($_POST['texttotag']);
printTag($tags);
}
?>
My problem is, the output in the browser results in strange line breaks in the middle of some of my <span> like so:
<span class="VB">Enter/VB</span> <span class="PRP$">your/PRP$</span> <span class="NN
">text/NN
</span> <span class="TO">to/TO</span> <span class="NN">tag/NN</span> <span class="RB
">here/RB
</span>
This means my CSS definitions don't apply to the "interrupted" spans. Any idea why this is happening and how I can stop it? I've had a good look around and haven't been able to find cause/solution. Thanks.
It seems that your $t['tag'] variable includes a line-break.
You can get rid of that using trim($t['tag']) instead.
It appears that the $t['tag'] in printTag function contains line break character. You can use str_replace function to remove these characters like:
function printTag($tags) {
foreach($tags as $t) {
echo '<span class="' . $t['tag'] . '">';
$ttag = str_replace('\n','',$t['tag']);
$ttag = str_replace('\r','',$ttag);
echo $t['token'] . "/" . $ttag;
echo '</span>';
echo " ";
}
}
Related
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
i have the following code:
<?php
$events = Event::with('skoler')->get();
$skoleIsSet = $_GET['skoleId'];
$eventIsSet = $_GET['event'];
if($skoleIsSet && !$eventIsSet) {
foreach($events as $event) {
if($event['skole_id'] == $skoleIsSet) {
***$img = $event['img_url'];***
echo '<div id="events">';
***echo '<img src="$img" height="100" width="100"/>';***
echo '<h1>' . $event['title'] . '</h1>';
echo '<p>Beskrivelse: ' . $event['description'] . '</p>';
echo '<p>Pris: ' . $event['pris'] . '</p>';
echo '<p>Dato: ' . $event['date'] . '</p></div>';
}
}
}
else {
foreach ($events as $event) {
if ($event['skole_id'] == $skoleIsSet) {
$bg = $event['img_url'];
'<div id="events">';
echo '<h1>' . $event['title'] . '</h1>';
echo '<p>Beskrivelse: ' . $event['description'] . '</p>';
echo '<p>Pris: ' . $event['pris'] . '</p>';
echo '<p>Dato: ' . $event['date'] . '</p></div>';
}
}
?>
I am supposed to show all events, with a picture. The url for the pictures is in the img_url column in my db. What is wrong with line number 8 and 10? The strong font option is not working, but the lines im referring to is marked with 6x *'s.
Thanks!
echo '<img src="$img" height="100" width="100"/>';
You cannot include a variable directly into a string, if you're using single quotes. Either use double quotes (and escape the double quotes inside the string), or interrupt your string:
echo '<img src="' . $img . '" height="100" width="100"/>';
echo "<img src=\"$img\" height=\"100\" width=\"100\"/>";
Part of the code is
while ($row = mysqli_fetch_assoc($result))
{
echo "$row[firstname] $row[lastname]";
echo "<hr>";
echo "<strong>Date Referred:</strong> $row[cf_831]";
echo "<br/>";
echo "<strong>Date Today:</strong> $today";
echo "<br/>";
}
How do I style this part for example?
"$row[firstname] $row[lastname]"
You can try
echo '<p class="mystyle">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
Then add CSS
.mystyle {
}
Addition to Sidsec9's answer: if you are afraid of stylesheets, you can also consider:
echo '<p style="color: red; font-size: 10pt;">' . $row['firstname'] . " " . $row['lastname'] . '</p>';
You could also use styles to get rid of , for example using margin-right or padding-right properties.
echo '<p id="name"> '. $row[firstname] $row[lastname] .'</p>';
Css you only using for name alone use id else use class and use the single css for all values.
#name
{
color: red;
}
You can wrap it in a class, and fix it with a stylesheet. I also changed the echo, you're allowed to close the PHP tag, do some HTML and then open PHP again.
<?=$variable?> is short echo, it is the same as <?php echo $variable; ?>.
while ($row = mysqli_fetch_assoc($result)){
?>
<span class="Example"><?=$row[firstname].' '.$row[lastname]?></span>
<hr>
<strong>Date Referred:</strong> <?=$row[cf_831]?>
<br/>
<strong>Date Today:</strong> <?=$today?>
<br/>
<?php
}
A better method would be making and html file, say example.com, and place <!-- FIRSTNAME -->,<!-- LASTNAME --> AND <!-- TODAY --> instead of the variables. Then, using php:
$totalResult = "";
$template_one = file_get_contents("example.html");
while ($row = mysqli_fetch_assoc($result)){
$item = $template_one;
$item = str_replace("<!-- FIRSTNAME -->", $row['firstname'], $item);
$item = str_replace("<!-- LASTNAME -->", $row['lastname'], $item);
$totalResult.= $item; // add to totel result
)
// $today doesnt change in the loop, do it once after the loop is done:
$totalResult = str_replace("<!-- TODAY -->", $today, $totalResult);
echo $totalResult; // Now you have the result in a pretty variable
What I have is a reply script, and I need the page to only use the username echo'd in for a variable, like if Meap sends me a PM and I want to reply to it, when I hit the reply link I need it to use his name, and not another name from the page. Currently it is using the first name on the page instead of the one that the reply link is for
Here's the code;
while ($row = mysqli_fetch_array($data)) {
session_start();
$reply = $row['from'];
echo '<div class="viewpost">';
echo '<div class="vpside">';
if(!empty($row['picture'])) {
echo '<img class="pictest" src="' . MM_UPLOADPATH . $row['picture'] . '" alt="' . MM_UPLOADPATH . 'nopic.png' . '" />';
}
if(!empty($row['from'])) {
echo '<p>From:<br />' . $row['from'] . '</p>';
$_SESSION['reply'] = $row['from'];
echo 'Reply';
}
if(!empty($row['rank'])) {
echo '<p>Rank:<br />' . $row['rank'] . '</p>';
}
if(!empty($row['gender'])){
echo '<p>Gender:<br /> ' . $row['gender'] . '</p>';
}
echo '</div>';
if(!empty($row['title'])) {
echo'<h4><u>' .$row['title']. '</u></h4>';
}
if(!empty($row['msg'])) {
echo '<p class="">' . $row['msg'] . '</p>';
}
echo '<div class="sig">';
if(!empty($row['bio'])) {
echo '<p>' . $row['bio'] . '</p>';
}
echo '</div>';
echo '</div><br />';
}
Here's a picture of what it looks like when you get a Private Message;
Evidently you have to be specific down to the last detail on here, the question - or rather the problem - is that it is not using the correct recipient for the reply. Say I click reply under Meaps name, it will use Lilpunish instead. Basically it will use the first one loaded, and I need it to use the correct username. How would I do this.?
Here's your problem:
$_SESSION['reply'] = $row['from'];
echo 'Reply';
Consider what is happening when this code is run multiple times. The first time, it sets the session variable reply to Meap. The next time, when the second message is printed, it sets it to another value.
What you actually want is to pass who to reply as a part of the link, e.g.
echo 'Reply';
And then not retrieve the reply-to from $_SESSION. Session variables are not really supposed to be used like this.
Please forgive the extreme-beginner style of coding. I'm working with PHP and JSON strings from an API for the first time.
What I'm trying to do here, which obviously doesn't work if you look through it, is print out multiple movie results that the user is searching for. This is a search results page - with a movie poster and some key details next to each item.
One of the things I want next to each result is a list of the first 5 actors listed in the movie, as "Starring" and then any directors listed in the movie as "Director".
The problem is no doubt the fact that I've nested foreach statements. But I don't know any other way of doing this.
Please please the simplest answer would be the best for me. I don't mind if it's bad practice, just whatever solves it quickest would be perfect!
Here's the code:
// Check if there were any results
if ($films_result == '["Nothing found."]' || $films_result == null) {
}
else {
// Loop through each film returned
foreach ($films as $film) {
// Set default poster image to use if film doesn't have one
$backdrop_url = 'images/placeholder-film.gif';
// Loop through each poster for current film
foreach($film->backdrops as $backdrop) {
if ($backdrop->image->size == 'thumb') {
$backdrop_url = $backdrop->image->url;
}
}
echo '<div class="view-films-film">
<img src="' . $backdrop_url . '" alt="' . strtolower($film->name) . '" />
<div class="view-films-film-snippet">
<h2>' . strtolower($film->name) . '</h2>
<img src="images/bbfc-' . strtolower($film->certification) . '.png" alt="" />
<h3>Starring</h3>
<p>';
$num_actors = 0;
foreach ($films[0]->cast as $cast) {
if ($cast->job == 'Actor') {
echo '' . $cast->name . ' ';
$num_actors++;
if ($num_actors == 5)
break;
}
}
echo ' </p>
<h3>Director</h3>
<p>';
foreach ($films[0]->cast as $cast) {
if ($cast->job == 'Director') {
echo '' . $cast->name . ' ';
}
}
echo ' </p>
</div>
</div>';
// End films
}
}
An example of the result would be this:
with line 120 being foreach ($films[0]->cast as $cast) { and line 131 being foreach ($films[0]->cast as $cast) {
Why are you using $films[0] when you're in a foreach ($films as $film)? You should be using $film->cast right? And you should dump $film at the start of your loop with var_dump and inspect it - make sure $cast is an array and is populated. If it's not, work back from there.
I'm trying to insert the following code below into the other piece of code where it says INSERT PHP CODE HERE.. but I don't now how to correctly do it?
The code I want to insert.
if (!empty($f)) {
echo $f;
} else if(!empty($u)) {
echo $u;
} else {
echo 'someting';
}
Here is the other part of the code.
if(!empty($avatar)){
echo '<li>' . $avatar . '<div>INSERT PHP CODE HERE..</div></li>';
}
$href='';
if (!empty($f)) {
$href=$f;
} else if(!empty($u)) {
$href=$u;
} else {
$href='someting';
}
if(!empty($avatar)){
echo '<li>' . $avatar . '<div>'.$href.'</div></li>';
}
You don't need to one-line everything. Break it up into separate statements.
if(!empty($avatar)){
echo '<li>' . $avatar . '<div><a href="#">';
INSERT PHP CODE HERE..
echo '</a></div></li>';
}
Just add it in like you would a variable:
echo '<li>' . $avatar . '<div>'. phpfunction() .'</div></li>';
I realized after writing this that you can just use the ternary operator:
echo '<li>' . $avatar . '<div>'. (!empty($f)?$f:(!$empty($u)?$u:'something')) .'</div></li>';
It's significantly harder to read though, so I don't think I'd recommend it.