I have a situation where I need to compare 3 different select option fields of the same table. I compared 3 fields, 2 fields and 1 field. The corresponding results are displayed, but in one field condition (category) result are not displayed.
<?php
include'connect.php';
if(isset($_POST['submit']))
{
$qn=$_POST['location'];//LOCATION
$qn1=$_POST['category'];//CATEGORY
$qn2=$_POST['salary'];//SALARY
if(isset($_POST['location']))
{
$q2=mysql_query("SELECT * FROM job_posting where location='$qn'");
while($quew=mysql_fetch_array($q2))
{
echo $ans=$quew['title'];
}
}
else
{
$qir=mysql_query("SELECT * FROM job_posting where category='$qn1'");
while($quew=mysql_fetch_array($qir))
{
echo $gn=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']))
{
$q3=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1'");
while($quew=mysql_fetch_array($q3))
{
echo $ans2=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) && isset($_POST['salary']))
{
$q4=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q4))
{
echo $ans3=$quew['title'];
}
}
if(isset($_POST['location']) || isset($_POST['category']) && isset($_POST['salary']))
{
$q5=mysql_query("SELECT * FROM job_posting where location='$qn' || category='$qn1' && minsalary='$qn2'");
while($quew=mysql_fetch_array($q5))
{
echo $ans4=$quew['title'];
}
}
if(isset($_POST['location']) && isset($_POST['category']) || isset($_POST['salary']))
{
$q7=mysql_query("SELECT * FROM job_posting where location='$qn' && category='$qn1' || minsalary='$qn2'");
while($quew=mysql_fetch_array($q7))
{
echo $ans5=$quew['title'];
}
}
}
?>
For a start, I can think of three things
Database Query => check if request the "category"
Database Type => utf8-bin, utf8-general-ci etc.. because is very important how are saved data.
Server file "php.ini" => check "default_charset" and "mbstring..." params, is important to manage string type data.
If you need more help only need send we more information
Try this:
if (!empty($_POST['location'])) {
$lq = "&& location='$_POST['location']'";
}
if (!empty($_POST['category'])) {
$cq = "&& category='$_POST[category]'";
}
if (!empty($_POST['salary'])) {
$sq = "&& minsalary='$_POST[salary]'";
}
$qr = mysql_query("SELECT * FROM job_posting WHERE 1 $lq $cq $sq");
while ($rs = mysql_fetch_array($qr)) {
// do whatever you want
}
Related
I have two arrays one has list of all majors and other has list of majors of the selected project
, the data is fetched form the database. I want to loop through the list of majors array and if its includes any of the project majors, i want to display it as selected in select drop down.
i have tried using nested while loop for iterating through both array, it but only displays one major instead of two
$majorList = $majorObject->getICTProjectMajor(); // row[0]
$assignedMajor = $projectMajorObject->getProjectMajor(28); //row[0]
if ($assignedMajor != false) {
while ($assignedMajorRow = mysqli_fetch_array($assignedMajor)) {
while ($row = mysqli_fetch_array($majorList)) {
echo "<option value='$row[0]'";
foreach ($assignedMajorRow as $majorID) {
if ($majorID == $row[0]) {
echo " selected";
}
echo ">$row[1]</option>'";
}
echo "</select>";
}
echo "</select>";
}
}
functions
public function getProjectMajor($projectID)
{
if ($this->dbc != NULL) {
$selectQuery = "SELECT Major.majorID FROM ProjectMajor JOIN Major on Major.majorID= ProjectMajor.majorID where projectID=$projectID";
$selectResult = mysqli_query($this->dbc, $selectQuery);
if ($selectResult != FALSE) {
return $selectResult;
} else {
return false;
}
} else {
return false;
}
}
public function getICTProjectMajor()
{
if ($this->dbc != NULL) {
$selectQuery = "SELECT * FROM Major where Major.programmeID=4 ";
$selectResult = mysqli_query($this->dbc, $selectQuery);
if ($selectResult != FALSE) {
return $selectResult;
} else {
return false;
}
} else {
return false;
}
}
The select list currently only shows one major instead of two
I have a script like this:
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1){
$something = $db->prepare('SELECT cookie FROM users WHERE id = ?');
$something->execute(array($_SESSION["Id"]));
$num_row = $something->fetch();
$_SESSION["cookie"] = $num_row['cookie'];
if ( $_SESSION["cookie"] != $_COOKIE['login']) ){
// jump to following else statement (outer else statement)
}
} else {
/* here - this block should be execute when
- That inner if-statement is true
OR
- That outer if-statement is false
*/
}
As you see, I need to execute if-statement and if inner if-statement is true then execute else-statement. How can I do that?
I think that is a algorithm problem and not php. Anyway, there are several ways to do. For example:
$innerCondition = false;
$outerCondition = false;
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1) {
$outerCondition = true;
// ...
if ($_SESSION["cookie"] != $_COOKIE['login']) {
$innerCondition = true;
// ...
}
}
if ($innerCondition || !$outerCondition) {
// ...
}
Try something like this:
$doElse = true;
if(isset($_SESSION["LoginValidation"]) && $_SESSION["LoginValidation"] == 1){
$doElse = false;
$something = $db->prepare('SELECT cookie FROM users WHERE id = ?');
$something->execute(array($_SESSION["Id"]));
$num_row = $something->fetch();
$_SESSION["cookie"] = $num_row['cookie'];
if ( $_SESSION["cookie"] != $_COOKIE['login']) ){
$doElse = true;
}
else {
//rest of the logic
}
}
if ($doElse) {
// here - this block should be execute when inner if-statement is true
}
I wasn't too sure how to title this question - Here's a snippet of what I'm doing:
<?php
if ($result_rows >= 1 && $membership = 'active') {
if ($when_next_allowed > $today_date) {
$output = 'You cannot renew your membership for another <b>' . $days_left . 'days</b>.';
}
/*
What if the membership is set to active, but it's been over a year since they
activated it? We don't have any server-side functions for determining such
at the time.
*/
else {
/* do database stuff to change the database entry to inactive */
/* skip to elseif below */
}
}
elseif (2 == 2) {
/* create new database entry for user's membership */
}
?>
If the first nested argument is false, it should move onto else which should continue from there and 'escape' the 'parent' if and move onto elseif. Other wise, if the first nested argument is true, then it should stay put.
Is that even a possible occurrence? The only thing I could think of was to add multiple continue; commands. That, of course, threw an error.
One other idea I had was setting a variable to equal continue; within the else, then set that right before the end of the parent if:
if (1 == 1) {
...
else {
$escape = 'continue;';
}
/* $escape here */
}
But I've never heard of, nor do I know of any method of using variables in a 'raw' form like that. Of course I've done research on it, though I've yet to find out how. I'm not sure if that's common knowledge or anything - But I've never heard of, or considered such a thing until now.
Solution? This is something I always thought about, though I never knew I'd have to use it.
Cleanest I could come up with:
$run = false;
if (1 == 1) {
$run = true;
if (1 == 2) {
/* Do something */
} else {
$run = false;
/* Do something else */
}
}
if (!$run && 2 == 2) {
}
Alternatively, you could use a goto between [Do something else] and the 2nd if block, but it'll be messy either way.
if (1 == 1) {
if (1 == 2) {
/* Do something */
} else {
/* Do something else */
goto 1
}
} else if (!$run && 2 == 2) {
1:
}
If I understand the problem correctly, then you could just do something like this:
if (1==1 && 1==2) {
/* ... */
}
elseif (2==2) {
$success = 'Success';
}
Obviously, I don't need to point out that 1==1 && 1==2 is completely illogical and is just used as an example of two boolean statements.
Update based on update to question:
Unless there are additional steps that you are omitting, this replicates your logic. Hard to know if this really solves your problem, because I don't know what 2==2 represents, or what other steps you might need to perform based on what other conditions.
if (($result_rows >= 1 && $membership == 'active') &&
($when_next_allowed > $today_date)) {
$output = 'You cannot renew your membership for another <b>' . $days_left . 'days</b>.';
}
elseif (2 == 2) {
/* create new database entry for user's membership */
}
This should do what you want to do.
If you have a variable to false and switch it to true if you go into the else you want, you just have to test the value of this variable right after to go into elseif you wanted to go in.
<?php
$test = false;
if (1 == 1) {
if (1 == 2) {
/* ... */
}
else {
/* Skip to elseif below */
$test = true;
}
}
if ($test == true) {
$success = 'Success';
}
echo $success;
?>
Not an easy question as it's really hard to understand what you're trying to achieve but I think this is the solution you're looking for.
<?php
$success = False;
if (1 == 1) {
if (1 == 2) {
/* ... */
} else {
$success = True;
/* True case code can go here */
}
}
echo $success;
?>
pseudo code is your friend.
Alternatively;
<?php
$success = False;
if (1 == 1) {
if (1 == 2) {
/* ... */
} else {
$success = True;
}
}
if $success == True {
/* ... */
}
echo $success;
?>
<?php
$continue = false;
if (1 == 1) {
if (1 == 2) {
/* ... */
}
else {
$continue = true;
}
}
if ($continue==true) {
$success = 'Success';
}
echo $success;
?>
hi i have several columns in my table if any of the column is empty it should be count as 1...at the same time if 2 or more columns empty in same row...it should not count as 2...
help me the mysql query.....
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
}
}
else
{
echo "";
}
?>
if $a or $b or $c or $d or $e or $f is empty...it should count as 1...per row only once..not to count as 2 for the same row
Try this simple code
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
$empty_record = 0;
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
$a=$row['ch1'];
$b=$row['ch2'];
$c=$row['ch3'];
$d=$row['ch4'];
$e=$row['ans'];
$f=$row['ques'];
if($a=='' || $b=='' || $c=='' || $d=='' || $e=='' || $f=='')
{
$empty_record++;
}
}
}
else
{
echo "";
}
echo $empty_record;
?>
I would try something like this:
<?php
include("connect.php");
$unit=$_GET['unit'];
$chapter=$_GET['chapter'];
//$dept=$_GET['dept'];
$result=mysql_query("select * from `$unit` where stopic='$chapter'");
if(mysql_num_rows($result)>0)
{
$numOfEmpty = 0;
while($row=mysql_fetch_row($result))
{
for($i = 0;$i<count($result);$i++) {
if ($result[$i] == "") {
$numOfEmpty++;
break;
}
}
}
echo $numOfEmpty;
}
else
{
echo "";
}
?>
Please let me know however, if the code works. :)
I don't really understand your problem.
Maybe this will help - you can use the isempty() function, which will return true if your variable is empty.
Make a variable named empty and give it value 0. Then on your for loop add this:
if(isempty($a)||isempty($b)||isempty($c)||isempty($d)||isempty($e)||isempty($f))
$empty++;
I have a select box with an option for All, and then a list of users.
What I'm struggling with is creating something like this. I have most of it except trying to query the database to for it to check if the variable is in the database.
if ($variable == 'All') { code here }
else if ($variable != 'ALL' != *[result in database]*) { code here }
else { code here }
I have most of it except trying to query the database to for it to check if the variable is in the database.
Any suggestions how I can encorporate a query of a mySQL database in my if statement.
Thanks
if ($variable == 'All') {
... do something ...
} else {
$sql = "SELECT ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
if ($row['somefield'] == 'whatever') {
... do something else ...
} else {
... do something even "elser" ...
}
}
You can't use operators like that.
Replace:
else if ($variable != 'ALL' != *[result in database]*) { code here }
With:
else if ($variable != 'ALL' AND $variable != *[result in database]*) { code here }