PHP concatenation issues - php

So I am really bad at concatenation Ive been working on it for an hour the best i can get this to do is no errors butit doesnt echo the username for some reason I am pretty sure it has to do with concatenation anyway.
Code:
echo '<span style="color: #"' . $row['color'] . ';"';
echo $row['user'] . ': ' . '</span>' . $row['message'] . '';
echo '</br>';
Basically I am trying to make the username show the color of the the hex in database. But when I do this it doesnt even show the username just the message.

echo '<span style="color: #' . $row['color'] . ';">'
. $row['user'] . ': </span>' . $row['message']
. '</br>';

You didn't close the <span> tag.
You can also just output the entire string and interpolate the values:
echo "<span style=\"color:#{$row['color']}\">{$row['user']}:</span> {$row['message']}<br>";

Related

Why is my picture not showing? [duplicate]

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\"/>";

How would I add another column and populate it?

I know this is way out of left field, but i was wanting to see if anyone could help.
I am wanting to add a column, populating the data from a certain variable (the variable i have yet to discover the name of). This is a plugin for wordpress and the developer wont really help.
Last ditch effort i guess. Here is the code:
}$output.='><tr><th>' . __('Request For', 'wpsc-support-tickets') . '</th><th>'
. __('Status', 'wpsc-support-tickets') . '</th><th>'
. __('Last Reply', 'wpsc-support-tickets')
. '</th><th>' . __('Department', 'wpsc-support-tickets') . '</th></tr>';
I added the last column, "Department".
Now, looking at the following code, i cant figure out how to populate that data (assuming i knew the variable even)
$output .= '<tr><td>
<a href="" onclick="loadTicket(' . $result['primkey'] . ',\'' . $canReopen . '\');
return false;" ';
if ($result['resolution'] == strtolower('open') ) {
$resresolution = __('Open', 'wpsc-support-tickets');
} elseif ($result['resolution'] == strtolower('closed') ) {
$resresolution = __('Closed', 'wpsc-support-tickets');
} else {
$resresolution = $result['resolution'];
}
if ($devOptions['disable_inline_styles'] == 'false') {
$output.='style="border:none;text-decoration:none;"';
}$output.='><img';
if ($devOptions['disable_inline_styles'] == 'false') {
$output.=' style="float:left;border:none;margin-right:5px;"';
}$output.=' src="' . plugins_url('/images/page_edit.png', __FILE__) . '"
alt="' . __('View', 'wpsc-support-tickets') . '" /> ' . base64_decode($result['title']) .
'</a></td><td>' . $resresolution . '</td><td>'
. date_i18n( get_option( 'date_format' ),
$result['last_updated']) . ' ' . __('by', 'wpsc-support-tickets') . '
' . $last_staff_reply . '</td>
</tr>';
again - where might i add the variable for the column "department" if the variable were $department_var?
You can add it to the very end of that large chunk of mess before the closing </tr>...
change
...$last_staff_reply . '</td></tr>';
to
...$last_staff_reply . '</td><td>' . $department_var . '</td></tr>';

PHP Private Message Reply Function

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.

Inside of a ternary php script how do i concatenate a second variable into my string?

Before style="style="color: ' . $rx_event_colors . '" I want to add the $ima variable. it's just a variable with an image inside of the string. Additionally, if you think i would benefit from just adding the
if(!empty($ins_event))
{
echo "<tr><td> <a href='". matry::here(array('event_id'=>$ins_event['id'])) . "'" .
( $ins_event['status'] == 2 ? ' style="color: ' . $rx_event_colors . '">Completed Insurance Event' : '>Active Insurance Event') . "</a></td></tr>";
}
I've tried:
? ' $ima, style="style="color: ' . $rx_event_colors . '"
style="style="color: ' . $ima, $rx_event_colors . '"
style="style="color: ' . ($ima), $rx_event_colors . '"
style="style="color: ' . ($ima), ($rx_event_colors) . '"
with no avail.
I'm assuming $ima is a string. To concatenate strings, use the "."
So in the first part of your ternary, if you'd like to add $ima, simply do this:
' style="color: ' . $ima . $rx_event_colors . '">Completed Insurance Event'
However, I'm confused when you say "[$ima] just a variable with an image inside of the string". Does $ima contain the path to the image itself? If so, it belongs in the SRC attribute of the img tag, not inside the STYLE attribute.
UPDATE:
Because $ima contained the entire img tag, it doesn't belong in the style atrribute. See:
' style="color: ' . $rx_event_colors . '">' . $ima . 'Completed Insurance Event'

Echo in one line in PHP

I want to change the original code I have,
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (";
echo metersToFeetInches($productarea) . " ft²)" . "</p>";
into a single echo line as shown here:
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . metersToFeetInches($productarea) . " ft²)" . "</p>";
But I'm getting some strange line breaks in this second case for metersToFeetInches($productarea).
Generated HTML:
24,757
<p>
<strong>Area:</strong>
2,300 m² ( ft²)
</p>
Output:
24,757
Area:
2,300 m² ( ft²)
How can I solve it? Is there any documentation I could read to learn how to do it by myself in the future?
I'm pretty sure I know what's going on here, your function metersToFeetInches is echoing a value rather than returning it.
function metersToFeetInches() {
echo 'OUTPUT';
}
echo 'FIRST '.metersToFeetInches().' LAST';
// Outputs: OUTPUTFIRST LAST
echo metersToFeetInches() is actually redundant.
This is because the function runs before the string you built is actually output. Note that both examples you posted would have this problem. Change your function to return a value instead. Afterwards, any places where you have used it like so:
echo 'Something';
metersToFeetInches();
echo 'Something Else';
You'll have to use an echo:
echo 'Something';
echo metersToFeetInches();
echo 'Something Else';
Functions should pretty much always return a value. Lesson learned, perhaps?
If you are really in a bind and cannot change the function, you'll have to resort to output buffering:
ob_start();
metersToFeetInches($productarea);
$metersToFeetInches = ob_get_clean();
echo "<p><strong>" . __('Area:', 'honegumi') . "</strong> " . number_format($productarea) . " m² (" . $metersToFeetInches . " ft²)" . "</p>";
...which is rather silly to have to do.

Categories