Hi there,

I'm currently trying to pull in two numbers (1 from a MySQL Database and another from a WordPress custom field) and multiply them.

E.g: <?php echo ($data['price'] / 2) * NUMBER-FROM-DATABASE ;?>

Can I pull in from the Database in a normal way and place the code in this formula or do I have to do something else with it?

Thanks in advance,

Barry

1 answer

0
points

Hey Barry, I'm not too familiar with wordpress, but I think this should work:

<?php
    // Connect to the other database
    mysql_connect("localhost", "username", "password") or die(mysql_error());
    mysql_select_db("otherDB") or die(mysql_error());

    // Use this to get the custom field value
    // According to the docs @ http://codex.wordpress.org/Function_Reference/get_post_custom_values

    $mykey_value = get_post_custom_values('my_key');
    // get the first value
    $first_value = $mykey_value[0];

    $result = mysql_query("SELECT price FROM example")
    or die(mysql_error());  

    // store the record of the "example" table into $row
    $row = mysql_fetch_array( $result );
    $second_value = (int) $row[0]['price'];
    $final_value = ($first_value / 2) * $second_value;

    print $final_value;
?>
Answered almost 6 years ago by Conor Mongey