I’m trying to replace a static way of getting data for a custom SQL by a dynamic way.
$filter1 = array( array('id' => 101, 'field' => 'name', 'name' => 'John'),
array('id' => 102, 'field' => 'name', 'name' => 'Mary'));
$custom_fields_array['Name_of_filter_1'] = $filter1;
$filter2 = array( array('id' => 201, 'field' => 'number', 'name' => '1234'),
array('id' => 202, 'field' => 'number', 'name' => '4321'));
$custom_fields_array['Name_of_filter_2'] = $filter2;
foreach($custom_fields_array as $block_name => $block_array) {
if($block_array <> NULL) {
foreach($block_array as $bf_array) {
if(!empty($get_custom_fields) and in_array($bf_array['id'],$get_custom_fields)) {
if ($bf_array['id'] == 101) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "John" ';
} else if ($bf_array['id'] == 102) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "Mary" ';
} else if ($bf_array['id'] == 201) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "1234" ';
} else if ($bf_array['id'] == 202) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "4321" ';
}
}
}
}
}
Now the first thing I replaced ($filter1) works:
$cs_dl_query = mysqli_query("SELECT name FROM customers");
if (mysqli_num_rows($cs_dl_query)) {
$item = 100;
while($res_cs_dl = mysqli_fetch_array($cs_dl_query)){
$item++;
$cs_dl_array[] = array('id' => $item, 'field' => 'name', 'name' => $res_cs_dl['name']);
}
}
$filter1 = $cs_dl_array;
$cs_dl2_query = mysqli_query("SELECT number FROM customers");
if (mysqli_num_rows($cs_dl2_query)) {
$item = 200;
while($res_cs2_dl = mysqli_fetch_array($cs_dl2_query)){
$item++;
$cs_dl2_array[] = array('id' => $item, 'field' => 'number', 'name' => $res_cs2_dl['number']);
}
}
$filter2 = $cs_dl2_array;
The problem is to prepare the if statements in the foreach block with the data collected for $filter1
I’ve tried to add it to a string and add the string in between the other if statements, but that just gives errors.
Any advice how I could dynamically create this:
if ($bf_array['id'] == 101) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "John" ';
} else if ($bf_array['id'] == 102) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "Mary" ';
} else if ($bf_array['id'] == 202) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "1234" ';
} else if ($bf_array['id'] == 202) {
$custom_fields[] = 'pf.'.$bf_array['field'].' = "4321" ';
}
With the pointers of IT goldman, this is what solved it.
foreach($custom_fields_array as $block_name => $block_array) {
if($block_array <> NULL) {
foreach($block_array as $bf_array) {
if(!empty($get_custom_fields) and in_array($bf_array['id'],$get_custom_fields)) {
foreach ($bf_array as $name_of_filter => $filter) {
if ($filter == $bf_array['id']) {
$custom_fields[] = 'pf.' . $bf_array['field'] . ' = "' . $bf_array['name'] . '" ';
}
}
}
}
}
}
}
This results in this being created:
$custom_fields[] = 'pf.'.$bf_array['field'].' = "Mary" ';
2
Answers
From what I understand you’re basically want to find an item in array by a key?
By the way, if all of these are going into a SQL query, pay attention not to be vulnerable to SQL injection, if one of the values contains
'
Attempt 2:
Create an array of filters. So you can iterate them.
I don’t know exactly what you are doing AND it doesn’t look like good practice, but if you want to search for data based on your generated numeric values, create a lookup array with those numbers as keys.
This is far more tidy. …but I these stored "expressions" shouldn’t be used to query the database. Prepared statements need to be implemented — the adjustment isn’t hard…
Then just append these conditions to your WHERE clause and feed the parameters array as the values for the
?
placeholders.