Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/158394-solved-creating-a-function/
Share on other sites

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']"

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?

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 ";

 

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?

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.  ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.