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
    }
}
6
  • Where are you using returned array $subarray from the search_r function? Commented Apr 25, 2014 at 14:58
  • Sorry, wrong function. Updated. Commented Apr 25, 2014 at 15:02
  • I feel like this is something that could/should be done with a JOIN. Commented Apr 25, 2014 at 15:05
  • Is there some sort of example or explanation for a JOIN in this case? Commented Apr 25, 2014 at 15:49
  • We'd need to see the structure of your tables. Commented Apr 25, 2014 at 15:59

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
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. dev.mysql.com/doc/refman/5.0/en/join.html
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.