unexpected T_VARIABLE, expecting ; in echo PHP - php

I'm keeping get this error
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in
on the following line...
echo '<li><div>'$caption'<a href='.($portfolio_url).'><img height='$src[2]' width='$src[1]' src='$src[0]' alt='$alt' /></a></div></li>';
I'm newbie in PHP and wrote this line with tutorial. Any help would be appreciated.

Concatenate properly:
echo '<li><div>' . $caption . '<a href='.($portfolio_url).'><img height=' . $src[2] . ' width=' . $src[1] . ' src=' . $src[0] . ' alt='$alt' /></a></div></li>';
^ ^
That is, always use the syntax:
echo 'text ' . $variable . ' more text';
So that you get (printed in multiple lines to make it more clear):
echo '<li><div>' . $caption . '<a hr ...
.... ef=' . $portfolio_url . '><img
.... height=' . $src[2] . ' width=' . $src[1] . ' src=' . $src[0] . '
.... alt=' . $alt. ' /></a></div></li>';

Related

Listing number of rows by group

I have a database with members joining and I am trying to display the number of members from each country.
Here is the code I'm using to show the country flags:
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $countrynum . ' fans<br>';
}
?>
I have really looked for the answer, and keep seeing the same type of code, but I haven't been able to get it to work for me.
I've tried this:
$countrynum = SELECT COUNT(name) FROM $table WHERE country = $sortvalue;
which I inserted after the foreach statement. This (and every other iteration of this I've tried gives me Parse error: syntax error, unexpected 'COUNT' (T_STRING) in...
The closest I've come to solving this is by using this code (though, I shouldn't have to reconnect to the db should i?):
<?
foreach( $sorters as $sortvalue )
if( $sortvalue == '' )
echo '<li> <a href="' . $list_url . $connector . $sortfield .
'=none">None given</a> </li>';
else
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT country, COUNT(*) FROM $table GROUP BY country') as $row) {
echo '<a href="' . $list_url . $connector . $sortfield .
'=' . $sortvalue . '"><img src="' . $flags . str_replace(' ', '_', $sortvalue) . '.gif" title="' . $sortvalue . '" alt="' . $sortvalue . '"></a> ' . $row['COUNT(*)'] . ' fans<br>';
}
?>
This gave me the correct row (member) count, but the same single flag was displayed beside each number instead of the corresponding flag with number of members.
I'm sure this is probably super simple for lots of you, but I'm a newbie who's really trying to learn coding and so far that mostly means cutting, pasting and LOTS of trial and error.
Any assistance (and learning resource suggestions) would be very much appreciated. :)
Since the flag image source is depending on$sorters and $sortvalue, it will be same for all country. Look at the loops. For each $sorters as $sortvalue, the link will be multiple countries but the img source will be same for all.

PHP concatenation issues

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

how to keep php output lines in one line

I just made a phone number to time zone converter and it displays result as:
Requested Phone No.: +1 732 78782722
Country: United States
Expected Region: Newark, New Brunswick
Timezone: America/New_York
Date: 2015-08-05
Time: 01:51:03 am
What I want to do is place all these outputs in a single line.
Here's my output code
if(!empty($record['country_name'])) {
$this->display('<strong>Country:</strong> ' . $record['country_name']);
}
if(!empty($record['city'])) {
$this->display('<strong>Expected Region:</strong> ' . $record['city']);
}
//echo json_encode($date);
if(!empty($record['zone_name'])) {
$this->display('<strong>Timezone:</strong> ' . $record['zone_name']);
$this->display('<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>');
$this->display('<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
}
Thanks for the help.
Try this: If you want to pass that in a variable
if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
$var = '<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] . '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>';
}
echo $var;
Or this if you want to pass in your object:
if(!((empty($record['country_name']) && empty($record['city']) && empty($record['zone_name'])) {
$this->display('<strong>Country:</strong> ' . $record['country_name'] . '<strong>Expected Region:</strong> ' . $record['city'] . '<strong>Timezone:</strong> ' . $record['zone_name']. '<h2><strong>Date:</strong> ' . date('Y-m-d') . '</h2>' . '<h2><strong>Time:</strong> ' . date('H:i:s a') . '</h2>');
}
return $this;
You need to create a string variable and concatenate all your output to that string like below:-
$result = ''; // create an empty string
if(!empty($record['country_name']) && !empty($record['city']) && !empty($record['zone_name'])){
$result = '<strong>Country:</strong> ' . $record['country_name'].' <strong>Timezone:</strong> '. $record['city'].' <strong>Timezone:</strong> '.$record['zone_name'].' <h2><strong>Date:</strong> '. date('Y-m-d') . '</h2>'.' <h2><strong>Time:</strong> '. date('H:i:s a') . '</h2>';
}
echo $result; // print output

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>';

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'

Categories