0

I am trying to get this working based on the answers found in this SO question: How to search by key=>value in a multidimensional array in PHP - but I am having no luck.

I am querying the database. I need to take the results and place all the ones that have 'type' = $type into a new array to use that new array for displaying info.

The Search Function:

function search($array, $key, $value){
    $results = array();
    search_r($array, $key, $value, $results);
    return $results;
}
function search_r($array, $key, $value, &$results){
    if (!is_array($array)) {
        return;
    }
    if (isset($array[$key]) && $array[$key] == $value) {
        $results[] = $array;
    }
    foreach ($array as $subarray) {
        search_r($subarray, $key, $value, $results);
    }
}  

The Code:

$getTypes = mysqli_query($link, "SELECT * FROM Type WHERE section = ".$selectValue." ORDER BY `order`");
$getCategories = mysqli_query($link, "SELECT * FROM Category WHERE section = ".$selectValue." ORDER BY `order`");
$getdbvalues = mysqli_query($link, "SELECT * FROM `Data` WHERE section = ".$selectValue." AND `date` = ".$datecomp);
$dbvalues = mysqli_fetch_assoc($getdbvalues);
while($type = mysqli_fetch_assoc($getTypes)){
    $typeid = $type['id'];
    $getdbdata = search($dbvalues, 'type', $typeid);
    while($category1 = mysqli_fetch_assoc($getCategories)){
        // DISPLAY DATA FROM $getdbdata
    }
}
Community
  • 1
  • 1
PBwebD
  • 778
  • 11
  • 33

1 Answers1

1

You should just do this in MySQL.

SELECT * FROM `Data`
LEFT JOIN `Type` ON `Type`.id = `Data`.type
LEFT JOIN `Category` ON `Category`.id = `Data`.category
WHERE `Data`.section = '$selectValue'

I'm guessing on the table structure. Also, if you need to check for selectValue on every table, then you need to add those to the WHERE section of the query.

stomo21
  • 280
  • 2
  • 5
  • What would joining these things do? How would I use this information when displaying (like when I need to display items with a certain type). How is this different from what I'm already doing? – PBwebD Apr 28 '14 at 19:19
  • It is one query vs your 3 queries. You'd be able to add to the WHERE section of it to limit the results. It's also very useful if you want to start sorting your results across multiple tables. http://dev.mysql.com/doc/refman/5.0/en/join.html – stomo21 Apr 28 '14 at 19:23
  • Yes, but I'd need to search the results for a specific type and display accordingly. That's still where I need help. That's fine to join the three queries, but I still don't know how to display the results correctly. The search function that I have up there isn't working. – PBwebD Apr 29 '14 at 15:16