getting introducer code based on the employee code - php

I want to get the introducer5 code for the employee code from that introducer5 code need to get the introducer4 code who introduce introducer4 like wise i want to get all their codes.
I have 7 levels of employee: 1) xxxx-7000
2) yyyy-6000
3)....
7) eeee -1000
<?php
if(isset($_POST['submit'])){
$introducer_code=$_POST['intro_code'];
function getOneLevel($intro_code)
{
$get_intro_code="SELECT * FROM `emp_details` WHERE `emp_code` = '".$intro_code."' ";
// echo $get_intro_code."</br>";
$exe_code=mysql_query($get_intro_code);
//$introducer_code=array();
$rows=mysql_num_rows($exe_code);
// echo $rows;
if($rows>0)
{
$get_role=mysql_fetch_array($exe_code);
$introducer_code= $get_role['intro_code'];
$compare_role1=" SELECT * FROM emp_details WHERE `emp_code` = '".$introducer_code."' ";
// echo $compare_role1."</br>";
$exe_role1=mysql_query($compare_role1);
$get_pos1=mysql_fetch_array($exe_role1);
//echo $get_pos1['emp_role'];
if($row_c=mysql_num_rows($exe_role1) > 0){
$compare_role="SELECT * FROM user_role WHERE `emp_role_id` = '".$get_pos1['emp_role']."' ";
echo $compare_role."</br>";
$exe_role=mysql_query($compare_role);
$get_pos=mysql_fetch_array($exe_role);
$id=$get_pos['id'];
}
}
$res=$id.','.$introducer_code;
echo $res."</br>";
return $res;
}
$compare_role2="SELECT * FROM emp_details WHERE `intro_code` = '".$introducer_code."'";
// echo $compare_role2 ."</br>";
$exe_role2=mysql_query($compare_role2);
if (mysql_num_rows($exe_role2)>0) {
//$n=7;
while ($get_pos=mysql_fetch_array($exe_role2)) {
//echo $get_pos['emp_code'];
// $c=$n-$id;
/* for($i=0;$i<$c;$i++){
echo "hi";*/
getOneLevel($get_pos['emp_code']);
// }
}
}
getOneLevel($introducer_code);
}
?>
<form method="post">
<div class="form-group">
<label>Introducer Code</label>
<input type="text" placeholder="Enter Introducer Code" id="intro_code" required="required" name="intro_code" class="form-control" />
</div>
<button value="Submit" type="submit" name="submit" >Submit</button>
</form>
The above code only returns the immediate introducer4 code for the value entered in the text box.It does not returning the introducer3 code who refers introducer4 and so on...
Please Help!!
Struggling more than 4 days!!

Try this may be useful
function getIntroducer($intro_code)
{
//Write your code to get introudcer and return it's Interducer id only
return $introudcer;
}
$finalInterducer = $intro_code //start
for($i=0;$i<5;$i++){ // If its 7 loop 7 time's
$finalInterducer = getIntroducer($finalInterducer)
}
//After loop you get last finalInterducer

Change the SQL Query, try this:
SELECT ED4.emp_code AS 'introducer4',
ED3.emp_code AS 'introducer3',
ED2.emp_code AS 'introducer2',
ED1.emp_code AS 'introducer1'
FROM emp_details ED1,emp_details ED2,emp_details ED3,emp_details ED4
WHERE ED4.emp_code = '".$introducer_code."'
AND ED3.emp_code = ED4.intro_code
AND ED2.emp_code = ED3.intro_code
AND ED1.emp_code = ED2.intro_code
All the best :)

Related

What's wrong with this calculation?

