Jump to content

convert if statement to for loop


karimali831

Recommended Posts

Hello!

 

I am going to end up ending with many If-Else statements so I'm hoping someone can change this into a for loop for me, I will learn this way too :D

 

if(empty($_GET['start']))
{
    $active_1_20 = 'active';
    $limit_by = 'LIMIT 0,20';
    $rank = 1;
}
elseif($_GET['start']==21)
{
    $active_21_40 = 'active';
    $limit_by = 'LIMIT 20,40';
    $rank = 21;
}
elseif($_GET['start']==41)
{
    $active_41_60 = 'active';
    $limit_by = 'LIMIT 40,60';
    $rank = 41;
} 
elseif($_GET['start']==61)
{
    $active_61_80 = 'active';
    $limit_by = 'LIMIT 60,80';
    $rank = 61;
} 

 

You can see the obvious pattern, next one will be elseif($_GET['start']==81) and I want it to end to elseif($_GET['start']==501)

 

Much appreciated anyone!

 

Thanks.

Link to comment
Share on other sites

 

 


// for simplicity (you can do some cleanup and checking here, too)
$start = $_GET['start'];

// the rank can be set using basic logic just once
$rank = ( isset($start) && ( $start > 0 ) ) ? $start : 1;

// This switch statement is really just here to show you what you can do besides a buch of if statements,
// but my guess is there is a better way of doing this. 
// How, exactly, are you using these $active_x1_x2 variables, and why do you need them?
switch($start){
  case 1:
    $active_1_20 = 'active';
    break;
  case 21:
    $active_21_40 = 'active';
    break;
  case 41:
    $active_41_60 = 'active';
    break;
  case 61:
    $active_61_80 = 'active';
    break;
}

// the limit can be set using simple math
$limit_by = 'LIMIT ' . ($start - 1) . ', ' ($start + 20);

 

Link to comment
Share on other sites

I'm wondering the same as socratesone as to how you're using the $active_xx_xx variables. I offered a solution using arrays as well, but this isn't really an area you could use a loop. This is without using a loop:

if(!isset($_GET['start'])){
$start = '1';
}else{
$start = $_GET['start'];
}

$active = 'active_' . $start . '_' . ($start + 19);

// Use variable variables to construct the new variable.
// If you really need it done this way that's fine, but I suggest an array.
$$active = 'active';

// Using an array.
$active = array(
$start . '_' . ($start + 19),
'active'
);

// Limit
$limit_by = 'LIMIT ' . ($start - 1) . ',' . ($start + 19);

// Rank
$rank = $start;

 

However if you are using a loop to ensure that no other values are set apart from the predefined ones, you could go for something like this:

if(!isset($_GET['start'])){
$start = '1';
}else{
$start = $_GET['start'];
}

for($i = 1; $i <= 501; $i += 20){
if($i == $start){
	$active = 'active_' . $i . '_' . ($i + 19);
	$$active = 'active';

	$rank = $i;
	$limit_by = 'LIMIT ' . ($i - 1) . ',' . ($i + 19);

	break; // finish the loop
}
}

 

 

Link to comment
Share on other sites

As to the series of $active_1_20, $active_21_40, ... variables. You wouldn't ever create a series of separately named variables like that, where you set one of them to indicate which VALUE is active. That implies that you have a series of conditional tests somewhere else in your code to find which one of those separate variables is active.

 

You would have ONE variable ($active) that you set to a VALUE that indicates which range is active.

Link to comment
Share on other sites

My guess is you're using it for some kind of pagination?

 

You could try something like this:

 

<?php


$start = $_GET['start'];
$rank = ( isset($start) && ( $start > 0 ) ) ? $start : 1;
$limit_by = 'LIMIT ' . ($start - 1) . ', ' ($start + 20);

$pageNum = 1;

// loop through all possible pages
for( $iStart = 1; $iStart > $max ; $iStart = $iStart + 20){

  $class = ($iStart == $_GET['start'] ) ? 'class="active"' : ''; 
  echo '<a href="results.php?start=' . iStart . '" ' . $class .'>' . $pageNum++ . '</a>';

}
?>

Link to comment
Share on other sites

Thanks for the for loop, worked fine but I'm having problems how to determine $active and will explain why I did it like that..

If you have a look at http://team-x1.co.uk/league/popup.php?site=standings&ladderID=24 you will see three grey bars to the right:

 

		<div id="RSCROLLBAR" class="PosButtons" style="position:absolute;top:0px;right:0px;height:555px;width:15px;">
										<div id="from_0" class="rPosButton '.$active_1_20.'" style="height: 185px;" onclick="getNextParticipants(this);this.blur();" onmouseover="Tip( get_value(\'1 - 20\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">
				  
			</div>
										<div id="from_20" class="rPosButton '.$active_21_40.'" style="height: 185px;" onclick="getNextParticipants_21_40(this);this.blur();" onmouseover="Tip( get_value(\'21 - 40\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">
				  
			</div>
										<div id="from_40" class="rPosButton '.$active_41_60.'" style="height: 185px;" onclick="getNextParticipants_41_60(this);this.blur();" onmouseover="Tip( get_value(\'41 - 60\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">
				  
			</div>
		</div>

 

It should show green bar which is "active" if I'm on that specified page.

So if $active_21_40 is set to "active", it will show green bar instead of grey.

 

I am having difficulty explaining, sorry.

 

Link to comment
Share on other sites

You're going to have to do it how PFMaBiSmAd has mentioned.

