setting the value of a function always=1/7 of the value of another function
-
I have a function: “function_prezzo_minimo”
$number = get_post_meta( $post->ID, "function_prezzo_minimo", true ); echo '<div><input type="number" name="function_prezzo_minimo" value="'.$number.'" placeholder="prezzo/notte in euro" /></div>'; }tha vaue of “function_prezzo_minimo” should be always 1/7 of this other function:
$number = get_post_meta( $post->ID, "function_prezzo_primo", true );They are both in the post editor page
-
Where you now concatenate
$numberto echo out, instead concatenate$number/7.But the output may include a fractional amount to great precision (for example 123.456789). You can round the result to any precision with something like
round( $number/7, 0 )for a full integer. If you want to include the cents amount, use2instead of0.so I have to change this
$number = get_post_meta( $post->ID, "function_prezzo_minimo", true ); echo '<div><input type="number" name="function_prezzo_minimo" value="'.$number.'" placeholder="prezzo/notte in euro" /></div>'; }into
$number = get_post_meta( $post->ID, "function_prezzo_minimo", true ); $number2 = get_post_meta( $post->ID, "function_prezzo_primo", true ); $number === $number2; echo '<div><input type="number" name="function_prezzo_minimo" value="'. round( $number/7, 0 ).'" placeholder="prezzo/notte in euro" /></div>'; }There is something wrong. I should add a conditional. if (empty (
get_post_meta( $post->ID, "function_prezzo_minimo", true ));What to do depends on what’s wrong 🙂
What’s the purpose of
$number === $number2;? As it is it does nothing. Nothing is done with the result of the comparison. No harm done, but there’s no purpose either.Checking if something is not empty before doing something is a common technique. You’d want to avoid needing to redo a DB query by calling get_post_meta() again. Typically we’d assign the query result to a variable. Then see if the variable is empty. It’s still considered “empty” if it has a null or empty string value assigned. If the variable is not empty, then you can do something with it. And optionally do something else if it is instead empty.
Ok, I did this and it’s working:
function prezzo_minimo_meta_box_render( $post ){ // Get the number and display it in a numeric field $number = get_post_meta( $post->ID, "function_prezzo_primo", true ); echo '<div><input type="number" name="function_prezzo_primo" value="'. round( $number/7, 0 ) .'" placeholder="prezzo/notte in euro" /></div>'; }But I have a problem, I dont know how divide for 7 “function_prezzo_primo” in the following
<div class="prezzo"><form><?php echo '<a href="'. get_permalink( get_the_ID() ) .'#titolo_listino_prezzi" >'; ?><input type="button" class="prezzo" value="da <?php echo get_post_meta( get_the_ID(), "function_prezzo_primo", true ); ?> euro/notte"></a></form></div>I tryed
"function_prezzo_primo"/7 but it’s not workingI have 2 problems, the second one is the following. The plugin search and filter let me choose between the meta fields “function_prezzo_minimo” or “function_prezzo_primo”, I have to give to it a meta key that is the result of the division, otherwise it display or the full price or nothing
If you’re sure the meta value will always be assigned you can do this:
value="da <?php echo round( get_post_meta( get_the_ID(), "function_prezzo_primo", true )/7, 0 ); ?> euro/notte"
Otherwise you should verify if the returned value is empty or not before trying to use it.Input and output needs to be symmetrical. If you divide by 7 on output, you need to multiply by 7 whatever is input. Beware of rounding errors being introduced, especially if the value is retrieved and re-saved multiple times. Instead of using
round()you should keep the full, un-rounded amount as the value. However this would not display nicely, you could get something like142.857142857 euro/notte. Maybe better would be; if the amount multiplied by 7 that is to be saved is within a small amount of the value already saved, do not update it.Or even better, maintain a price that’s already divided by 7 and don’t do any math on the amount in or out.
I’m testing the suggested solution and it seems to me adequate
I’m trying to put a <strong> concerning only “function_prezzo_primo” but unsuccesfully. I tryed both out <?php and inside, using ‘<strong>’ or . ‘<strong>’ . ; maybe in an echo construct this is not possible
It’s difficult because it concerns only the number, if I’d wish put strong all the button content, I could use the class and css
Apparently both the overall div and the input field both have
class="prezzo"? It’s OK to do that, but unnecessary because child elements within the overall div inherit the div’s styles anyway. You could use a unique class for the element you want bold, whether it’s an additional class or replacing “prezzo”. Then style that class’ font-weight as bold. IMO this is preferable to using explicit<strong>tags. It makes future restyling easier if the HTML needn’t change, the only changes are in CSS.But if you prefer
<strong>tags, wrap the entire<input>field with them. As part of the HTML portion of your code, PHP is not involved at all.I had already tryed via css, but it’s not possible to isolate just the number. On the button you read “da X euro/ notte” . Only X should be <strong>
Adjust where the tags go then:
value="da <strong><?php echo get_post_meta( get_the_ID(), "function_prezzo_primo", true ); ?></strong> euro/notte"Or if you prefer to use CSS, use span tags (with a class attribute) in place of the strong tags and use CSS to target the assigned class attribute.
The topic ‘setting the value of a function always=1/7 of the value of another function’ is closed to new replies.