This is probably really basic and my first PHP question on here but here goes...
I'm working with WordPress and trying to output ACF data assigned to the current user.
So far I have:
<?php $current_user = wp_get_current_user();
echo the_field('price_list', 'user_$current_user->ID'); ?>
So basically, the output of wp_get_current_user is e.g. 2, but ACF needs to be passed the value as e.g user_2... so my question is how can I prepend the user_ to the 2?
I'd rather add it in at the output stage rather than adding it to the variable if that makes sense, as the variable is used elsewhere too.
You can use string concatenation:
echo "user_".$current_user->ID;
So in your example and specifications you would use it like this:
echo the_field('price_list', 'user_'.$current_user->ID);
Related
I am trying to dynamically set the first dimension of a multidimensional array using a value from an ACF input field in Wordpress with the ID of fund_data_id. The array is stored within the variable $fund_array and when manually drilling down to the specific data point I want to access within the array, it works just fine. An example of manually drilling down successfully is: $fund_array[1][5].
Ideally, I'd like to be able to set the first dimension of the array (which is currently [1]) to read the input from the ACF field fund_data_id in Wordpress, which I set to a number value of 1. However, the first dimension of the array isn't recognizing the ACF input from Wordpress. I've tried:
<?php
echo $fund_array[the_field('fund_data_id')][5] ;
?>
and...
<?php
$num = the_field('fund_data_id');
echo $fund_array[$num][5] ;
?>
The second dimension will stay constant, so there's no need to change that one, just the first dimension via an input from an ACF field.
Thanks in advance to everyone for any help or suggestions!
the_field() does an echo, which you do not want.
You need to use get_field('fund_data_id')
So this should work:
<?php
echo $fund_array[get_field('fund_data_id')][5] ;
?>
I am a new bee to this. I am trying to store the result of a random value generated into a variable which then I will be storing in MySQL DB.
Tried a code like below
$des_nu1 = echo rand(100,9999);
but looks like the statement isn't correct. Please advise.
I did see an option to use session but is there any alternative than using session ( Ref using Session : How to store a variable in php using session)
Remove the echo
$des_nu1 = rand(100,9999);
The only thing echo does is this Output one or more strings. Since you don't want to output the result of your function you don't need echo. After you assign that value to your variable you can use it anyway you want and you can also use it for echo
echo $des_nu1;
$temp = $des_nu1 + 10; //etc
Always refer to the manual,
echo is language construct echo manual that returns nothing, it will write in standard output (stdout)
I advise you to always use sprintf()
sprintf("%i", rand(100,9999));
I am making a dashboard using php and displaying it on webpage using html. For this I am taking some count values. Is !$value[" variable"] valid in php? How do i get the not of (!=) value? There is no other way to get the data. I am getting the values from a database.
print "<td><a href=\"$strBase and status!='Closed'\" target=\"_blank\">".$value[" ? "]."</td>";
I need to print !closed. Somehow I do not know how to pass that inside $value[].
If you want to use a variable as a key in order to retrieve a value from an array you can do as follows (I use a snippet of your code):
.$value[$myVar].
Where $myVar should be a string with the name of the key you want to retreive from $value
But anyhow, your question is pretty unclear so I'm not sure if this is what you are looking for.
You can check if the variable is empty(), i.e.:
if(empty($value["variable"])){
echo "variable is empty";
}
How to use a varible to store the value of the for loop?
for($i=1;$i<=$var;$i++)
How to get $i's value and store into another value and show its result?
I am a beginner of PHP and I want to improve my concept, I am very grateful if anyone can helps, Cheers!
Are you want dynamic variable or just a normal variable which will give you the out put like 12345678910
<?php
$var=10;
$a='';
for($i=1;$i<=$var;$i++){
$a.=$i;
}
echo $a;
?>
output: http://3v4l.org/2p8L6
or if you want dynamic variable then use following code to make a variable dynamic
${"num_" . $i} = $i;
output will be
$num_1=1;
$num_2=2;
$num_3=3;
etc
In plain English; PHP is updating the value stored in lopping variable automatically, and this is the whole idea behind using for loops, not only in PHP, but also in other programming languages.
Usually, you would want to use it (the variable $i) as a kind of a counter, and run code in the loop based on the iteration count.
I'm trying to use some simple AJAX and then passing my variable through a HTML string.
Struggling to get some PHP included to print the value into the string. Been trying like this and the variable prints blank even though there is a value assigned to $user_id.
xmlhttp.open("GET","update.php?user=<?php $user_id ?>&q="+str,true);
Any suggestions on how I could do this or better solutions.
Thanks
you forgot echo:
xmlhttp.open("GET","update.php?user=<?php echo $user_id; ?>&q="+str,true)