Consider this markup:
<?php
$drill = 0;
$result = '';
$dynamicD = rand(5,15);
$num01 = $dynamicD-4;
$placeholder = $num01.'+4';
if($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST["drill"])){
$humanErr = "Empty";
}
else {
$drill = $_POST["drill"];
if($drill !== $dynamicD) {
$result = "No";
}
else {
$result = "Yes";
}
};
};
?>
<!doctype html>
<html>
<body>
<p>Correct Answer: <?php echo $dynamicD ?></p>
<p>Does this work? <?php echo "$result";?></p>
<form method="post">
<fieldset>
<label for="drill">Can you solve it?</label>
<input type="text" name="drill" id="drill" maxlengh="2" placeholder="<?php echo $placeholder ?>" required="true" />
</fieldset>
<button type="submit" name="check">Check</button>
</form>
</body>
</html>
Basically it generates "random" number, insert it as a drill, and then let the user submit his answer. The problem is that the answer is always "NO"*.
I tried to separate the condition to this:
if($drill > $dynamicD) { $result = "bigger" }
elseif ($drill < $dynamicD) { $result = "smaller" }
and so on - but can't understand the logic of the $result (sometime bigger, sometime smaller, but i ALWAYS enter $dynamicD...).
What am i doing wrong here???
EDIT:
As the comments points, every time the page submit it generates new numbers. The first time the code execute is the only time that correct answer would be equal to the numbers that display. After the first submit there is a gap.
Note for future readers: The above code extracted and simplified from bigger and much complex system. Not something that anyone would want to just copy-paste.
The solution i choose was to store the dynamically created vars on a session and re-declare if the answer is correct.
Would love to hear about other ways (not client side).

form post action into php var

Im in this page: website.php?page=5 and in this page I have this form:
<form method="POST" action="website.php?page=<?php echo $pagenumber; ?>">
<input type="text" name="goto" />
<input type="submit" name="submit" value="goto" />
</form>
If I write something like 3 into the text field and press the goto button (if isset.... my php code get the number and upload into the $pagenumber var)
But the new page isn't the website.php?page=3 what I want. The new page is website.php?page= (there is no number) .and if I press again then goes it to the right page.
I think in the first press when i do the $pagenumber isn't declared. Only when i press second.
How can I fix it? I must use this way i cant use session, cookie etc.
<?php if(isset($_POST['submit']))
{
$kitkeresek = $_POST['goto'];
$becsuletemw = "SELECT * FROM adatok WHERE nev = '$kitkeresek'";
$becsuletem2w = mysql_query($becsuletemw);
while( $becsuletem3w = mysql_fetch_array($becsuletem2w))
$becsuletemw = $becsuletem3w["becsulet"];
$valllamiw = mysql_query("SELECT becsulet FROM adatok WHERE becsulet > '$becsuletemw' ");
$rowsw = mysql_num_rows( $valllamiw );
$kitkeresekw = $rowsw + 1 ;
$intvizsgalat= $kitkeresekw/10;
if (is_int($intvizsgalat))
{ $pagenumber = $intvizsgalat - 1 ; }
else
{$pagenumber = floor($kitkeresekw/10); } ;
}
?>
When you do this :
$intvizsgalat= $kitkeresekw/10;
$intvizsgalat is float, even if the result is int, because it is the result of a division.
You can try var_dump($intvizsgalat) to confirm
try something like this:
$floatParts = $intvizsgalat - floor(intvizsgalat);
if ($floatParts == 0) {
//
} else {
//
}

select all checkboxes if none are select in php query

