cdoyle Posted April 26, 2008 Share Posted April 26, 2008 I've been slowly learning how to write out these pages, and now I'm trying to create something all on my own. I almost have it, but having a small problem with an If statement. Here is what I have, and what I'm trying to do. I have 2 tables players and Jobs players table has these fields Players_Job_ID P_Job_Skill Jobs table Job_ID Req_Job_Skill When a player 'applies' for a job I want it to check to make sure they don't already have a job (this part works) and also makes sure they have enough job skills to apply for the job (this doesn't work) I think the problem is trying to use $jobfetch[Req_Job_Skill], because for a test I tried using 2 other fields that are in my players table as criteria, and the elseif works. Does anyone see what I'm doing wrong? I also have another question, is it good practice to put a exit; or break; at the end of each if? or does it not really matter? Here is the code I'm stuck with. $player = check_user($secret_key, $db); $jobid = $_GET['jobid']; $query = $db->execute("select * from `Jobs` where `Job_ID`=?", array($_GET['jobid'])); while ($jobfetch = $query->fetchrow()) if ($_GET['action'] == "inquire") { include("templates/private_header.php"); echo "<strong>" . $jobfetch[Job_Name] . "\n</strong><br><br>"; echo $jobfetch[Job_Description] . "<p></p>"; echo "<br><strong>Daily Pay:</strong> $" . $jobfetch[Pay] . "<br>"; echo "<strong>Job Skill required for this Job:\n</strong>" . $jobfetch[Req_Job_Skill] . "<br>"; echo "<strong>Computer Skills Required for this Job:\n</strong>" . $jobfetch[Computer_SKill]; echo "<p></p>"; echo "<strong>You will earn:</strong> $" . $jobfetch[Pay] . "\n per day<br>"; echo "<strong>You will also earn:\n</strong>" . $jobfetch[Job_Skill_given] . "\nJob Skills per Day<br>"; echo "<a href='employment.php?action=apply&jobid={$jobfetch['Job_ID']}>Apply</a>\n"; include("templates/private_footer.php"); } if ($_GET['action'] == "apply") { if ($player->Players_Job_ID > 0) { include("templates/private_header.php"); echo "You already have a job!<br><br>"; echo "<a href='employment.php?action=quit&jobid={$jobfetch['Job_ID']}>Quit</a>\n"; include("templates/private_footer.php"); }elseif ($player->Players_P_Job_Skill < $jobfetch[Req_Job_Skill]){ include("templates/private_header.php"); echo "<Strong>Boss:</strong>We've looked over our resume, and don't feel that you are what we are looking for<br>"; echo "<Strong>You:</strong> Bite Me, I didn't want this piece of crap job anyways"; echo "<a href=\"employment.php\">Other Jobs</a>\n"; include("templates/private_footer.php"); }else{ //update Job include("templates/private_header.php"); $query = $db->execute ("UPDATE players set `Players_Job_ID`=? where `id`=?", array($jobid[Job_ID], $player->id)); echo "<strong>Boss:</strong> We've looked over you're resume and feel that you are suitable for this position<br>"; echo "<strong>Boss:</strong>Can you start today?<br>"; echo "<strong>You:</strong> I sure can, you won't be disappointed!<p></p>"; echo "<strong>Boss:</strong> You better not screw up<p></p>"; echo "<a href=\"home.php\">Home</a>"; include("templates/private_footer.php"); } } if ($_GET['action'] == "quit") //quit current job { include("templates/private_header.php"); echo "You have quit your job, you unemployed Loser!<br>"; $query = $db->execute ("UPDATE players SET Players_Job_ID=0 where id=$player->id"); echo "<a href=\"employment.php\">Back to unemployment office</a>"; include("templates/private_footer.php"); } Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/ Share on other sites More sharing options...
jonsjava Posted April 27, 2008 Share Posted April 27, 2008 A cleaned up version of the code (It appears to be ok, so long as you are using numerical values for those DB fields) I changed no code, just made it easier for other coders to parse through. <?php $player = check_user($secret_key, $db); $jobid = $_GET['jobid']; $query = $db->execute("select * from `Jobs` where `Job_ID`=?", array($_GET['jobid'])); while ($jobfetch = $query->fetchrow()) if ($_GET['action'] == "inquire") { include("templates/private_header.php"); echo "<strong>" . $jobfetch['Job_Name'] . "\n</strong><br><br>"; echo $jobfetch['Job_Description'] . "<p></p>"; echo "<br><strong>Daily Pay:</strong> $" . $jobfetch['Pay'] . "<br>"; echo "<strong>Job Skill required for this Job:\n</strong>" . $jobfetch['Req_Job_Skill'] . "<br>"; echo "<strong>Computer Skills Required for this Job:\n</strong>" . $jobfetch['Computer_SKill']; echo "<p></p>"; echo "<strong>You will earn:</strong> $" . $jobfetch['Pay'] . "\n per day<br>"; echo "<strong>You will also earn:\n</strong>" . $jobfetch['Job_Skill_given'] . "\nJob Skills per Day<br>"; echo "<a href='employment.php?action=apply&jobid={$jobfetch['Job_ID']}>Apply</a>\n"; include("templates/private_footer.php"); } if ($_GET['action'] == "apply") { if ($player->Players_Job_ID > 0) { include("templates/private_header.php"); echo "You already have a job!<br><br>"; echo "<a href='employment.php?action=quit&jobid={$jobfetch['Job_ID']}>Quit</a>\n"; include("templates/private_footer.php"); } elseif ($player->Players_P_Job_Skill < $jobfetch['Req_Job_Skill']){ include("templates/private_header.php"); echo "<Strong>Boss:</strong>We've looked over our resume, and don't feel that you are what we are looking for<br>"; echo "<Strong>You:</strong> Bite Me, I didn't want this piece of crap job anyways"; echo "<a href=\"employment.php\">Other Jobs</a>\n"; include("templates/private_footer.php"); } else{ //update Job include("templates/private_header.php"); $query = $db->execute ("UPDATE players set `Players_Job_ID`=? where `id`=?", array($jobid[Job_ID], $player->id)); echo "<strong>Boss:</strong> We've looked over you're resume and feel that you are suitable for this position<br>"; echo "<strong>Boss:</strong>Can you start today?<br>"; echo "<strong>You:</strong> I sure can, you won't be disappointed!<p></p>"; echo "<strong>Boss:</strong> You better not screw up<p></p>"; echo "<a href=\"home.php\">Home</a>"; include("templates/private_footer.php"); } } if ($_GET['action'] == "quit") //quit current job { include("templates/private_header.php"); echo "You have quit your job, you unemployed Loser!<br>"; $query = $db->execute ("UPDATE players SET Players_Job_ID=0 where id=$player->id"); echo "<a href=\"employment.php\">Back to unemployment office</a>"; include("templates/private_footer.php"); } Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528057 Share on other sites More sharing options...
cdoyle Posted April 27, 2008 Author Share Posted April 27, 2008 Thanks, Ya it didn't copy/paste over too well, I should have fixed that. thank you and to answer your question, yes both fields are numerical values int(11). Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528059 Share on other sites More sharing options...
cdoyle Posted April 27, 2008 Author Share Posted April 27, 2008 Still can't seem to get this to work, been trying trying different things. Here is another question, I've been told on another forum the exit; I have on each if is not needed. Is this correct? Because I tried removing them, and the whole page basically doesn't work right. When you click on stuff, it's really slow to load, and when it did load it wasn't right. Things were out of order, etc. So that leads me to think either the exits are needed, or my coding isn't right and the exits are hiding that? Here is my whole page <?php include("lib.php"); define("PAGENAME", "CAC Unemployment Office"); $player = check_user($secret_key, $db); $jobid = $_GET['jobid']; $query = $db->execute("select * from `Jobs` where `Job_ID`=?", array($_GET['jobid'])); while ($jobfetch = $query->fetchrow()) if ($_GET['action'] == "inquire") { include("templates/private_header.php"); echo "<strong>" . $jobfetch['Job_Name'] . "\n</strong><br><br>"; echo $jobfetch['Job_Description'] . "<p></p>"; echo "<br><strong>Daily Pay:</strong> $" . $jobfetch['Pay'] . "<br>"; echo "<strong>Job Skill required for this Job:\n</strong>" . $jobfetch['Req_Job_Skill'] . "<br>"; echo "<strong>Computer Skills Required for this Job:\n</strong>" . $jobfetch['Computer_SKill']; echo "<p></p>"; echo "<strong>You will earn:</strong> $" . $jobfetch['Pay'] . "\n per day<br>"; echo "<strong>You will also earn:\n</strong>" . $jobfetch['Job_Skill_given'] . "\nJob Skills per Day<br>"; echo "<a href='employment.php?action=apply&jobid={$jobfetch['Job_ID']}>Apply</a>\n"; include("templates/private_footer.php"); exit; } if ($_GET['action'] == "apply") { if ($player->Players_Job_ID > 0) { include("templates/private_header.php"); echo "You already have a job!<br><br>"; echo "<a href='employment.php?action=quit&jobid={$jobfetch['Job_ID']}>Quit</a>\n"; include("templates/private_footer.php"); exit; } elseif ($player->Players_P_Job_Skill < $jobfetch['Req_Job_Skill']){ include("templates/private_header.php"); echo "<Strong>Boss:</strong>We've looked over our resume, and don't feel that you are what we are looking for<br>"; echo "<Strong>You:</strong> Bite Me, I didn't want this piece of crap job anyways"; echo "<a href=\"employment.php\">Other Jobs</a>\n"; include("templates/private_footer.php"); exit; } else{ //update Job include("templates/private_header.php"); $query = $db->execute ("UPDATE players set `Players_Job_ID`=? where `id`=?", array($jobid[Job_ID], $player->id)); echo "<strong>Boss:</strong> We've looked over you're resume and feel that you are suitable for this position<br>"; echo "<strong>Boss:</strong>Can you start today?<br>"; echo "<strong>You:</strong> I sure can, you won't be disappointed!<p></p>"; echo "<strong>Boss:</strong> You better not screw up<p></p>"; echo "<a href=\"home.php\">Home</a>"; include("templates/private_footer.php"); exit; } } if ($_GET['action'] == "quit") //quit current job { include("templates/private_header.php"); echo "You have quit your job, you unemployed Loser!<br>"; $query = $db->execute ("UPDATE players SET Players_Job_ID=0 where id=$player->id"); echo "<a href=\"employment.php\">Back to unemployment office</a>"; include("templates/private_footer.php"); exit; } include("templates/private_header.php"); echo "<h1>CAC Unemployment Center</h1><br>"; echo "Where even losers like you can get a job!<br>"; echo "Current Jobs Available within CAC<br>"; echo "<table width=\"100%\" border=\"1\">\n"; echo "<tr>"; echo "<td width=\"199\" class=\"cellheader\">Job</td>"; echo "<td width=\"217\" class=\"cellheader\"> Description</td>"; echo "<td class=\"cellheader\">Pay</td>"; echo "<td class=\"cellheader\">Contact</td>"; echo "</tr>"; $queryjob = $db->execute("SELECT * FROM Jobs"); while ($getjob1 = $queryjob->fetchrow()) { echo "<tr>"; echo "<td>"; echo "$getjob1[Job_Name]"; echo "</td>\n"; echo "<td>"; echo "$getjob1[Job_Description]"; echo "</td>\n"; echo "<td>\n"; echo "$$getjob1[Pay]"; echo "</td>"; echo "<td>\n"; echo "<a href='employment.php?action=inquire&jobid={$getjob1['Job_ID']}'>Inquire</a>\n"; echo "<td>\n"; echo "</tr>\n"; } echo "</table>\n"; ?> <?php include("templates/private_footer.php"); ?> Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528124 Share on other sites More sharing options...
cdoyle Posted April 27, 2008 Author Share Posted April 27, 2008 anyone have any suggestions? Or any other info I can provide to help troubleshoot? Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528255 Share on other sites More sharing options...
haku Posted April 27, 2008 Share Posted April 27, 2008 When I'm troubleshooting, I will often throw in this code into each of the if/else statements one at a time: die("here"); I do this until 'here' is outputted to my screen, and nothing else. THhis tell me which if/else statement the script is entering in to. After I have found out which one it is entering in to, I work backwards from there and figure out why it is entering that if/else statement and not the one I want. Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528277 Share on other sites More sharing options...
cdoyle Posted April 27, 2008 Author Share Posted April 27, 2008 When I'm troubleshooting, I will often throw in this code into each of the if/else statements one at a time: die("here"); I do this until 'here' is outputted to my screen, and nothing else. THhis tell me which if/else statement the script is entering in to. After I have found out which one it is entering in to, I work backwards from there and figure out why it is entering that if/else statement and not the one I want. I just tried this, and it never outputted 'here' on the screen. I think the IF statement is OK, it's just something about the $jobfetch[Req_Job_Skill] that it doesn't like. I don't think it's getting any data As a test, I replaced ($player->Players_P_Job_Skill < $jobfetch['Req_Job_Skill']){ with ($player->HP < $player->maxHP{ and it starts working, since the players HP will never be > MaxHP. But I don't know why $jobfetch['Req_Job_Skill']) doesn't work here, it worked in the previous IF where I echo it. Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528292 Share on other sites More sharing options...
haku Posted April 27, 2008 Share Posted April 27, 2008 if it didn't print 'here' to the screen, then it didn't enter the spot where you put that code. Now you have to figure out why. Link to comment https://forums.phpfreaks.com/topic/103085-help-with-this-if/#findComment-528581 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.