noj75 Posted May 16, 2009 Share Posted May 16, 2009 Hi Guys, I am getting there slowly with my PHP experience but have always had trouble understanding functions. I am trying to include a functions.php page with the following function in it: function selectClients() { $qperm_qry = "SELECT * FROM $clients "; $qperm_res = mysql_query($qperm_qry); while($qsamerow = mysql_fetch_array($qperm_res)) { $clients = '<option value="includes/index.php?id='.$qsamerow['id'].'">'.$qsamerow['name'].'</option>'; $qsamerow++; } return $clients; } I want this to return a list of option elements containing a clints id and name. In the main page I call the fuction like so: selectClients(); It doesnt show what I want it to. Can anyone please help me? Many thanks! Quote Link to comment Share on other sites More sharing options...
RussellReal Posted May 16, 2009 Share Posted May 16, 2009 u dun need this for your particular loop $qsamerow++; and, instead of SETTING $clients every time, try appending the data to the end of the list, try this $clients .= instead of $clients = also, your table $clients, it is set NOWHERE in the function, functions have their own scope, so everything outside of the functions in the global scope, they're inaccessible in the function's scope, so you'd hafta request them or use the $GLOBALS superglobal use like this: "SELECT * FROM $GLOBALS['clients']" Quote Link to comment Share on other sites More sharing options...
farvahar Posted May 16, 2009 Share Posted May 16, 2009 are you using only: selectClients(); or it is: $clients = selectClients(); first one is wrong. also you can debug your function by using print() thing within the function to see if it is actually returning something. Quote Link to comment Share on other sites More sharing options...
noj75 Posted May 16, 2009 Author Share Posted May 16, 2009 Hi fella's, Thanks for the snappy replies. OK here is the info: functions.php <?php include('db_con.php'); function selectClients() { $qperm_qry = "SELECT * FROM $rcr_clients "; $qperm_res = mysql_query($qperm_qry); while($qsamerow = mysql_fetch_array($qperm_res)) { $clients .= '<option value="index.php?horse_id='.$qsamerow['c_id'].'">'.$qsamerow['c_name'].'</option>'; } return $clients; } ?> index.php <option value="">Select Client</option> <?php selectClients(); ?> Obviously I have the include functions.php etc etc the above is just the section I want to get working. Any advances on this? Quote Link to comment Share on other sites More sharing options...
farvahar Posted May 16, 2009 Share Posted May 16, 2009 <option value="">Select Client</option> <?php selectClients(); ?> As Isaid this is wrong you should have something like: print selectClients(); Quote Link to comment Share on other sites More sharing options...
noj75 Posted May 16, 2009 Author Share Posted May 16, 2009 Sorry mate, I wasnt ignoring you. I have added print before as you have suggested but to no avail. It still does not return anything. Can you help further? I appreciate your time Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 16, 2009 Share Posted May 16, 2009 Where is the variable $rcr_clients defined? functions cannot grab variables that are defined outside of it. In order for your function to retrieve a variable from the global scope you'll need to define it as global in the function. function selectClients() { gloabl $rcr_clients; $qperm_qry = "SELECT * FROM $rcr_clients "; Quote Link to comment Share on other sites More sharing options...
noj75 Posted May 16, 2009 Author Share Posted May 16, 2009 Where is the variable $rcr_clients defined? functions cannot grab variables that are defined outside of it. In order for your function to retrieve a variable from the global scope you'll need to define it as global in the function. function selectClients() { gloabl $rcr_clients; $qperm_qry = "SELECT * FROM $rcr_clients "; The variable $rcr_clients is defined in the included database connection file. Do I need to connect to the database within the function then? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 16, 2009 Share Posted May 16, 2009 No change the first three lines of your function to what I posted in my post above, Quote Link to comment Share on other sites More sharing options...
noj75 Posted May 16, 2009 Author Share Posted May 16, 2009 No change the first three lines of your function to what I posted in my post above, Sorry wildteen, didnt quite understand you the first time Added global $rcr_clients to the function and it is now working a treat Thank you very much for your time and help in solving this problem for me. Extremely appreciated. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 16, 2009 Share Posted May 16, 2009 No problem. to help you understand further you should have a read of variable scope from the manual. Quote Link to comment Share on other sites More sharing options...
noj75 Posted May 16, 2009 Author Share Posted May 16, 2009 Will have a good dig into that right now. Thanks again, thats just put to end 3 hours of pulling my hair out. Kindest regards NOJ75 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.