Set a single variable $active to 'active_1_20', or whatever the variable at the time will be. Then you can use the ternary operator to determine if the bar is active or not.

 

Unless you run the all the divs in the loop too, which will make it a lot easier. Something like:

 

for($i = 1; $i <= 501; $i += 20){
   $active = ''; // for now, we'll set the active variable to nothing.

   if($i == $start){
      $active = 'active'; // set the active variable to active
   }

   $from = $i - 1;
   $to = $i + 19;

   // Now you can construct each DIV
   echo '<div id="from_' . $from . '" class="rPosButton '' . $active . '" style="height: 185px;" onclick="getNextParticipants(this);this.blur();" onmouseover="Tip( get_value(\'' . $i . ' - ' . $to . '\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">
				  
			</div>';
}

 

Now you'll have to change your JS function getNextParticipants a bit, you can simply add in the from and to variables as parameters to that function. Otherwise you're going to have a crap load of JS functions. But I hope you get the idea...

Link to comment
Share on other sites

Thanks alot for the help!

 

I am understanding this a bit now but still having trouble determining what bar is active, I have put it together:

 

$start = (!isset($_GET['start']) ? 1 : $_GET['start']);

for($i = 1; $i <= 501; $i += 20){
   $active = ''; // for now, we'll set the active variable to nothing.

   if($i == $start){
      $active = 'active'; 
      $rank = $i; 
      $limit_by = 'LIMIT ' . ($i - 1) . ',' . ($i + 19);
   }
   
   $from = $i - 1;
   $to = $i + 19;

   // Now you can construct each DIV
   $bars = '<div id="from_' . $from . '" class="rPosButton ' . $active . '" style="height: 185px;" onclick="getNextParticipants(this);this.blur();" onmouseover="Tip( get_value(\'' . $i . ' - ' . $to . '\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">  </div>
            <div id="from_' . $from . '" class="rPosButton ' . $active . '" style="height: 185px;" onclick="getNextParticipants(this);this.blur();" onmouseover="Tip( get_value(\'' . $i . ' - ' . $to . '\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">  </div>
            <div id="from_' . $from . '" class="rPosButton ' . $active . '" style="height: 185px;" onclick="getNextParticipants(this);this.blur();" onmouseover="Tip( get_value(\'' . $i . ' - ' . $to . '\'),  TEXTALIGN, \'justify\', ABOVE,\'true\',  TEXTALIGN, \'justify\', PADDING,10,  TEXTALIGN, \'justify\', OPACITY,90,  TEXTALIGN, \'justify\', FIX,[300,450],  TEXTALIGN, \'justify\', DELAY,1,  TEXTALIGN, \'justify\' );" onmouseout="UnTip();">  </div>';
   
   break;
}

 

Now $active is set to all bars active.. ?

http://team-x1.co.uk/league/popup.php?site=standings&ladderID=24

Link to comment
Share on other sites

Your code really should have used page numbers, 1,2,3,4... rather than using the actual range in the links. This would have allowed you to have a variable that holds the number of items per page and simply calculate the values when you need them and you could have changed the number of items you display by simply changing one variable.

 

The only looping you should be doing is to produce the bars and since you only want three, you should only loop three times. Based on what I think you want as the result -

<?php
$num_per_page = 20;
$start = (isset($_GET['start']) && (int)$_GET['start'] > 0) ? (int)$_GET['start'] : 1;

// start should only be 1, 21, 41, 61 Test for valid start values -
if((($start - 1) % $num_per_page) != 0){
// not valid
$start = 1;
}

$rank = $start; // is $rank really used, since it is the same as $start?

$limit_by = 'LIMIT ' . ($start - 1) . ', ' . ($start + $num_per_page - 1);

$bars = '';
// loop three times
for($i = 1; $i <= 41; $i += $num_per_page){
$active = ($i == $start) ? ' active' : '';
$from = $i - 1;
$to = $i + $num_per_page - 1;

   $bars .= "<div id='from_{$from}' class='rPosButton{$active}' style='height: 185px;' onclick=\"getNextParticipants(this);this.blur();\" onmouseover=\"Tip( get_value('$i - $to'),  TEXTALIGN, 'justify', ABOVE,'true',  TEXTALIGN, 'justify', PADDING,10,  TEXTALIGN, 'justify', OPACITY,90,  TEXTALIGN, 'justify', FIX,[300,450],  TEXTALIGN, 'justify', DELAY,1,  TEXTALIGN, 'justify' );\" onmouseout=\"UnTip();\"> </div>\n";
}
?>

Link to comment
Share on other sites

that is spot on!! thank you soo much :)

 

ok, I thought I wouldn't mention about the 3 bars but now that you mention it and that I'm getting this close, I'll explain...

now if there are 1-20 participants, there should only be 1 bar, in that case I can set the style height of the bar to 555px

which is the full height.

 

21 to 40 participants will show 2 bars therefore 555 / 2 = 277.5 px for bar 1 and bar 2 only.

41 to 60 participants will show 3 bars therefore 555 / 3 = 185 pixels for bar 1, 2 and 3 (this is what it is set to now as you can see)

61 to 80 participants will show 4 bars therefore 555 / 4 = 138.75 and so on...

 

I use this to check out how many are registered:

 

  $result = safe_query("SELECT count(*) as number FROM ".PREFIX."cup_clans WHERE ladID='".$laddID."' && checkin='1' && credit!='0'");
  $ap=mysql_fetch_array($result); 

 

I'll try give this a go myself.. again thank you very much for your help!

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.