jimmy
jimmy

Reputation: 65

work with mysql array data in PHP

how to convert array data to work with it in this case? especially in wordpress. when i print the results i get stdClass Object.

Array
(
    [0] => stdClass Object
        (
            [posts_id] => 336
            [value] => 8
            [count(*)] => 2
        )

    [1] => stdClass Object
        (
            [posts_id] => 329
            [value] => 2
            [count(*)] => 1
        )

    [2] => stdClass Object
        (
            [posts_id] => 327
            [value] => 3
            [count(*)] => 1
        )

    [3] => stdClass Object
        (
            [posts_id] => 338
            [value] => 1
            [count(*)] => 1
        )

)

and this is my query

   <?php global $wpdb;

$test = $wpdb->get_results('select
    posts_id,
    value,
    count(*)
From
    wp_mrp_rating_item_entry_value
group by
    posts_id,
    value
order by
    count(*) desc'); 
echo '<pre>';
print_r($test);
echo '</pre>';

How can i get the data out of this array to save into another database table? Or how can i at least save the single values into variables for every id? I tried allot but i have no idea yet so i hope someone is knowing it better than me? thanks for any help.

Upvotes: 0

Views: 177

Answers (5)

Vineet
Vineet

Reputation: 4645

//Just use type cast brother. Use

$test = (array) $test;

//print_r($test);

foreach($test as $item) :

//if you want to place into the db 
$wpdb->query( $wpdb->prepare( 
    "INSERT INTO $wpdb->name_of_wp_table_goes_here
        ( post_id,value,count )
        VALUES (".$item['post_id'].", '".$item['value']."', ".$item['count'].")
    %d",  
    1
) );

endforeach;

Upvotes: 1

user3225501
user3225501

Reputation: 74

foreach($test as $item) :

$ID = $item->posts_id; 
var_dump($ID); //test variable


//if you want to place into the db 

   //DB CODE>>>

$firstName = "Dylan"  //example of field in db to change
$LastName =  "bloggs" //example of second field in db to change

$wpdb->query( $wpdb->prepare( 
	"
		INSERT INTO $wpdb->name_of_wp_table_goes_here
		( name_of_field_goes_here )
		VALUES ( %d, %s, $s ) 
	", 
    $ID,
    $field
) );

   //<<<DB END

endforeach; 

Remove the DB code if you want to manually store into a database.

You could also access other keys the same way. e.g., $value = $item->value; -- you would place this in the foreach loop and continue.

Upvotes: 2

RiggsFolly
RiggsFolly

Reputation: 94662

Well you have an array of objects, which is a very nice contruct. All you need to do is process over the array.

I cannot help you with the WordPress mechanisms for a table insert but the pseudo code would be something like this.

First it would be a good idea to give the count a more usable name

$test = $wpdb->get_results('select
    posts_id, value,count(*) as how_many
From wp_mrp_rating_item_entry_value
group by
    posts_id, value
order by count(*) desc'); 

Now process each occurance of the array of objects

foreach ($test as $obj) {
    // Use these fields in an Insert statement
    $sql = "INSERT INTO TableName (f1,f2,f3)
            VALUES ({$obj->post_id}, 
                    {$obj->value}, 
                    {$obj->how_many}");
    // WP command to issue a query
}

Upvotes: 1

taliezin
taliezin

Reputation: 916

As this is array of objects, you can use simple foreach to access your values:

<?php
    foreach ($test as $t) {
        $t->post_id;
        $t->value;
        // You can insert your values in different table here
    }
?>

For count you can change a bit your query to:

SELECT posts_id, value, COUNT(*) AS cnt FROM
    wp_mrp_rating_item_entry_value
GROUP BY
    posts_id,
    value
ORDER BY
    COUNT(*) DESC

so your count in foreach-loop will be accessible $t->cnt.

Upvotes: 1

Bang
Bang

Reputation: 929

In Wordpress when you use get_results you can choose de hydration mode :

output_type One of three pre-defined constants. Defaults to OBJECT. OBJECT - result will be output as an object. ARRAY_A - result will be output as an associative array. ARRAY_N - result will be output as a numerically indexed array.

So if you do :

$test = $wpdb->get_results('select
posts_id,
value,
count(*)
From
wp_mrp_rating_item_entry_value
group by
posts_id,
value
order by
count(*) desc', ARRAY_N); 

You should get an array as result.

Upvotes: 1

Related Questions