Jump to content

[SOLVED] Php If else


leest

Recommended Posts

Ok, I have simplified my if else/ else if statement, however when i add an elseif block the code stops working.  No errors are produced just the page stops displaying any contents at the point where the if else file is called from another page.

 

Any guidance would be appreciated:

 

Thanks

 


if ($num1 > 0)
{ echo "test 1";}

elseif ($num2 <1) 
{ echo "test 2";}

else {echo "Database is empty"

 

 

 

 

 

 

Link to comment
Share on other sites

Ok, I have simplified my if else/ else if statement, however when i add an elseif block the code stops working.  No errors are produced just the page stops displaying any contents at the point where the if else file is called from another page.

 

Any guidance would be appreciated:

 

Thanks

 


if ($num1 > 0)
{ echo "test 1";}

elseif ($num2 <1) 
{ echo "test 2";}

else {echo "Database is empty"

 

It's almost perfect, you just need to use a } instead of a ) at the very end.

 

Also, if no error was displayed, then you have error reporting off.

Link to comment
Share on other sites

OK, that was a bit short lived,

 

what i have just discovered is that whilst it works fine for the if and else if parts, if both num 1 & num 2 return a value = 0 then i get the same effects as before.

 

As for the error reporting i think it is on as i had an error earlier telling me when i had made a mistake with my database connection script, how can i check this?

 

thanks

Link to comment
Share on other sites

If your code is:

 

<?php
  if ($num1 > 0) {
    echo "test 1";
  } elseif ($num2 < 1) {
    echo "test 2";
  } else {
    echo "Database is empty";
  }
?>

 

Then something will ALWAYS be echoed. Since this is just a snippet, the problem may be else where. What is the rest of the code for the page?

Link to comment
Share on other sites

thanks for the reply,

 

The whole page is pasted below, the two include pages refer to the database queries that generate num1 and num 2 and they are the parts that are working fine, well i think they are if num 1 = 1 then it displays test1

 

if num2 = 1 it displays test 2, the problem seems to be when num 2 = 0, though the v2 page that is included is just a page that runs one query to get a value from a database and then uses that value to run a second query and generate the result for num 2.

 


include 'connections/db1.php';
include 'v1.php';
include 'v2.php';

session_start();
$username = $_SESSION['username'];
$num1 = $_SESSION['num1'];
$num2 = $_SESSION['num2'];


if ($num1 > 0)
{ echo "test 1";}

elseif ($num2 <1) 
{ echo "test 2";}

else {echo "Database is empty"

 

 

Link to comment
Share on other sites

Adjust your script like so:

 

<?php
  //Turn on error reporting
  ini_set('display_errors', '1');
  error_reportIng(E_ALL);

  //Start Session
  session_start();

  //Inlcudes
  include 'connections/db1.php';
  include 'v1.php';
  include 'v2.php';

  $username = $_SESSION['username'];
....

 

Link to comment
Share on other sites

OK, thanks for that, now i get the following message:

 

"Notice: A session had already been started - ignoring session_start() in /home/sites/sbg.com/public_html/search/database_check.php on line 7"

 

the only place i can see a session is on the page that calls for the if else wihich is contained in it's own page, if that makes sense,

 

 

Link to comment
Share on other sites

Try this out and see what happens:

 

<?php
  //Turn on error reporting
  ini_set('display_errors', '1');
  error_reportIng(E_ALL);

  //Start Session
  session_start();

  //Inlcudes
echo "includeing db1.php...';
  require_once('connections/db1.php');
echo "done<br>includeing v1.php...";
  require_once('v1.php');
echo "done<br>includeing v2.php...";
  require_once('v2.php');
echo 'done<br>';

  $username = $_SESSION['username'];
echo "username: {$username}<br>";
  $num1 = $_SESSION['num1'];
echo "num1: {$num1}<br>";
  $num2 = $_SESSION['num2'];
echo "num2: {$num2}<br>";

  if ($num1 > 0) {
    echo "test 1";
  } elseif ($num2 <1) {
    echo "test 2";
  } else {
    echo "Database is empty";
  }
?>

Link to comment
Share on other sites

ok, I think must be something to do with the sessions, first of i got the following message:

 

"Notice: A session had already been started - ignoring session_start() in /home/sites/harrier-recruitment.com/public_html/search/database_check.php on line 7

done

includeing v1.php... "

 

Then i removed the start session on line seven

 

and this is all that was returned:

 

done

includeing v1.php...

 

 

 

I think i will have to go back to the drawing board and start again as i may have made it to difficult as the current structure is as:

 

 

Page 1 -  calls the Include (if else )

 

the if else page then calls (v1.php + v2.php)

 

and some where along the lines i think there must be a problem with the sessions as they are all registered on page one and then passed through each page, if that makes sense

Link to comment
Share on other sites

ok, the problem only occurs when num 1 and num 2 = 0, as long as num 1 or either num 2 = 1 then the script works perfectly, below are the returns for num 1 - 1

 

done

includeing v1.php... done

includeing v2.php... done

username: leest

num1: 1

num2: 1

test 1

 

when i choose an option to lead me to num2 = 0 but num 1 = 1 i get the following

 

done

includeing v1.php... done

includeing v2.php... done

username: leest

num1: 1

num2: 1

test 1

 

 

so the problem is my way of thinking, as it only fails when num 1 and num 2 both = 0, so i need to add more options i think

 

 

 

done

includeing v1.php... done

includeing v2.php... done

username: leest

num1: 1

num2: 1

test 1

 

 

Link to comment
Share on other sites

This is the script from v1, it queries the candidates database to see if they are already in the database with that company, then gets their id if they are and then sees if they have applied for the job to which they are applying, if they have num 1 = will equal 1:

 


include 'connections/db1.php';

//***** Gets Candidate ID from candidate table  *************//
$username = $_SESSION['username'];
$SID = $_SESSION['SID'];

$query = "SELECT * FROM candidate WHERE username ='$username' AND site_id ='$SID'"; 	 
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());


$candidate_id = $row['candidate_id'];
$_SESSION['candidate_id']="$candidate_id";

$JOID = $_SESSION['JOID'];
$candidate_id = $_SESSION['candidate_id'];

$query = "SELECT * FROM candidate_joborder WHERE candidate_id ='$candidate_id' AND joborder_id ='$JOID'";
$result = mysql_query($query)or die(mysql_error());
$num1 = mysql_num_rows($result);

$_SESSION['num1']="$num1";

Link to comment
Share on other sites

The code for v2 is below, having worked out that they haven't applied it then tries to establish if they have already registered with the company advertising the job.

 

include 'connections/db1.php';

//***** Gets Candidate ID from candidate table  *************//
$username = $_SESSION['username'];
$SID = $_SESSION['SID'];

$query = "SELECT * FROM candidate WHERE username ='$username' AND site_id ='$SID'"; 	 
$result = mysql_query($query) or die(mysql_error());
$num2 = mysql_num_rows($result);

$_SESSION['num2']="$num2";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.