I have a script wher users can find exercise from a database, I have checkboxes for the user to find specific exercises the script works fine when a least 1 checkbox is selected from each checkbox group however I would like it that if no checkboxes was selected then it would the results of all checkboxes.
my checkbox form looks like this
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="criteria">
<p><strong>MUSCLE GROUP</strong></p>
<input type="checkbox" name="muscle[]" id="abdominals" value="abdominals"/>Abdominals<br />
<input type="checkbox" name="muscle[]" id="biceps" value="biceps" />Biceps<br />
<input type="checkbox" name="muscle[]" id="calves" value="calves" />Calves<br />
ect...
<br /><p><strong>EQUIPMENT (please select a least one)</strong></p>
<input type="checkbox" name="equipment[]" id="equipment" value="bands"/>Bands<br />
<input type="checkbox" name="equipment[]" id="equipment" value="barbell" />Barbell<br />
<input type="checkbox" name="equipment[]" id="equipment" value="dumbbell" />Dumbbell<br />
ect....
<input type="submit" name="sub" value="Generate Query" />
</form>
and here is my script
<?php
if(isset($_POST['muscle']) && !empty($_POST['muscle'])){
if(isset($_POST['equipment']) && !empty($_POST['equipment'])){
//get the function
include ($_SERVER['DOCUMENT_ROOT'] .'/scripts/functions.php');
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$limit = 14;
$startpoint = ($page * $limit) - $limit;
// Runs mysql_real_escape_string() on every value encountered.
$clean_muscle = array_map('mysql_real_escape_string', $_REQUEST['muscle']);
$clean_equipment = array_map('mysql_real_escape_string', $_REQUEST['equipment']);
// Convert the array into a string.
$muscle = implode("','", $clean_muscle);
$equipment = implode("','", $clean_equipment);
$options = array();
if(array($muscle))
{
$options[] = "muscle IN ('$muscle')";
}
if(array($equipment))
{
$options[] = "equipment IN ('$equipment')";
}
$fullsearch = implode(' AND ', $options);
$statement = "mytable";
if ($fullsearch <> '') {
$statement .= " WHERE " . $fullsearch;
}
if(!$query=mysql_query("SELECT * FROM {$statement} LIMIT {$startpoint} , {$limit}"))
{
echo "Cannot parse query";
}
elseif(mysql_num_rows($query) == 0) {
echo "No records found";
}
else {
echo "";
while($row = mysql_fetch_assoc($query)) {
echo "".$row['name'] ."</br>
".$row['description'] ."";
}
}
echo "<div class=\"new-pagination\">";
echo pagination($statement,$limit,$page);
echo "</div>";
}
}
Im new with php so my code may not be the best. If anyone can help me or point me in the right direction I would be very greatful.
OK - I think I have figured it all out. You were right - the main problem was in your isset and isempty calls. I created some variations on your file, and this shows what is going on. Note - since I don't have some of your "other" functions, I am only showing what is going wrong in the outer parts of your function.
Part 1: validating function
In the following code, I have added two JavaScript functions to the input form; these were loosely based on scripts you can find when you google "JavaScript validation checkbox". The oneBoxSet(groupName) function will look through all document elements; find the ones of type checkBox, see if one of them is checked, and if so, confirms that it belongs to groupName. For now, it returns "true" or "false". The calling function, validateMe(formName), runs your validation. It is called by adding
onclick="validateMe('criteria'); return false;"
to the code of the submit button. This basically says "call this function with this parameter to validate the form". In this case the function "fixes" the data and submits; but you could imagine that it returns "false", in which case the submit action will be canceled.
In the validateMe function we check whether at least one box is checked in each group; if it is not, then a hidden box in the "all[]" group is set accordingly.
I changed the code a little bit - it calls a different script (muscle.php) instead of the <?php echo $_SERVER['PHP_SELF']; ?> you had... obviously the principle is the same.
After this initial code we will look at some stuff I added in muscle.php to confirm what your original problem was.
<html>
<head>
<script type="text/javascript">
// go through all checkboxes; see if at least one with name 'groupName' is set
// if so, return true; otherwise return false
function oneBoxSet(groupName)
{
var c=document.getElementsByTagName('input');
for (var i = 0; i<c.length; i++){
if (c[i].type=='checkbox')
{
if (c[i].checked) {
if (c[i].name == groupName) {
return true; // at least one box in this group is checked
}
}
}
}
return false; // never found a good checkbox
}
function setAllBoxes(groupName, TF)
{
// set the 'checked' property of all inputs in this group to 'TF' (true or false)
var c=document.getElementsByTagName('input');
// alert("setting all boxes for " + groupName + " to " + TF);
for (var i = 0; i<c.length; i++){
if (c[i].type=='checkbox')
{
if (c[i].name == groupName) {
c[i].checked = TF;
}
}
}
return 0;
}
// this function is run when submit is pressed:
function validateMe(formName) {
if (oneBoxSet('muscle[]')) {
document.getElementById("allMuscles").value = "selectedMuscles";
//alert("muscle OK!");
}
else {
document.getElementById("allMuscles").value = "allMuscles";
// and/or insert code that sets all boxes in this group:
setAllBoxes('muscle[]', true);
alert("No muscle group was selected - has been set to ALL");
}
if (oneBoxSet('equipment[]')) {
document.getElementById("allEquipment").value = "selectedEquipment";
//alert("equipment OK!");
}
else {
document.getElementById("allEquipment").value = "allEquipment";
// instead, you could insert code here that sets all boxes in this category to true
setAllBoxes('equipment[]', true);
alert("No equipment was selected - has been set to ALL");
}
// submit the form - function never returns
document.forms[formName].submit();
}
</script>
</head>
<body>
<form action="muscle.php" method="post" name="criteria">
<p><strong>MUSCLE GROUP</strong></p>
<input type="checkbox" name="muscle[]" id="abdominals" value="abdominals"/>Abdominals<br />
<input type="checkbox" name="muscle[]" id="biceps" value="biceps" />Biceps<br />
<input type="checkbox" name="muscle[]" id="calves" value="calves" />Calves<br />
<input type="hidden" name="all[]" id="allMuscles" value="selectedMuscles" />
etc...<br>
<br /><p><strong>EQUIPMENT (please select a least one)</strong></p>
<input type="checkbox" name="equipment[]" id="equipment" value="bands"/>Bands<br />
<input type="checkbox" name="equipment[]" id="equipment" value="barbell" />Barbell<br />
<input type="checkbox" name="equipment[]" id="equipment" value="dumbbell" />Dumbbell<br />
<input type="hidden" name="all[]" id="allEquipment" value="selectedEquipment" />
<br>
<input type="submit" name="sub" value="Generate Query" onclick="validateMe('criteria'); return false;" />
</form>
</body>
</html>
Part 2: what's wrong with the PHP?
Now here is a new start to the php script. It checks the conditions that you were testing at the start of your original script, and demonstrates that you never get past the initial if statements if a check box wasn't set in one of the groups. I suggest that you leave these tests out altogether, and instead get inspiration from the code I wrote to fix any issues. For example, you can test whether equipmentAll or muscleAll were set, and create the appropriate query string accordingly.
<?php
echo 'file was called successfully<br><br>';
if(isset($_POST['muscle'])) {
echo "_POST[muscle] is set<br>";
print_r($_POST[muscle]);
echo "<br>";
if (!empty($_POST['muscle'])) {
echo "_POST[muscle] is not empty!<br>";
}
else {
echo "_POST[muscle] is empty!<br>";
}
}
else {
echo "_POST[muscle] is not set: it is empty!<br>";
}
if(isset($_POST['equipment'])) {
echo "_POST[equipment] is set<br>";
print_r($_POST['equipment']);
echo "<br>";
if (!empty($_POST['equipment'])) {
echo "_POST[equipment] is not empty!<br>";
}
else {
echo "_POST[equipment] is empty!<br>";
}
}
else {
echo "_POST[equipment] is not set: it is empty!<br>";
}
if(isset($_POST['all'])) {
echo "this is what you have to do:<br>";
print_r($_POST['all']);
echo "<br>";
}
// if(isset($_POST['muscle']) && !empty($_POST['muscle'])){
// if(isset($_POST['equipment']) && !empty($_POST['equipment'])){
If you call this with no check boxes selected, you get two dialogs ('not OK!'), then the following output:
file was called successfully
_POST[muscle] is not set: it is empty!
_POST[equipment] is not set: it is empty!
this is what you have to do:
Array ( [0] => allMuscles [1] => allEquipment )
If you select a couple of boxes in the first group, and none in the second:
file was called successfully
_POST[muscle] is set
Array ( [0] => abdominals [1] => calves )
_POST[muscle] is not empty!
_POST[equipment] is not set: it is empty!
this is what you have to do:
Array ( [0] => selectedMuscles [1] => allEquipment )
I do not claim to write beautiful code; but I hope this is functional, and gets you out of the fix you were in. Good luck!

