/    Sign up×
Community /Pin to ProfileBookmark

Need to add HTML to PHP

I am very much a PHP novice; I am occasionally able to modify PHP if I have really good directions on what to look for and what changes to make — but beyond that, I am anything but fluent in PHP.

However, in a website I’m working on, using Drupal modules, I have a PHP function that is outputting search results as a table. I want to add some CSS classes to some of the lines of output … but every time I try to put HTML into the PHP coding, I end up getting error messages, so I could use some help with this.

Here’s the code:

[code=php]
function theme_ulisting_search_result($record){

$thumbnails = ulistingapi_get_thumbnails($record->nid);
$destination = drupal_get_destination();
if (count($thumbnails) > 0){
$href = file_create_url($thumbnails[0]);
$image = l(“<img class=”list-view” src=”$href”>”,’node/’.$record->nid,
array(‘html’=>true,’query’=>$destination));
}else{
$image = “”;
};
if (trim($record->address) != ”){
$address = $record->address . ‘, ‘ . $record->city_name;
}else{
$address = $record->city_name;
}
$address = l($address, ‘node/’.$record->nid,array(‘query’=> $destination));
// icons
if ($record->bedroom > 0){
$icons .= ‘<div class=”ulisting-icon-bed”>’.$record->bedroom.'</div>’;
}
if($record->bathroom_full > 0){
$icons .= ‘<div class=”ulisting-icon-bathroom”>’.$record->bathroom_full.'</div>’;
}
$image .= ‘<div style=”clear:both”></div>’.$icons;
$currency = ($record->currency == ”)? variable_get(‘ulisting_currency’,’$’): $record->currency;

$node->vid = $record->nid;
$vid = variable_get(‘ulisting_property_type’, ”);
$terms = taxonomy_node_get_terms_by_vocabulary($node,$vid);
list($index,$property_type) = each($terms);
$vid = variable_get(‘ulisting_regions’, ”);
$terms = taxonomy_node_get_terms_by_vocabulary($node,$vid);
list($index,$region) = each($terms);
if ($region){
$region = $region->name;
};
$row = array(
array(‘data’ => $image),
array(‘data’ =>
$address.'<br />’.
$region.'<br />’.
$record->title,
‘valign’=>’top’,
),
array(‘data’ => $property_type->name,’valign’=>’top’),
array(‘data’ => $currency.number_format($record->price),
‘valign’=>’top’,
)
);

return $row;
}[/code]

I would like to take out the line break tags at the end of the [B]address[/B] and [B]region[/B] lines; I would like to enclose the [B]address[/B], [B]region[/B], and [B]record->title[/B] lines in paragraph tags, and to add a class for each of those paragraph tags so I can style each of those items differently.

Any help or suggestions would be greatly appreciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@monarch_684Feb 26.2010 — Change all your " (double quotes) to ' (single quotes) in the HTML portions. For instance this:
[CODE]$icons .= '<div class="ulisting-icon-bathroom">'.$record->bathroom_full.'</div>'; [/CODE]
should be this:
[CODE]$icons .= '<div class='ulisting-icon-bathroom'>'.$record->bathroom_full.'</div>'; [/CODE]
Copy linkTweet thisAlerts:
@skywalker2208Feb 26.2010 — What is the error message that you are getting?

The poster above is wrong. Their suggest will cause a syntax error.
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 26.2010 — Warning to the OP -- modifying Drupal module code isn't novice level because Drupal has its own way of doing things and really requires a solid understanding of their SDK. With that said, try this untested code, no promises, but you get the idea:

Find:

[code=php] array('data' =>
$address.'<br />'.
$region.'<br />'.
$record->title,
[/code]


Change to:

[code=php] array('data' =>
'<div class="address_class">'.$address.'</div>'.
'<div class="region_class">'.$region.'</div>'.
'<div class="title_class">'.$record->title.'</div>',
[/code]


Note: I used DIV and not P because, depending on your theme, Drupal might override the behavior of the P tag in other CSS. So it's best to use div's instead so you have less problems. If you could care less about what I just said simply change each div to p, simple as that. I made up arbitrary class names for you, adjust to suit your style sheet or needs, obviously.

On a side note, monarch_684's code will not work. In PHP if you wrap a string in single quotes, you must escape any embedded single quotes by preceeding those with (slash), otherwise you can embed double quotes as I did, but NOT variables since single quoted strings are static text.

-jim
×

Success!

Help @News_design spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...