I'm working on some code where I'm getting values from a db and comparing them to values I've gotten from a web form. It works correctly, but I don't like the format of my array I'm using because later on, I have to instantiate "custom fields" with the values. The problem is that there are "groups" that may have many "fields", but the "fields" can have one and only one "value". Currently, my array is not grouped that way:
Code: Select all
Array
(
[0] => Array (
[0] => 4 (Edit: Group ID)
[1] => 6
[2] => General Facilities
)
[1] => Array (
[0] => 4 (Edit: Group ID)
[1] => 5
[2] => DEC
)
[2] => Array (
[0] => 5 (Edit: Group ID)
[1] => 7
[2] => test
)
)I'm currently just using a foreach loop to simply loop through all of the values:
Code: Select all
function instantiate_custom_fields($custom_fields)
{
$field_handler = new cer_CustomFieldGroupHandler();
$filename = "test_instantiate.txt";
$fp = fopen($filename, "a");
foreach($custom_fields as $cf_fields)
{
if($cf_fields[0] != "")
{
fwrite($fp, "group id: $cf_fields[0]\n");
fwrite($fp, "field id: $cf_fields[1]\n");
fwrite($fp, "field value: $cf_fields[2]\n");
//$instance_id = $field_handler->addGroupInstance("T", $this->ticket_id, $val[0]);
//$field_handler->setFieldInstanceValue($val[1], $instance_id, $val[2]);
}
}
fclose($fp);
}Does anyone have a suggestion about doing this? I'm going to keep working on it, but I was wondering if anyone here had any ideas. I'd rather modify the instantiate_custom_fields code than my find_custom_fields_in_body, since that logic in more involved in the latter (I've included it at the end of this post, in case you want to look at it - it's kind of long), but I'm open to anything. I've Googled it and looked at the manuals, but nothing I've tried has worked. Sorry to be a bother, but I'm not very good at multidimensional arrays.
Thanks! I really appreciate everyone's help!
Code: Select all
function find_custom_field_in_body(&$raw_email)
{
$custom_field_values = array();
$sql = "SELECT g.group_id, g.group_name, f.field_id, f.field_name, f.field_type ".
"FROM field_group g ".
"LEFT JOIN `fields_custom` f ON (f.field_group_id = g.group_id) ".
"ORDER BY g.group_name";
$res = $this->db->query($sql);
preg_match_all("/\[Group\](.*)/i",$raw_email->body,$groups, PREG_SET_ORDER);
preg_match_all("/\[Field Name\](.*)/i",$raw_email->body,$field_names, PREG_SET_ORDER);
preg_match_all("/\[Field Value\](.*)/i",$raw_email->body,$field_values, PREG_SET_ORDER);
if($this->db->num_rows($res) > 0)
{
$counter = 0;
while($row = $this->db->fetch_row($res))
{
if($row["group_id"] != NULL)
{
foreach($groups as $group_name)
{
if(strcmp(rtrim($group_name[1]), stripslashes($row["group_name"])) == 0)
{
foreach($field_names as $f_name)
{
if(strcmp(rtrim($f_name[1]), stripslashes($row["field_name"])) == 0)
{
foreach($field_values as $f_val)
{
if(strcmp($row["field_type"], "D") == 0)
{
$sql2 = sprintf("SELECT o.field_id, o.option_id, o.option_value ".
"FROM fields_options o ".
"WHERE o.field_id = %d ".
"ORDER BY o.option_order, o.option_value ASC",
$row["field_id"]
);
$res2 = $this->db->query($sql2);
if($this->db->num_rows($res2))
{
while($row2 = $this->db->fetch_row($res2))
{
if(strcmp(rtrim($f_val[1]), $row2["option_value"]) == 0)
{
$custom_field_values[$counter] = array($row["group_id"], $row["field_id"], $row2["option_value"]);
}
}
}
}
else
{
$custom_field_values[$counter] = array($row["group_id"], $row["field_id"], $f_name[1]);
}
}
}
}
}
}
$counter++;
}
}
}
return $custom_field_values;
}