logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
PHP Array question

I have a few custom tables in my database and I need to echo some values but not sure how to.

This is how part of the array looks when I Print it


<?php
$id = 19200;
$myrows = $wpdb->get_results( "SELECT * FROM _isContact WHERE Id='".$id."'");

<pre>
print_r ($myrows);
</pre>
?>





Array
(
[0] => stdClass Object
(
[Id] => 19200
[meta_field] => Address2Street1
[meta_value] => 2400 N Merritt Loop
)

[1] => stdClass Object
(
[Id] => 19200
[meta_field] => Address2Street2
[meta_value] => Suite A
)

[2] => stdClass Object
(
[Id] => 19200
[meta_field] => City
[meta_value] => Tempe
)

[3] => stdClass Object
(
[Id] => 19200
[meta_field] => City2
[meta_value] => Tempe
)

[4] => stdClass Object
(
[Id] => 19200
[meta_field] => CompanyID
[meta_value] => 0
)


Basically how do I get the meta_value of a meta_field that equals say CompanyID. The array numbers change because I will be creating more values so I cant use $companyid = $myrows[4]->meta_value because it may not always be [4]

Thanks!

This question has been answered.

platinum | 10/09/11 at 9:08pm Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    10/09/11
    9:14pm
    Julian Lannigan says:

    Add this just after the query.


    $custom = array();
    foreach ($myrows as $item) {
    $custom[$item->meta_field] = $item->meta_value;
    }


    And then you can use $custom["CompanyID"] to get that meta field.

    Previous versions of this answer: 10/09/11 at 9:14pm

  • avatar
    Last edited:
    10/09/11
    9:14pm
    Maor Barazany says:

    foreach ($myrows as $row)	{
    if $row->meta_field == 'CompanyID';
    {
    $value = $row->meta_value;
    }
    }

    echo $value;

    • 10/09/11 9:20pm

      Maor Barazany says:

      And if you want to use all the different meta_keys you have, you can use this:

      $values = array();
      foreach ($myrows as $row) {
      $values[$row->meta_field] = $row->meta_value;
      }

      Than you will have the values in the array as keys -
      $custom['CompanyID'] 
      $custom['City']
      $custom['Address2Street1']

      etc...

    • 10/09/11 9:21pm

      Maor Barazany says:

      In these -

      $values['CompanyID'] 
      $values['City']
      $values['Address2Street1']

  • avatar
    Last edited:
    10/09/11
    9:27pm
    Romel Apuya says:

    Previous versions of this answer: 10/09/11 at 9:27pm

  • avatar
    Last edited:
    10/09/11
    9:40pm
    ej_emman says:

    I think you need this..

    <?php
    foreach ($myrows as $echo_some_rows)
    {
    if ($echo_some_rows->meta_field == 'city2'); //your conditional tags to display rows
    {
    echo 'theID-> ' .$echo_some_rows->ID;
    echo 'theMetafield-> '.$echo_some_rows->meta_field;
    echo 'themetavalue->'. $echo_some_rows->meta_value;
    echo '<br>';
    }

    }
    ?>

    Previous versions of this answer: 10/09/11 at 9:40pm

This question has expired.



Gabriel Reguly, platinum, Luis Abarca voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.