reading from table in a loop

with the code below i let people fill in how many pizza's they want and then pass the chosen pizza ids through the url using get.
<?php // some code that connects to database ?>
<table>
<form method="get" action="oder_process.php">
<?php
while (list($pizzaId, $pizzaName, $pizzaNumber, $pizzaPrice) = mysql_fetch_row($resultaat))
{
echo "<tr>
<td>".$pizzaName."</td>
<td>".$pizzaNumber."</td>
<td>".$pizzaPrice."</td>
<td> <input type='text' name='pizzas[$pizzaId]' value='$qty' size='3' /></td>
</tr>";
}
mysql_close($db);
?>
<input type="submit" value="Bestel!" />
</form>
</table>
Now displaying the pizza id with the amount filled in works just fine. But then I also want to read other columns like pizza name or description from the database. However what i'm doing below doesn't do it. Any ideas?
part of the order_process.php file:
echo "Your order: <br />";
foreach ($_GET['pizzas'] as $pizzaId => $qty)
{
if ($qty > 0)
echo $pizzaId." ".$qty."<br />";
$pizzaSql = mysql_query("SELECT * FROM pizzas where pizza_id = $pizzaId");
while ($row = mysql_fetch_assoc($pizzaSql))
{
echo $row['pizza_name'];
}
}
why foreach()? can't you just ...
if ( is_numeric( $_GET['pizzaid'] ) ) {
$pizzaSql = sprintf("SELECT * FROM orders WHERE pizza_id=%d",$_GET['pizzaid']);
$pizzaQuery = mysql_query($pizzaSql);
$numPizzas = mysql_num_rows( $pizzaQuery );
while ( $order = mysql_fetch_array($pizzaQuery,MYSQL_ASSOC) ) {
if ( $numPizzas > 0 ) {
echo $orderVariables;
} else {
echo 'Error: no order exists for that ID thingy';
}
}
}
i added some minor validation that assumes the pizza/order id is a numeric value. i don't trust anything that comes into a script from the $_GET array without some serious data validation first
can you try chucking in a test to make sure the query worked as expected
$pizzaSql = mysql_query("SELECT * FROM pizzas where pizza_id = $pizzaId");
if (mysql_error()) {
die(mysql_error());
}

