MSUK1 Posted March 29, 2012 Share Posted March 29, 2012 Hello i have a system where leaders add members, i want to display a table of members WHERE the 'leader' field equals 'myusername' so that leaders only see members they added. when using the WHERE function i get a database empty as the result :'( but when i remove the WHERE function i get all the data returned include("system/include/session.php"); $myUname = $session->username; echo "$myUname"; function displayMembers(){ global $database; $q = "SELECT * " ."FROM ".TBL_MEMBERS." WHERE leader = '$myUname'"; $result = $database->query($q); $num_rows = mysql_numrows($result); if(!$result || ($num_rows < 0)){ echo "Error displaying info"; return; } if($num_rows == 0){ echo "Database table empty"; return; } echo "<table>\n"; echo "<thead> <tr> <th>Full Name</th> <th>Gender</th> <th>DOB</th> <th>Post Code</th> <th>Email Address</th> <th>Edit User</th> </tr> </thead>\n"; for($i=0; $i<$num_rows; $i++){ $Mprefix = mysql_result($result,$i,"prefix"); $Mfname = mysql_result($result,$i,"firstname"); $Mlname = mysql_result($result,$i,"lastname"); $Mgender = mysql_result($result,$i,"gender"); $Mdob = mysql_result($result,$i,"dob"); $Mpostcode = mysql_result($result,$i,"postcode"); $Memail = mysql_result($result,$i,"email"); echo " <tbody> <tr> <td>$Mprefix $Mfname $Mlname</td> <td>$Mgender</td> <td>$Mdob</td> <td>$Mpostcode</td> <td>$Memail</td> <td> <center> <form action=\"system/admin/adminprocess.php\" method=\"POST\"> <input type=\"hidden\" name=\"deluser\" value=\"$uname\"> <input type=\"hidden\" name=\"subdeluser\" value=\"1\"> <input type=\"submit\" class=\"button plain\" value=\"Edit Member\"> </form> </center> </td> </tr> </tbody>\n"; } echo "</table><br>\n"; } all help i am thankful for Quote Link to comment Share on other sites More sharing options...
titan21 Posted March 29, 2012 Share Posted March 29, 2012 We'd probably need to see your data but try outputting the SQL and then run it via phpMyAdmin or something similar and see if you get any results that way Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 i will try this just to shed a bit of light... if i put an actual username into the WHERE lets say 'admin' it works and only displays admin's members if i put the variable into the WHERE my '$myUname' (which as you can see i echo earlier and it correctly echos 'admin' then i get my db empty error Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 sounds like a white space issue, try using trim() on your $myUname also, echo out your $q and post up what it contains. Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 thaanks for your reply what ive done is: $myUname = trim($session->username); to trim the variable but it still returns databse empty i'm also trying to echo $q but nothing seems to be returned i think i have something clearing q after its been queried? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 echo it before it's executed: $q = "SELECT * " ."FROM ".TBL_MEMBERS." WHERE leader = '$myUname'"; die($q); Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 SELECT * FROM members WHERE leader = '' so its not reading the variable :/ but why it doesnt make sense, im echoing the variable above and its reading it Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 there in lies the problem, here's a hint: your $myUname is set in the page code, your $q query is being run in the displayMembers() function... Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 i kind of am making sense of what you are saying but when i try and get the username within the function it returns a blank, which i guess as your right explains why im getting this error. but i then tried to define the username inside the function */ function displayMembers(){ $myUname = $session->username; echo "$myUname"; global $database; $q = "SELECT * " ."FROM ".TBL_MEMBERS." WHERE leader = '$myUname'"; echo "$q"; etc etc but i cant get it to work :/ im not that pro a coder :/ Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 you want to use refferencing - here, some example code to ilustrate how variable refferencing works in functioin calls: <?php function exampleRefference($referenceVariable){ $q = "this refferences -| $referenceVariable |-\n<br>"; return $q; } $r = exampleRefference("direct string input"); echo $r; $variable = "sting input using a variable"; $r = exampleRefference($variable); echo $r; $r = exampleRefference("\$variable"); echo $r; ?> produces the following: this refferences -| direct string input |- this refferences -| sting input using a variable |- this refferences -| $variable |- Does that help any? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2012 Share Posted March 29, 2012 You need to have php's error_reporting set to E_ALL and display_errors set to ON in your master php.ini on your development system so that all the php detected errors will be reported and displayed. You will save a TON of time. You would be getting undefined variable error messages, first for your $myUname variable, then for your $session object when you reference them inside the function. Your problem is because variables inside of functions are local to that function (this so that you can write any code you need inside of a function without needing to keep track of what variables you are using in that code so that they don't interfere with the application you happen to be using the function inside of - once you write large applications with 1000's of variables and hundreds of functions, you will see why this is important.) You need to pass values into your functions as call time parameters. Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 this is making more and more sense, thank you! i did a bit more googling using phrases you have suggested, is it okay, to use the global feature? so global $myUname ? i have made sure myUname is a complete unique variable and is not used anywhere else in my system Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 as a rule of thumb - ignore the fact that global even exists (especialy, and I don't mean this in a nasty put you down kind of way, with your current knowledge regarding variable scope). If there is information you want to get into a function, refference it. If there is information you want to make available after the function has processed it's part, return it. Honestly, I'm not being derogatory about this: as well as other problems, when you get to making your own classes and librarys, rogue global's will seriously screw with your happines. It's a far better thing to learn now not to work with them, than to try and adapt your thinking and your code later on. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2012 Share Posted March 29, 2012 is it okay, to use the global feature NNNNnnnnoooooo...... That just makes more work for you. This is more than just keeping variables straight. It is about writing code that is general purpose and can be easily used/reused in any situation or in different situations within one application. Functions should be 'black boxes'. They optionally accept inputs that they need as call time parameters, perform some useful processing, and return or output the result they were designed to do. If you use call time parameters, you can directly call your function with a literal value, a variable, or another function result - displayMembers('admin'); // a literal value displayMembers($session->username); // the currently logged in user displayMembers($value); // a 'random' variable, such as from looping over a list of usernames displayMembers(someOtherFunction()); // a function that returns a username displayMembers(trim($_POST['username'])); // a built in function that does something you need You don't need to add yet another $myUname variable in your code. That just makes more work and takes more memory (both your's, keeping track of it and all the other ones that are brought into functions using the global keyword, and the computers memory.) Also, there's a reason that built in functions (optionally) take call time parameters, perform some useful processing, and return the result back to the calling code, it's so that you can use them as building blocks in your code. Your user written functions should do the same. Here's an argument that your global $database variable should be a call time parameter to your function. What if you had an application where you needed to call your function at different points for different databases that happen to require different database connections? You already have 100's of lines of code that expect your main program's $database variable to contain the main database connection, but you have a second database connection in $db2. For using global $database to get the db connection into your function, at a point before you call your function to operate on the $db2 database connection, you must assign $database = $db2; What just happened to your main database connection in $database? It got overwritten. Did you remember to copy it to some temporary variable? Did you remember to copy the temporary variable back into $database after you called the function so that the remainder of your code on the page will have the expected database connection in $database? However, if the database connection was passed as a call time parameter into your function, all this goes away. You just call your function with the database connection it needs - displayMembers($database, $session->username); // the main database and the currently logged in user displayMembers($db2, $session->username); // the second database connection and the currently logged in user displayMembers($db3, $session->username); // oops, the boss just added a requirement that this work for a 3rd separate database connection that we cannot change and the currently logged in user The theme throughout this is simplification and writing clean and clear code. Would you rather have one line of code to call your function displayMembers($session->username); or would you like to be typing extra lines of code - $myUname = $session->username; displayMembers(); to accomplish the same thing? Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 29, 2012 Author Share Posted March 29, 2012 really appreicate this guys ok so im trying to use some of your methods but its just causing syntax errors left right center... first tried adding $session->username to pass the username into the function, but that gives me a function not defined error i then tried passing $username from session itself into the function, just by doing function displayMembers($username) { // i noticed in your examples you included ';' after the brackets. when i added this i got syntax errors again ? Forgive my noobyness really trying to get this right thank you Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 29, 2012 Share Posted March 29, 2012 you know, looking at your code, your best solution would be to just delete the funcion deffenition line and it's closing curly brace at the end. None of the content is properly designed to be contained in a function. When making custom functions you really need to trat them as indipendant entities, no interaction with anything that you don't specificaly pass in - so no direct communication with the display. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 29, 2012 Share Posted March 29, 2012 You would need to post the actual code that you need help with. All the statements I posted above were function calls. User written functions consist of the function definition and then actually calling that function at the point where you want to use it. A function definition (i.e. the function keyword is used along with {} to delineate the start and end of the function code) - function displayMembers($someParameterName){ // your code and variables that implement the useful operation you have defined for this function } Calling the function at some point in your code - displayMembers($session->username); // it's a program statement, therefore it is terminated with a ; Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 30, 2012 Author Share Posted March 30, 2012 that makes total sense, i understand further now the idea behind it if i had function name($v1, $v2, $v3) that would correspond to the vaules inputted when i call that function name(a, b, c); ? Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted March 30, 2012 Author Share Posted March 30, 2012 you know, looking at your code, your best solution would be to just delete the funcion deffenition line and it's closing curly brace at the end. None of the content is properly designed to be contained in a function. When making custom functions you really need to trat them as indipendant entities, no interaction with anything that you don't specificaly pass in - so no direct communication with the display. The problem is this makes my code between my body tags a lot more bulky than it needs to be for me? (i hope that makes sense?) i like to set my functions above or outside of the page they may need to be used on and then call upon them when neeeded using only one line of code inside the body of the site? Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 31, 2012 Share Posted March 31, 2012 Then we really need to work on how functions operate. Functions should only interact with the code that calls them, and the code they in turn call from within them selves. they should not, for instance, interface with screen output directly. To get information into a function you push it there at the time you call the function. to get information out of a function you assign a variable to the function call it's self and then feed that variable the information you want to function to give by using the return keyword within the function. On average I would say that at least 80% of the information within a function is self contained. This is a good part of what makes functions so very usefull - you can use logical variable names and declerations without concerning yourself if they already exist somewhere else in the code. This isn't just usefull for your own code, but when applications get large, and you are programming as part of a team, you can each write your own functions without having to worry "Do you think Dave would use that variable name in his function?" Functions are the old men of programing, they have been about since procedural programing was new and trendy, and the word "modular" bore the promise of better days to come. Objects have now taken that title, but just like everything else in life as things move on, the realy potent advances are kept with the new versions and integrated to work. The point behind my seeming digression on this matter is that, because functions aren't born of OOP, they can easily haze things up if your not paying absolout attention when you start to use them. Your function is trying to do more than a function does. Unfortunetly, wihtout seeing and going through all your code and how everything is refferenced within it, I can't think of a way to make it work properly. Quote Link to comment Share on other sites More sharing options...
MSUK1 Posted April 21, 2012 Author Share Posted April 21, 2012 I finally found an efficient way to get this done thank you for all your help and explanations! Fantastic forum this is! 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.