inserting a variable within another variable string that contains an if statement - php

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

Related

How to call an ACF variable with php to replace a nested html element?

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.

php echo href from mysql issue

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'] . '"

Flexslider with php multiple videos

I have a table "videos" with id_video, client, code. And i found this:
http://demo.juanfra.me/multiple-videos-flexslider-v2/
First i tried put the -li- into a while and show all results with mysqli_fetch_array.
But i cant show anything. I think the id of each iframe "player_number" must be unique, so do i have to use a for()?
<?php while($rowmulti = mysqli_fetch_array($resultmultimedia)){
echo'
<li>
<iframe id="player_1" src="http://player.vimeo.com/video/'.$rowmulti["code"].'?api=1&player_id=player_1"></iframe>
</li>
';}?>
Maybe the error is in the jquery code but i dont good js programmer. thank you!
You don't necessarily need a for loop. You can just keep track of an $id variable within the loop that increments each iteration.
Try this:
<?php
$id = 1;
while ($rowmulti = mysqli_fetch_array($resultmultimedia)) {
echo'
<li>
<iframe id="player_' . $id . '" src="http://player.vimeo.com/video/' . $rowmulti["code"] . '?api=1&player_id=player_' . $id . '"></iframe>
</li>';
$id++;
}
?>

PHP variables not rendering in the right place

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!

Insert function name with php

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();':'').'">';

Categories