skip to Main Content

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


  1. 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 '

    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)) {
                    $found = null;
                    // find item in filter1 by $bf_array['id']
                    foreach ($filter1 as $filter) {
                        if ($filter['id'] == $bf_array['id']) {
                            $found = $filter;
                            break;
                        }
                    }
                    if ($found) {
                        $custom_fields[] = 'pf.' . $bf_array['field'] . ' = "' . $found['name'] . '" ';
                    }
    
                }
            }
        }
    }
    

    Attempt 2:

    Create an array of filters. So you can iterate them.

    $custom_fields_array['Name_of_filter_1'] = $filter1;
    $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)) {
    
                    $found = null;
                    foreach ($custom_fields as $name_of_filter => $filter) {
                        foreach ($filter as $item) {
                            if ($item['id'] == $bf_array['id']) {
                                $found = $item;
                                break;
                            }
                        }
                        if ($found) {
                            break;
                        }
                    }
    
                    if ($found) {
                        $custom_fields[] = 'pf.' . $found['field'] . ' = "' . $found['name'] . '" ';
                    }
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. 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.

    $lookup = [];
    foreach ($mysqli->query('SELECT name, number FROM customers') as $i => ['name' => $name, 'number' => $number]) {
        $lookup[$i + 100] = ['name', $name];
        $lookup[$i + 200] = ['number', $number];
    }
    
    $custom_fields = [];
    foreach ($custom_fields_array as $block_array) {  
        foreach ($block_array ?? [] as $bf_array) {
            if (isset($lookup[$bf_array['id']])) {
                $custom_fields[] = vsprintf('pf.%s = "%s"', $lookup[$bf_array['id']]);
            }
        }
    }
    

    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…

    $custom_fields = [];
    $paramters = [];
    foreach ($custom_fields_array as $block_array) {  
        foreach ($block_array ?? [] as $bf_array) {
            if (isset($lookup[$bf_array['id']])) {
                $custom_fields[] = sprintf('pf.%s = ?', $lookup[$bf_array['id']][0]);
                $parameters[] = $lookup[$bf_array['id']][1];
            }
        }
    }
    

    Then just append these conditions to your WHERE clause and feed the parameters array as the values for the ? placeholders.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search