I’m using WordPress for this, but it is not a WordPress-centric issue, so I am asking here.
You can see a snippet of my code here: http://pastebin.com/Cbc8wKvB
<?php
function getFormIds()
{
global $wpdb;
$sql = "
SELECT a.id
FROM wp_rg_lead a
WHERE a.form_id = 10 AND a.payment_status = 'Approved'
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
function getFormInfo($form_id)
{
global $wpdb;
$sql = "
SELECT *
FROM wp_rg_lead_detail
WHERE lead_id = $form_id
";
$query = $wpdb->get_results($sql);
if($query)
return $query;
return false;
}
$credit_card_form_ids = getFormIds();
if ( $credit_card_form_ids ) {
$entries = array();
foreach( $credit_card_form_ids as $entry) {
$single_entry = getFormInfo($entry->id);
if ( $single_entry ) {
$entry_array = array();
$full_array = array();
foreach( $single_entry as $entry ) {
$curr_array = array( $entry->field_number => strip_tags($entry->value));
$entry_array[$entry->field_number] = strip_tags($entry->value);
array_push($full_array, $entry_array);
}
}
}
#var_dump('full_array', $full_array);
}
Basically I’m running a DB query to grab all entry ID’s that match a certain criterion. This returns an array of objects of single values. I then pass this array through a foreach() and try to extract/combine data to make it easier to work with.
Here is a print_r() of a $single_entry from the code above: http://pastebin.com/RZmfD2EU
single entry:
Array
(
[0] => stdClass Object
(
[id] => 1983
[lead_id] => 86
[form_id] => 10
[field_number] => 34
[value] => 695
)
[1] => stdClass Object
(
[id] => 1982
[lead_id] => 86
[form_id] => 10
[field_number] => 39
[value] => 0
)
[2] => stdClass Object
(
[id] => 1981
[lead_id] => 86
[form_id] => 10
[field_number] => 40.1
[value] => Yes
)
... etc etc.
With the code I provided, the final array seems to collapse somehow — meaning I lose entries somewhere along the way.
Ideally I’d love the data to associate id‘s to their key => value pairs. Namely:
[id] => [lead_id]
[field_number#] => [field_number_value]
For a actual representation of the first few lines of data provided:
Array (
[0] =>
(
[id] => 86
[34] => 695
[39] => 0
[40.1] => Yes
...etc
)
)
Is there a better way to loop through and associate data than what I’m doing? I’m relatively new/bad at PHP and MySQL and would love some guidance.
I’m not sure if I’m understanding it right but if you want to assign values to specified keys…