How to retain the value of a text box?

This is the content on my view page and i want to replace the value from "0" when the monthfunc() is called...the monthfunc() is given below the view content
<tr>
<td>
<input type="submit" name="monthplus" id="monthplus" onClick="return monthfunc('monthplus');" value=">>>>>>">
<input type="hidden" name="monthnum" id="monthnum" value="1">
<input type="text" name="monthname" id="monthname" value="0" value="<? echo $month;?>" >
<input type="submit" name="monthminus" id="monthminus" onClick="return monthfunc('monthminus');" value="<<<<<<">
</td>
</tr>
My script is
function monthfunc(mnth)
{
if(mnth == 'monthplus')
{
var yn = document.getElementById('monthnum').value;
ynpo = parseInt(yn)+1;
if(ynpo==13)
{
ynpo=1;
}
}
else if(mnth == 'monthminus')
{
var yn = document.getElementById('monthnum').value;
ynpo = parseInt(yn)-1;
if(ynpo==0)
{
ynpo=12;
}
}
if(ynpo ==1)
{
document.getElementById('monthname').value = 'january';
document.getElementById('monthnum').value = ynpo;
return true;
}
else if(ynpo ==2)
{
document.getElementById('monthname').value = 'february';
document.getElementById('monthnum').value = ynpo;
return true;
}
else if(ynpo ==3)
{
document.getElementById('monthname').value = 'March';
document.getElementById('monthnum').value = ynpo;
return true;
}
return false;
}
How can i replace The value with the value like january february etc..
Actually i can change the values but cannot retain the values...
i want to retain the values ,,,How to do that
For the monthname input you have two 'values'. What happens when you remove the first one?
Also, you really need to improve the structure of your function. For example, using the following array would reduce your code significantly:
var month=new Array(12);
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
I'm not sure I exactly understand your question, but by way of an answer:
One of the input elements in your HTML snippet has two value attributes:
value="0"
and
value="<? echo $month;?>"
I would start by removing one of them.

Categories