I have the following in PHP, using Wordpress + Buddypress addon:
$blogusers = get_users('role=s2member_level1&number=5');
foreach ($blogusers as $user) {
$username = str_replace('"', '',$user->user_nicename);
$fullname = str_replace('"', '',bp_profile_field_data( array( 'user_id'=>$user->ID,'field'=>'name' ) ));
$category = str_replace('"', '',bp_profile_field_data( array( 'user_id'=>$user->ID, 'field'=>'category' ) ));
echo '<div style="float:left; width:100px; height:100px;">';
echo '<h3>' . $fullname . '</h3>';
echo '<strong>Camp Type: </strong>' . $category . '<br />';
echo '</div>';
}
The $fullname and $category vars get bumped to before the float:left element, but the a $username variable stays in place, which is how all the variables are supposed to be rendered.
ie:
<div class="page" id="blog-latest" role="main">
<div style="width:500px;height:100px;position:relative;">
Frank GoreType 4<div style="float:left; width:100px; height:100px;"><h3></h3><strong>Camp Type: </strong><br /></div>
</div>
Any clue is to why those two variables are being tossed out of the echo statement?
It has to do with how php's echo construct deals with concatenations. It makes absolutely no sense to me, but apparently, when a concatenation occurs, it opens up a temp variable to perform the combine and then outputs the result.
If you're going to use that syntax, replace the periods with commas in the echo statements. Echo treats comma separated values as separate statements and echos them in the order they were received.
Plus, it's a teeny bit faster = but only on the order of 1 second per millions of echo statements. :)
My preferred method is to create an html string and then echo once per logical block.
$echo = '<div style="float:left; width:100px; height:100px;">'
. '<h3>' . $fullname . '</h3>'
. '<strong>Camp Type: </strong>' . $category . '<br />'
. '</div>';
echo $echo;
It turns out that it has something to do with bp_profile_field_data. It is a function used by the core to retrieve data. The more developer-friendly method, get_bp_profile_field_data, was much better to work with. I'm getting proper formatting of all my strings. I appreciate you sharing the knowledge, however. Thanks!
Related
I am using the repeater field of Wordpress' advanced custom fields to make the following content dynamic on my site.
Image of how the content looks while marked up statically
This is how I have marked up the content using plain html.
<div class="container">
<h2>A few general rules of thumb</h2>
<p><span>1.</span>Hallways and transitional spaces are best painted...</p>
<p><span>2.</span>It is best to keep expensive furnishings...</p>
<p><span>3.</span>If you want furnishings and accessories...</p>
<p><span>4.</span>Neutral furnishings and accessories?</p>
</div>
The paragraphs have been styled by setting their display to flex so that the span (number) is on the left and the paragraph is on the right.
This is how my code now looks with PHP.
<div class="container">
<h2><?php echo $container3title['title'];?></h2>
<?php
// Check rows existexists.
if(have_rows('rules')):
// Loop through rows.
while(have_rows('rules')) : the_row();
// Load sub field value.
$ruleNumber = get_sub_field('ruleNumber');
$ruleDetails = get_sub_field('ruleDetails');
// Do something...
echo '<span>' . $ruleNumber . '</span>';
echo '<p>' . $ruleDetails . '</p>';
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
?>
</div>
The problem is that I do not know how to echo the php so that the paragraphs aren't displayed as two block elements that are stacked on top of one another. I want it to be marked up the same way as I have marked it up using plain html so that my CSS will style it accordingly.
I tried the following but it didn't work.
echo '<p>' '<span>' . $ruleNumber . '</span>'; . $ruleDetails . '</p>';
Can someone please tell me how to put this together correctly? Thank you
Just store the span in a variable and concatenate it in:
$span = '<span>' . esc_html($ruleNumber) . '</span>';
echo '<p>' . $span . esc_html($ruleDetails) . '</p>';
I threw some escapes in there, too. It is generally recommended to always escape user-generated input when outputting, just in case. And even though you might “know” that the number is always numeric, it doesn’t really hurt to escape it anyway, and it makes it easier to scan for unescaped values.
I have a string as follows and store it in the variable:
$div = '<div class="posts"> . $row['author'] .'<?php if('.$perm.'>1):?><br> '.$role.'</br> Edit<?php endif ?></div>
What I want is to use this variable in another page so I need to insert the if statement to regulate permission so the content only displays with those users with above 1. It comes up with no syntax errors but when I run it this happens
John1):?>
Moderator
Edit
so the 1):?> is being displayed when it shouldn't.
Any reasons why? or how I could do this better?
Thanks.
Can you give an try like this:
$div ='<div class="posts">'.$row['author'].''; ?> <?php if($perm>1) {'<br> '.$role.'</br> Edit';} ''
I think its badly formated with single quotes ' , please check and lets see if it is.
Try conditional operator like this. The div section is kept at both statements just to improve readability.
$div = if('.$perm.'>1) ? ('<div class="posts"> . $row['author'] . <br> '.$role.'</br> Edit<div>') : ('<div class="posts"> . $row['author'] .'<div>');
Or:
$div = '<div class="posts"> . $row['author'] .';
$div += if('.$perm.'>1) ? '<br> '.$role.'</br> Edit<div>' : '<div>';
I feel a bit stupid having to ask this, but for the life of me it won't work and I know I must be missing something small. I have the following PHP code for a gallery of model's photos. I have 2 pages. guests1.php and guests2.php. Guests1 shows the thumbnails and lists all the models. guests2 will show a particular model's individual portfolio. I am trying to pass the model name in the url, as I need it for the title on the second page and also for the directory name, so that the page knows where to find the pictures.
Simple enough, I thought, just add it into the url as a variable. No problem... however no matter how I write it, it will not put it in the url. The name of the model is always missing... however if I echo the variable it does it no problem?! The pages are working wonderfully apart from this one little thing and it's driving me bonkers. Any help most appreciated.
Here is the code :
<?php
echo "<div class=\"guests-gallery\">";
echo "<ul class=\"guests-gallery-list\">";
$dirs = glob("guests/*", GLOB_ONLYDIR);
foreach($dirs as $model) {
$files = glob($model. "/*.{jpg,png,gif,JPG}", GLOB_BRACE);
foreach($files as $file) {
$m = basename($model);
echo "<li><a href=\"index.php?page=guests2&model=\"" .$m. "\">";
echo "<img src=\"" . $file . "\" alt=\"" .basename($model). "\"></a><br />
<h3>" .basename($model). "</h3></li>";
}
}
echo "</ul></div>";
?>
You're making your code harder to read by double quoting and escaping your HTML quotes so you're not spotting your mistake with the quotes, make it easier to read and write by using single quotes on your echos leaving the double quotes for the html then you won't need to escape them and it'll be easier to spot the extra unnecessary " you included.
Try this:
<?php
echo '<div class="guests-gallery">';
echo '<ul class="guests-gallery-list">';
$dirs = glob("guests/*", GLOB_ONLYDIR);
foreach($dirs as $model) {
$files = glob($model. "/*.{jpg,png,gif,JPG}", GLOB_BRACE);
foreach($files as $file) {
$m = basename($model);
echo '<li><a href="index.php?page=guests2&model=' .$m. '">';
echo '<img src="' . $file . '" alt="' .basename($model). '"></a><br>
<h3>' .basename($model). '</h3></li>';
}
}
echo '</ul></div>';
?>
try removing the quote s from the model and maybe try using the & character without using it encoded:
echo "<li><a href=\"index.php?page=guests2&model=" .$m. "\”>something</a></li>";
Tell me if this works.
Ok, so I have the php echo working and pulling specific url's from the mysql database but this string that I got from an example is adding in single quotes to my href. So instead of being localhost/newdesign/about.php it is localhost/newdesign/'about.php'.
here is the code:
<p>
<?php
echo '' . $row['url'] . '';
?>
</p>
Thank You
It would be simpler if you use <?php echo only around the variables, not the literal HTML parts as well. It also looks like part of the onclick got lost when you were copying to SO.
<p><?php echo $row['url'] ?></p>
No need for the 2nd apostrophe.
"' . $row['url'] . '"
I'm trying to do a simple task, insert a second function name under "onmouseover" but probably something is wrong with the syntax.
echo '<div onmouseover="changeText(); <?php if($title==""){echo changeViz();}; ?> ">';
I probably need to escape some quotes and add some, but i can't figure out the correct solution.
Thanks
Nothing it's working. Let me give you the complete code...:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); <?php if($title[$i]==""){echo changeViz();}; ?>">';
You are nesting <?php ?> inside existing php code, which is a syntax error. Instead, concatenate in the javascript function changeViz() as a quoted string.
This version uses a ternary operator to duplicate the if() statement you had originally.
echo '<div onmouseover="changeText(); ' . ($title == '' ? 'changeViz();' : '') . '">';
The ternary operation here will concatenate changeViz(); onto the echo string if $title == "", or otherwise just concatenate on an empty string.
Update after seeing full code:
You have the quote escaping correct in the first part.
echo '<div class="frame" onmouseover="changeText(\'' . $text[$i] . '\'); ' . ($title == '' ? 'changeViz();' : '') . '">';
You can make your code much more readable if you do not try to do everything in one line:
$onmouseover_action = "changeText(); ";
if($title==""){
$onmouseover_action .= "changeViz(); ";
}
echo '<div onmouseover="'.$onmouseover_action.'">';
It makes your code easier to maintain, and you have less need to comment it, because it describes itself quite much better.
Try this:
echo '<div class="frame" onmouseover="changeText(\''.$text[$i].'\'); '. ($title[$i]=="")?'changeViz();':'').'">';