cdecker15 Posted September 29, 2015 Share Posted September 29, 2015 The below code works and all, but I would like to figure out how to get rid of the global variable. I tried without declaring it as a global variable and it did not work. Any other way to do that? Also, how would I go about returning two variables(right now I am returning $id), how can I return both $id and $first_name from the database. <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/reset.css"> <link rel="stylesheet" type="text/css" href="css/structure.css"> <script type="text/javascript"> window.onload = function () { var inputs = document.getElementsByTagName('input'); for (i = 0; i < inputs.length; i++) { inputs[i].onkeyup = function () { if (this.value.match(/^([0-9][0-9][0-9][0-9][0-9])$/)) { document.forms["punch"].submit(); } }; } }; $("form").bind("keypress", function (e) { if (e.keyCode == 13) { $("#btnSearch").attr('value'); //add more buttons here return false; } }); </script> </head> <?php $id = 0; function getID($staffid){ $servername = "localhost"; $username = "root"; $password = "x"; $dbname = "hr"; $conn1 = new mysqli($servername, $username, $password, $dbname); if ($conn1->connect_error) { die("Connection failed: " . $conn1->connect_error); } $sql = "SELECT id, first_name, employee_id FROM Employees WHERE employee_id='".$staffid."'"; $result = $conn1->query($sql); global $id; if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $id = $row["id"]; } return $id; } else { return false; } $conn1->close(); } if (isset($_POST['staffid'])){ $staffid = $_REQUEST['staffid']; if(getID($staffid)) { $status = "<font color='green'>Punched</font>"; echo $id; } else { $status = "<font color='red'>Invalid ID, try again</font>"; } } ?> <body> <form class="box login" action="" id="punch" method="post"> <fieldset class="boxBody"> <label>Staff ID</label> <p><input id="staffid" type="text" autofocus="autofocus" name="staffid"></p> </fieldset> <fieldset class="boxBody2"> <center><img src="clock.png" /><br><?php if ($status) {echo $status;} else {echo 'Please enter your Staff ID';}?></center> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/ Share on other sites More sharing options...
hansford Posted September 29, 2015 Share Posted September 29, 2015 (edited) Pass the id variable to the function. You can also return multiple variables using an array. function getID($staffid, $id){ return array($id,$first_name); Edited September 29, 2015 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521830 Share on other sites More sharing options...
cdecker15 Posted September 29, 2015 Author Share Posted September 29, 2015 I changed it to the following, and it is now returning 0, not 1 like it should. Any ideas? <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="css/reset.css"> <link rel="stylesheet" type="text/css" href="css/structure.css"> <script type="text/javascript"> window.onload = function () { var inputs = document.getElementsByTagName('input'); for (i = 0; i < inputs.length; i++) { inputs[i].onkeyup = function () { if (this.value.match(/^([0-9][0-9][0-9][0-9][0-9])$/)) { document.forms["punch"].submit(); } }; } }; $("form").bind("keypress", function (e) { if (e.keyCode == 13) { $("#btnSearch").attr('value'); //add more buttons here return false; } }); </script> </head> <?php $id = 0; function getID($staffid, $id, $first_name){ $servername = "localhost"; $username = "root"; $password = "x"; $dbname = "hr"; $conn1 = new mysqli($servername, $username, $password, $dbname); if ($conn1->connect_error) { die("Connection failed: " . $conn1->connect_error); } $sql = "SELECT id, first_name, employee_id FROM Employees WHERE employee_id='".$staffid."'"; $result = $conn1->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $id = $row["id"]; $first_name = $row["first_name"]; } return array($id, $first_name); } else { return false; } $conn1->close(); } if (isset($_POST['staffid'])){ $staffid = $_REQUEST['staffid']; if(getID($staffid)) { $status = "<font color='green'>Punched</font>"; echo $id; echo $first_name; } else { $status = "<font color='red'>Invalid ID, try again</font>"; } } ?> <body> <form class="box login" action="" id="punch" method="post"> <fieldset class="boxBody"> <label>Staff ID</label> <p><input id="staffid" type="text" autofocus="autofocus" name="staffid"></p> </fieldset> <fieldset class="boxBody2"> <center><img src="clock.png" /><br><?php if ($status) {echo $status;} else {echo 'Please enter your Staff ID';}?></center> </fieldset> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521831 Share on other sites More sharing options...
gizmola Posted September 29, 2015 Share Posted September 29, 2015 This line: if(getID($staffid)) { --- depends on getID returning a boolean equivalent value. You have changed the function so that it no longer works that way. You will need to rethink the way that code is written if you want to change the way getID() works. Returning false or an array is no longer compatible with returning false OR some positive integer that will evaluate to true. Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521834 Share on other sites More sharing options...
hansford Posted September 29, 2015 Share Posted September 29, 2015 I don't understand why you need the $id variable as you don't seem to use it. The only id variable I see you use is the $staffid $arg = getID($staffid); if(is_array($arg)) { $status = "<font color='green'>Punched</font>"; echo $arg[0]; //$id echo $arg[1]; //$first_name } else { $status = "<font color='red'>Invalid ID, try again</font>"; } Change the arguments in this function to this: function getID($staffid){ Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521835 Share on other sites More sharing options...
cdecker15 Posted September 29, 2015 Author Share Posted September 29, 2015 I plan on using the ID later in the file, for a different MySQL statement. So what should I be using for this? Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521836 Share on other sites More sharing options...
hansford Posted September 29, 2015 Share Posted September 29, 2015 Well, if it's not in use now, just place a default value for that variable. The default will get over ridden later on when you pass the variable in. function getID($staffid, $id=false) Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521839 Share on other sites More sharing options...
gizmola Posted September 29, 2015 Share Posted September 29, 2015 I plan on using the ID later in the file, for a different MySQL statement. So what should I be using for this? This is how you create spaghetti. functions should be discrete and do one thing. Planning to "add a different MySQL statement later" isn't the right solution. You should write a different function for that. It's also clear that getID() is a bad name for this function. It really should be named something like: function getEmployee($id) { } Quote Link to comment https://forums.phpfreaks.com/topic/298356-basic-php-questionhelp/#findComment-1521840 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.