0

Good day, I am trying to create a form that will search for results in mysql DB and display the results using a loop. I have two options for searching, one using the ID and one using a string.The ID search works fine, as ID's are unique and one result is return which I can print out, but I am trying to adapt this to a function I can use for both, as the string should return a list results which will need to be looped through. The result is being returned as an object, and this is where I am struggling to get the loop to give a list of attributes and keys for each result, as it loops for each key=>Value fine, but need to know how to go through each object first, then the key=>Value.

In my class this is the find code i am using:

public static function find_by_qual_id($qual_id){
        global $database;
        $clean_qual_id = $database->escape_value($qual_id);
        $result_array = static::find_by_sql("SELECT * FROM sdp WHERE qual_id='{$clean_qual_id}' LIMIT 1");
        return !empty($result_array) ? array_shift($result_array) : false;
        return $result_array;
    }

On the page this is the loop (which does not work):

if(isset($qual_id)){
            $qual_info = Qual_lookup::find_by_qual_id($search_qual_id);
            if($qual_info != null){
                echo "<h4>RESULTS FOUND FOR \"{$search_qual_id}\"</h4>";
                echo "<div><form name=\"found_qual\"><table>";
                foreach($qual_info as $qual){
                    foreach($qual as $key => $value){
                        echo "<tr><td>{$key} : </td><td">{$value}</td></tr>";
                    }
                }
            } else {
                echo "No results found for \"{$search_qual_id}\"";
            }
            echo "</table></form></div>";
        }
Bhavin
  • 2,070
  • 6
  • 35
  • 54
  • pretty much every part of your coding can do with improvement. For starters, you can replace`\"` with either single quotes(`'`) (or `"` for text such as in your `h4` tag), – Martin Aug 20 '16 at 11:13
  • why areyou using static functions rather than proper class objects? – Martin Aug 20 '16 at 11:13
  • Also please [stop using globals in your PHP classes](http://stackoverflow.com/questions/12445972/stop-using-global-in-php) – Martin Aug 20 '16 at 11:15
  • and after your `foreach` loops you should really `unset` your foreach variables (`$key`,`$value`,`$qual`) – Martin Aug 20 '16 at 11:18
  • you have a syntax error in here `echo "{$key} : {$value}";` which should read as `echo "{$key} : {$value}";` - quote too many. Error reporting would have told you about it. – Funk Forty Niner Aug 20 '16 at 13:25
  • Thanks for comments so far, please elaborate on the problem with the global in the class? whats the way around that? – WarrenThePHPNoob Aug 20 '16 at 18:43
  • And the main problem I need corrected is the "foreach($qual_info as $qual){" line as it gives me an error, then loops through the attributes of the object, how can I loop through objects returned? will this even return more than one object anyway? – WarrenThePHPNoob Aug 20 '16 at 18:45
  • As for the syntax error in {$value}, there is a bit more code in this block which I removed and see I left it there by accident here. – WarrenThePHPNoob Aug 20 '16 at 23:18

1 Answers1

0

OK I found the problem... for some reason I have 2 return statements in the class:

return !empty($result_array) ? array_shift($result_array) : false;
return $result_array;

removing the first return statement now makes this work... it seems.