If/Else

I have a community where virtual pilots can log hours flight by flight. I have a downloads page that I want pilots to access but only after logging 10 hours. This code is an attempt to get the users logged hours and then show them the downloads page link if they meet the 10 hour requirement. It fetches the correct hours but gives the same result whether a pilot has 10 hours or 1. It may be that the database is holding varchar instead of int perhaps. Weeks and weeks, I’ve played with this, any ideas? Switch may be better, I’ve tried both. New to coding. Thanks!

[php]<?php

// Connect to the DB
require_once(‘connectvars.php’);

$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

// Get the users total flight hours
mysql_select_db(“flight_db”, $con);
$query = “SELECT SUM(vfrhrs + ifrhrs) AS total FROM logbook WHERE user_id = '” . $_SESSION[‘user_id’] . “’ AND approved=1 GROUP BY user_id”;

$result = mysql_query($query) or die(mysql_error());

// Print out result to make sure it has pulled the session users total hours from database
while($row = mysql_fetch_array($result)){

echo "Current Hours: ". $row[‘total’];
echo “

”;
}

$row[‘total’] = $currenthours;

// Again, print this to make sure this worked
echo("$currenthours");

// Test to see if user has enough hours to proceed to download page link
if ($currenthours > 10) {
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else ($currenthours < 10) {
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
?>
[/php]

try replacing
[php]
$row[‘total’] = $currenthours;

// Again, print this to make sure this worked
echo("$currenthours");

// Test to see if user has enough hours to proceed to download page link
if ($currenthours > 10) {
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else ($currenthours < 10) {
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

with :

[php]
$currenthours = $row[‘total’];

// Again, print this to make sure this worked
echo("$currenthours");

// Test to see if user has enough hours to proceed to download page link
if ($currenthours >= 10)
{
echo “Show The Downloads Page Link or redirect to downloads page”;
}
elseif ($currenthours < 10)
{
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

why elseif?

[php]
$currenthours = $row[‘total’];

// Again, print this to make sure this worked
echo $currenthours;

// Test to see if user has enough hours to proceed to download page link
if ($currenthours >= 10) {
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else {
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

yeah i could just use else but since he already had an statement there ($currenthours < 10)
ijust added if to the else

both urs and mine will have the same result

Hello,

Thanks for your responses. Unfortunately, still not working though. No matter if I check it as one user that has 1 hour and my other user that has 18.61 hours it still gives me the “else” result.

Could it be that the vfrhrs and ifrhrs in the query are stored as varchar and need to be converted to int. I think I read something in the php manual or a post about that. I reposted the code to make sure I didn’t mess it up. I assume I would only need elseif if I was going to be checking for more than one condition is that correct?

Thanks so much for the help!

[php]// Print out result to make sure it has pulled the users total hours from database
while($row = mysql_fetch_array($result)){

echo "Current Hours: ". $row[‘total’];
echo “

”;
}

$currenthours = $row[‘total’];

// Again, print this to make sure this worked
echo ("$currenthours");

// Test to see if user has enough hours to proceed to download page link
if ($currenthours >= 10)
{
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else
{
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}[/php]

im taking look at your code i will post back if i find anything

correct

Thanks again for your your help! It is appreciated.

I dont think you need the loop. loop is when you have an array of value(more than one )since from the query you will only get a single value which is the total from a single user you dont need the loop.

if i am correct read on if im not correct please provide me with your table structure.

replace

[php]
// Print out result to make sure it has pulled the users total hours from database
while($row = mysql_fetch_array($result)){

echo "Current Hours: ". $row[‘total’];
echo “

”;
}

$currenthours = $row[‘total’];

// Again, print this to make sure this worked
echo ("$currenthours");

// Test to see if user has enough hours to proceed to download page link
if ($currenthours >= 10)
{
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else
{
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

with:
[php]
// Print out result to make sure it has pulled the users total hours from database
$row = mysql_fetch_array($result);

echo "Current Hours: ". $row[‘total’];
echo “

”;

$currenthours = $row[‘total’];

//Test to see if user has enough hours to proceed to download page link
if ($row[‘total’] >= 10)
{
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else
{
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

That did it. I’ll take a look through the code again, I’m sure there is a lot there I need to pick up on. This will get me on the right track to do more with this.

Thank you, thank you!

mark close if you dont have any questions and you are welcome

let me explain why it was not working for you

[php]
//here you initiate a loop which is used when you have multiple values and you need to echo all of them
//which is not your case.
while($row = mysql_fetch_array($result))
{
echo "Current Hours: ". $row[‘total’];
echo “

”;
}

//this variable will not work because $row[‘total’] its only visible inside the loop so $currenthours = 0
$currenthours = $row[‘total’];

//it will print 0
echo ("$currenthours");

//this condition atually means if (0 >= 10) of course not. that is why you always got the else result.
if ($currenthours >= 10)
{
echo “Show The Downloads Page Link or redirect to downloads page”;
}
else
{
echo “You have not logged enough hours, you need at least 10 to access our super duper downloads page”;
}
[/php]

Solved and Closed!

I don’t see where I can mark post as closed, probably because I started the thread as a guest, then finished it after I registered. Also don’t see where I can add to your karma or give you kudos Wilson, perhaps for the same reasons. I’ll read the posting guide, thanks again!

Right below my name you will see karma and bellow a plus [+]

No he won’t. he needs 25 posts to do it. However I have clicked it for him.

Thank you

Sponsor our Newsletter | Privacy Policy | Terms of Service