1

My goal is to have two ways to filter the displayed data. The first is how many days back in time to display. The other allowing the user to select the group to display. They should work independently of each other. But also in tandem, select 20 days back for group NSV and only records with both criteria are displayed. But select 20 days back for all groups and that works too.

The first drop-down is a simple HTML select/option setup seen on screen as ‘Show 10 days back’. The other is seen as ‘All Groups’ on the page and is created from a MySQL table. To do this I have this function;

  function GroupSelect() {
  $g_query = "SELECT DISTINCT b.g_code, concat(b.g_code, '    ', b.g_name) as theGroups FROM skeds AS a, Groups AS b
   WHERE a.g_code = b.g_code
   ORDER BY b.g_name";  //echo $g_query;  
   foreach($db_found->query($g_query) as $g_row) {
  $bg_code   = $g_row['g_code'];  
  $theGroups = $g_row['theGroups'];  
  $theGroupsSelect .= "<option value=\"$bg_code\">".$theGroups."</option>";
  }
   echo $theGroupsSelect;
  } // End GroupSelect

This function or at least the query work great and the correct records are selected from the table. The and statements are in HTML.

I want either/or both to run when the ‘Filter’ button is selected. Both drop-down lists are in the same DIV tag (selectDIV) and both use the same and whose name is ‘Filter’. Changing either of the two drop-downs, and clicking on ‘Filter’ would cause the display to change.

The call to the GroupSelect function is down around line 434 and looks like this;

The problem is no matter where I put my PDO database allocation, in the ‘GroupSelect’ function or in the primary function of the page ‘buildlist’ I get a Fatal error. The only difference is where it points in the code that is causing them.

Fatal error: Call to a member function query() on a non-object in .../arhab/hab_launch_list_new.php on line 12

So my first issue is where to put the PDO database allocation so it works for both functions. I’ve tried it in both functions individually, as well as above both functions. No matter where I put it I get the above fatal error, only the line number changes.

I think my issue is related to not understanding how to use the PDO to pull both sets of information. They are both in the same MySQL data base, and both use the “skeds” table. However the larger function also uses two other tables. I also tried making the GroupSelect function stand alone in its own file and calling it with a require_once. But had issues with this concept too.

I’m still trying to learn the best way of doing things in PHP, but I really don’t have anyone local I can ask. If you have the time, please take a look and tell me where I’ve gone horribly wrong with this code.

YES, I know the GroupSelect() function is currently commented out. This is the only way to give you the overall feel of the page. Experience tells me some of you will criticize the way I wrote up this question, other because I forgot some detail, please be kind, I’ll try and answer all questions.

And thank you in advance!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Keith D Kaiser
  • 1,016
  • 2
  • 14
  • 31

1 Answers1

3

$db_found is undefined in your case, you can create a new connection inside of that function:

  function GroupSelect() {
  $db_found = new PDO('mysql:host=localhost;dbname=test', 'userName','Password'
                      array(PDO::ATTR_EMULATE_PREPARES => false, 
                      PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

       //.......
   }

or pass it as a parameter:

function GroupSelect($db_found) {....}

and also you can also make it a property and access it $this->db_found

meda
  • 45,103
  • 14
  • 92
  • 122
  • I tried all three ways, primarily this is what I got. Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in /Users/KKAISER/Sites/arhab/hab_launch_list_new.php:12 Stack trace: #0 /Users/KKAISER/Sites/arhab/hab_launch_list_new.php(12): PDO->query('SELECT DISTINCT...') #1 /Users/KKAISER/Sites/arhab/hab_launch_list_new.php(434): GroupSelect() #2 {main} thrown in /Users/KKAISER/Sites/arhab/hab_launch_list_new.php on line12 – Keith D Kaiser May 27 '14 at 16:42
  • @KeithDKaiser you need to adjust the connection string, go through the docs and set it up properly https://php.net/manual/en/pdo.connections.php – meda May 27 '14 at 17:00
  • It's exactly the same connection string I use for the rest of the data being displayed, which works fine. I don't think I understand what I've done wrong. Thanks for your time here meda can you give me an example maybe? – Keith D Kaiser May 27 '14 at 17:04
  • @KeithDKaiser show me how your code looks when it works, you should harcode the values just for the sake of testing. You can create a function to return a new instance of the PDO connection, you have many options here. – meda May 27 '14 at 17:15
  • $db_found = new PDO("mysql:host=$strHostName;dbname=$strDbName;charset=utf8", $strUserName, $strPassword, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); – Keith D Kaiser May 27 '14 at 17:43
  • @KeithDKaiser variables such as `$strDbName` will be undefined in your function, type in the values directly and give it a try. – meda May 27 '14 at 19:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54562/discussion-between-meda-and-keith-d-kaiser). – meda May 27 '14 at 21:25
  • I'm so sorry meda that I wasn't there. Got called away on a family issue, grandson... all is well. Anyway, I'll be around here all day tomorrow (Wednesday) if you have the patience to deal with me, please let me know. I'll keep close eye on this site. Thank you! P.S. I added my require_once with the passwords and all. It didn't work. So I typed all of them in long hand, it still gives me a Fatal Error on line 73's -- Keith, WA0TJT – Keith D Kaiser May 28 '14 at 02:35