Jump to content

if to loop?


karimali831

Recommended Posts

Hey can someone use their intelligence and teach me how to change the below if statements to a loop?

 

       if($h_rows) {
           $rl_rows = 8;
       }
       elseif($g_rows) {
           $rl_rows = 7;
       }
       elseif($f_rows) {
           $rl_rows = 6;
       }
       elseif($e_rows) {
           $rl_rows = 5;
       }
       elseif($d_rows) {
           $rl_rows = 4;
       }
       elseif($c_rows) {
           $rl_rows = 3;
       }
       elseif($b_rows) {
           $rl_rows = 2;
       }
       elseif($a_rows) {
           $rl_rows = 1;
       }

 

Thanks  8)

Link to comment
Share on other sites

Here you go:

<?php
for($i=8; $i>0; $i--){
if(${chr($i+96).'_rows'}){
	$rl_rows = $i;
	break;
}
}
?>

 

It does exactly what you want, but it's really bad coding, not because of what I did, but because of what you wanted. Should probably have been an array in the first place. If they are all set on the server side, and they are all booleans, I guess its okay, but else you probably want to use isset.

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.