This question already has answers here:
Is there a difference between '<?=' and '<?php'? [duplicate]
(5 answers)
Closed last year.
I want to display only certain data from database on my page, so I end my foreach loop with if and break; statement. I checked with var_dump() and the data is being provided from database correctly, function getName() is also correct.
Moreover I inserted there a test "echo"test"; which is displaying well. But getName() is still missing.
I think that problem might be with this if statement, because when I delete it everything works correctly (except that I get data I dont want to get).
Same situation is when I insert if statement after the getName() function - this part of the code is not executed, it seems.
<?php //var_dump($leagues); ?>
<?php foreach($leagues as $leauge): ?>
<?php if($leauge->getLevel() != 'A') { break; } ?>
<li>
<i class="fas fa-long-arrow-alt-right"></i>
<?php $leauge->getName(); echo "test"; ?>
</li>
<?php endforeach; ?>
There is one echo missing.
Replace: <?php $leauge->getName(); echo "test"; ?>
With: <?php echo $leauge->getName(); ?>
Consider to use continue; instead of break;
The break statement terminates the whole loop early whereas the
continue brings the next iteration early. Reference: Geeks for Geeks
As you are using break IF $leauge->getLevel() != 'A', in case of your first iteration goes to this condition, your entire loop will terminate. Just change to continue if you wanna "skip the current league, but go to the next one and check again".
Rewriting it here so it will be more seeable. What helped me:
In this line of code
<?php $leauge->getName(); echo "test"; ?>
When i want to display results i have to use
<?= instead of <?php . Why is that i dont really know.
Related
Need help with some php to modify a WP plugin which is paid memberships pro. Terrible at php here.
What am trying to do is create one line of code that would say if the membership level equals XXX then print this link. SO the variable I would need are somewhere in this line I imagine:
<li><strong><?php _e('Membership Level', 'paid-memberships-pro' );?>:
</strong> <?php echo $current_user->membership_level->name?></li>
The above is just a snippet of code already found in the page I want to create this if/then link statement.
so something like:
<?php if($Membership Level == $Conflicts of Interest #14124(that's the name
of one level) then print this link.
AM I making sense?
Edit:
Thanks to some help below, this seems to work:
<?php if($membership_level == 'Conflicts of Interest #14124') {
echo "<a href=\"conflicts-of-interest-in-modern-legal-practice-and-internal-
investigations-14124/\">Testing</a>";
}
?>
But the 'Conflicts of Interest #14124' doesn't match even though it is the correct name.
Then general If else statement in the html page
<?php if($membership_level == 'string to be compared') {
echo 'the link that you want to print if it is a string. you can replace this string
with a variable if the value is in variable'
} else {
'any other text if require else you can remove the else block'
}
?>
Hope that helps.
My problem is that I need to save a value from the link as a variable which will then do something. Below is my main code for this problem.
<table>
<? foreach ($csv as $row) : ?>
<tr>
<td><? echo $row[0]; ?></td>
</tr>
<? endforeach; ?>
</table>
Basically, it prints the first column from my array $csv. However I want to save the '$row[0]' for each link - depending on which one is clicked.
This happens here:
<?php
if (isset($_GET['GoToProfile'])) {
}
?>
This works. E.g. when something is clicked it prints something. But I cannot find a way to save the values from each link. Depending on which one is clicked. I have tried many different methods online, but none seem to work.
I have even tried:
<? echo $row[0]; ?>
Any help would be greatly appreciated.
Thanks!
Use an ampersand (&) instead of a question mark
<? echo $row[0]; ?>
The ? indicates the beginning of the query string, which is the data sent on a GET request. In most cases it is a collection of name/value pairs, separated with & s.
A simple example of a GET request
http://example.com?first=1&second=fifty
You would get the value of the parameters in PHP with $_GET
$first = $_GET['first'];
$second = $_GET['second'];
To see what the server is receiving, you can use var_dump
var_dump($_GET)
Have been reading through multiple similar questions and went over my syntax many times, but I can't figure out why my PHP code is executing both conditions.
I'm trying to replace the url of an element with the string from a custom field if the field is not empty. If the field is empty, I want to output the permalink normally. What happens is that the code concatenates both the string from the custom field and the permalink when the field is not empty. If i remove the string in the field, it works fine.
<div class="profile-content">
<a href="
<?php
if ( the_field('direct_resource_link') != '') {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
Thanks!
Dan.
EDIT after comment from original poster
My initial assessment (left below for reference) was correct. You are using function that will print/echo content instead of returning it. Your if will always evaluate to false, because you are calling function that returns nothing; and PHP thinks that nothing and empty string are the same thing.
You didn't see that when field was empty, because the_field() for empty field printed empty string (or nothing at all), i.e. it didn't modify value printed by the_permalink() in any way/
According to ACF documentation, the_field() is accompanied by get_field() which returns value instead of printing it.
Your code should look like that:
<div class="profile-content">
<a href="
<?php
if ( get_field('direct_resource_link') ) {
the_field('direct_resource_link');
} else {
the_permalink($id);
} ?>
"><h5><?php the_title(); ?></h5></a>
<div class="profile-footer">
My initial post
What happens is that you run function the_field('direct_resource_link') and compare it's return value to ''; if that value is empty, you run the_permalink($id);.
It's hard to tell what the_field() is and what it is supposed to do, but I guess that it prints value instead of returning it. So if field is empty, it prints nothing, resulting in pure run of the_permalink(). If field is not empty, it prints it content and returns nothing. Since nothing is equal to empty string, PHP proceeds with else branch and invokes the_permalink() that prints additional info.
Solution: modify the_field() to return value instead of printing it, or make additional function that will query for value and return it, and use that function in if statement.
Miroslaw Zalewski already answered your question here, so this is simply to show you the kind of code needed to fix your issue:
function get_the_field($field) {
ob_start();
the_field($field);
return ob_get_clean();
}
This code will start an output buffer (which will capture all echo'd data), run the_field and return (and delete) the output buffer (the echo'd data from the_field). This means you can simply do the following:
...
<?php
$theField = get_the_field('direct_resource_link');
if ( $theField != '') {
echo $theField;
} else {
the_permalink($id);
}
?>
...
This can all be simplified. the_field() echoes a meta value. You don't want that...Instead, you want to return the value to check it, before conditionally echoing it. You can do this using get_field().
In the simplest form, your final code would look like the following:
<a href="<?php get_field('direct_resource_link') ? the_field('direct_resource_link') : the_permalink($id); ?>">
<h5><?php the_title(); ?></h5>
</a>
On my page I'm trying to run two functions from the same .PHP document, however I was getting the error "function already declared".
I had a look here: PHP: how to avoid redeclaring functions?
After looking at this I changed my code to:
<?php
include_once('resource/buildtalentpage.php');
while($row = mysqli_fetch_array($result2)) {
echo getTalentDetails($row);
}
?>
/////////// Loads of HTML ///////////
<?php
include_once('resource/buildtalentpage.php');
while($row = mysqli_fetch_array($result2)) {
echo getTalent($row);
}
?>
The good news is, I don't get the error any more. The bad news is, that function 'getTalent' no longer seems to be called?
The result set from mysql doesn't get reset so your second loop condition returns false the first time through, hence the code never gets called. The easiest thing to do here is do all of your work in one loop.
<?php
include_once('resource/buildtalentpage.php');
$talentHtml = "";
while($row = mysqli_fetch_array($result2)) {
echo getTalentDetails($row);
$talentHtml.= getTalent($row);
}
?>
/////////// Loads of HTML ///////////
<?php
echo $talentHtml;
You should include the file that defines your function one time only. You can then call the function as many times as needed
I have a php function which displays a rating bar with the arguments. I have a variable called itemID inside my php page which holds the unique item number. I need to send this value to my function and also echo command must stay. Is there a way to achieve this?
Here is the code, which does not work. When I try it on the server, it does not show the id of item, it prints the variable name as it is.
<?php echo rating_bar('$as',5) ?>
What I get at html file:
<div id="unit_long$as">
instead of the item id in place of $as.
Single Quotes do not support variable replace,
$as = "test";
echo '$as'; //$as in your end result
echo "$as"; // test in your end result
echo $as; // test in your end result
//For proper use
echo " ".$as." "; // test in your end result
Update for newer PHP versions you should now use Template Syntax
echo "{$as}"
If I get what you are saying, this is what you are asking.
<?php echo rating_bar($itemID,5); ?>
With the limited code you are providing, thats what looks like you are asking.