Jump to content

IF..ELSE Loop? Not working... Is there a better way?


monkeypaw201

Recommended Posts

I have a query to display an appropriate rank according to how many hours they have. As such, i am having issues checking... the largest issue is that the check needs to be dynamic, in the way that it needs to check against a database for every rank in that table...

 

<?php
mysql_select_db($database_vamsys, $vamsys);
$query_rankd = sprintf("SELECT * FROM vamsys_ranks ORDER BY hours ASC");
$rankd = mysql_query($query_rankd, $vamsys) or die(mysql_error());
$row_rankd = mysql_fetch_assoc($rankd);
$totalRows_rankd = mysql_num_rows($rankd);

mysql_select_db($database_vamsys, $vamsys);
$query_rank = sprintf("SELECT * FROM vamsys_ranks ORDER BY hours ASC LIMIT 1,100");
$rank = mysql_query($query_rank, $vamsys) or die(mysql_error());
$row_rank = mysql_fetch_assoc($rank);
$totalRows_rank = mysql_num_rows($rank);

if($row_hour_count['hour_count'] > $row_rankd['hours'])
{
$rank1 = $row_rankd['title'];
}
do(
elseif($row_hour_count['hour_count'] > $row_rank['hours'])
{
$rank1 = $row_rank['title'];
}
} while ($row_rank = mysql_fetch_assoc($rank));
?>

 

is what i tried but it gave me errors to no end...

Link to comment
Share on other sites


if($row_hour_count['hour_count'] > $row_rankd['hours'])
{
$rank1 = $row_rankd['title'];
}
do(
elseif($row_hour_count['hour_count'] > $row_rank['hours'])
{
$rank1 = $row_rank['title'];
}
} while ($row_rank = mysql_fetch_assoc($rank));

 

Look at where your do statement is it should be inside of your else if or completely outside of your if statement that is what is causing your error.

Link to comment
Share on other sites

yes, this is invalid. you can't continue an IF after starting a DO loop:

 

do(
elseif($row_hour_count['hour_count'] > $row_rank['hours'])
{
$rank1 = $row_rank['title'];
}
} while ($row_rank = mysql_fetch_assoc($rank));

 

can you explain the logic of what the code is supposed to do?

Link to comment
Share on other sites

The logic:

 

I need to get a list of ranks and hours from the database and then compare them all to the user's hours and echo what rank that user is.

 

ie bob has 7 hours, the 2 closest ranks are: rank1 and rank2

 

rank1 requires 0 hours

rank2 requires 10 hours

 

it will echo rank1

Link to comment
Share on other sites

i would select all records, then loop over them. inside the loop, i would say "if hours >= 10, then rank = 2, else rank = 1"

 

so i would only select once, then loop over that result, checking the hours field to set the rank at 1 or 2.

 

 

the problem is that the ranks are stored in the database and when the administrator changes the hour requirement it needs to update, the numbers can't be hard coded..

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.