Tremour Posted July 9, 2007 Share Posted July 9, 2007 Ok, so basicly in this script I'm requesting players ranks from the server. I have all the players information, including ranks stored into a table and that works fine, but soon as i try to use an If statement it all seems to go wrong. for example. This works fine and displays the persons rank: <?php echo $val['rank'];?> Altogether there are 5 ranks, called: 0,1,2,3,4 but as soon as i use an if statement: <?php if ($val['rank'] == 1) { echo 'Person is Rank1'; } if ($val['rank'] == 0) { echo 'Person is Rank0'; } else { echo 'Unexpected error please contact the Admin'; } ?> It constantly shows them as being Rank0, but it does not display the else command unless i remove the Rank0 so its just 1,2,3,4 then it'll show the else command. I'm really confused. if anyone could help it'll be great. Here's the full source code for my page: <?php //opens 2 html pages the clan member page and who's online page //converts them into arrays then loops thru //and outputs matches //for caching you must create a directory(folder) named cache with write permissions //config $caching = 1;//1 is on 0 is off $cache_expiry = 1;//seconds before cached page is rebuilt $clanid = 27;//change number to the clan number you want to see $dir = 'cache';//name of directory with write permissions $pathtocachedfile = $dir.'/clan'.$clanid.'.htm'; $cache = new CacheFile($pathtocachedfile, $cache_expiry); if($caching && $cache->check_expiry()) { include $pathtocachedfile;//show cached page }else{//cached page is stale or doesn't exits so lets rebuild(or caching is off) //get online players and clan members $clan_members = get_file('http://www.astonia.com/cgi-v3/conflict.cgi?step=6&clan='.$clanid, 7); $online_players = get_file('http://www.astonia.com/cgi-v3/conflict.cgi?step=5', ; $online_player_info = fix_online_players($online_players); //buffer output ob_start(); display_members_online($clan_members,$online_member,$online_player_info,$clan_id); //display_unclanned_online($online_names); // caching if($caching) { $ob_contents = ob_get_contents(); $cache->update($ob_contents); } ob_end_flush();//unset output buffer } //functions function display_members_online($clan_members, $online_member,$online_player_info,$clanid) { $online_members = get_members_online($clan_members,$online_player_info); $clan_name = array_shift($online_members); ?> <h2 align="center"><?php echo $clan_name;?></h2> <table align="center" width="60%"> <tr> <th>Name</th> <th>Location</th> <th>Class</th> <th>Level</th> <th>Clan Rank</th> </tr> <?php if(empty($online_members) ) { ?> <tr><td colspan="5">No one online</td></tr> <?php }else { foreach($online_members as $key=>$val) { if ($val['rank'] == 4) { echo 'Person is Rank4'; } if ($val['rank'] == 3) { echo 'Person is Rank3'; } if ($val['rank'] == 2) { echo 'Person is Rank2'; } if ($val['rank'] == 1) { echo 'Person is Rank1'; } if ($val['rank'] == 0) { echo 'Person is Rank0'; } else { echo 'Unexpected error please contact the Admin'; } ?> <tr> <td align="left" ><?php echo $val['name'];?></td> <td align="center"><?php echo $val['location'];?></td> <td align="center"><?php echo $val['class'];?></td> <td align="center"><?php echo $val['level'];?></td> <td align="center"><?php echo $val['rank'];?></td> </tr> <?php }//end foreach ?> </table> </body> </html> <?php }//end else return $online_members_names; }//end function display online members function get_file($filename,$slice/* amount of garbage elements to remove */) { //gets online members file contents and returns it as an array $buffer = file_get_contents($filename); $buffer = clean_file_buffer($buffer); $buffer = explode(',', $buffer); $_name = $buffer[1]; //first elements are trash so remove $result = array_slice($buffer,$slice); array_unshift($result,$_name);//add clan name to beginning of array unset($buffer); return $result; } function fix_online_players($online_players) { //shortens and splits the array for quicker searches $online_players = array_slice($online_players, 1);//first element is trash $i = 0; $n = 0; foreach($online_players as $key=>$val) { ++$i; if($i%5 == 1) $online_player_info[0][$n] = $val;//names else if($i%5 == 2) $online_player_info[1][$n] = $val;//location else if($i%5 == 0) ++$n; else continue; }//end foreach return $online_player_info; } function clean_file_buffer($buffer) { //separate the text by converting an html tag into a string separator //strip remaining html $search = array ('@(<h2>)@i', // convert to separator '@(</td>)@i', // convert to separator '@(</th>)@i', // convert to separator '@<[\/\!]*?[^<>]*?>@si'); // strip remaining html $replace = array (',', ',', ',', ''); return preg_replace($search, $replace, $buffer); } function get_members_online($clan_members,$online_player_info){ //gets name matches from clan and online names $clan_name = array_shift($clan_members);//gets first element then shifts indices to start back at zero $i = 0; $n = 0; foreach($clan_members as $key=>$val) { //if member is in the online array, add to online_member array or else skip them ++$i; if($i == 1) { $skip = 0; $member_key = array_search($val,$online_player_info[0]); if( $member_key && !empty($val) ) $online_member[$n]['name'] = $val; else $skip = 1; } else if($i == 2 && $skip == 0) { $online_member[$n]['class'] = $val; $online_member[$n]['location'] = $online_player_info[1][$member_key]; } else if($i == 3 && $skip == 0) { $online_member[$n]['level'] = $val; } else if($i == 4) { if($skip == 0) $online_member[$n]['rank'] = $val; $i = 0; $n++; } }//endforeach if(is_array($online_member) ) array_unshift($online_member,$clan_name); else $online_member[] = $clan_name; return $online_member; } //included library files class CacheFile { //compares file created date with time-expiry //if cached page is fresh it is included and the script exits //if it is stale, lets the script rebuild the page //when update($contents) is called it gets recached var $tmp_file; var $filename; var $expires; var $no_script = 0;//cleans out php, java script if not set to 0 function CacheFile($filename = FALSE, $expires = 300) { if($filename === FALSE) { echo '<div align="center">No filename in CacheFile::check_expiry()</div>'; return FALSE; } $this->filename = $filename; $this->tmp_file = $this->filename.'.tmp'; $this->expires = time() - $expires; } function check_expiry(){ if(file_exists($this->filename)) { if(filemtime($this->filename) > $this->expires) { return TRUE; } return FALSE;//page expired } return FALSE;//file doesn't exist } function update($contents) { if($this->filename === FALSE) { return FALSE; } $fp = fopen($this->tmp_file,'w'); if(!$fp) { return FALSE; } if(fwrite($fp, $contents ) == FALSE) { fclose($fp); echo 'could not create file'; return FALSE; } fclose($fp); if(file_exists($this->filename) ) unlink($this->filename); rename($this->tmp_file,$this->filename); } function _sanitized() { $buffer = file_get_contents($this->filename); $find = array ('@<script[^>]*?>.*?</script>@si', // strip js '@<\?[^?>]*?\?>.@si'); // strip php $changeto = array ('', '', ''); return preg_replace($find, $changeto, $buffer); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/ Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 Care to show a little more of your script? When you echo them all are you actually getting "one", "two", "three" and "four" or are you gettign numbers? Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/#findComment-293283 Share on other sites More sharing options...
HuggieBear Posted July 9, 2007 Share Posted July 9, 2007 Have you read the PHP manual on Comparison Operators? Are your operands of the same type? Regards Huggie Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/#findComment-293288 Share on other sites More sharing options...
Yesideez Posted July 9, 2007 Share Posted July 9, 2007 In your first snippet you're checking a numberical WORD yet in your script you're using numbers!? Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/#findComment-293293 Share on other sites More sharing options...
BillyBoB Posted July 9, 2007 Share Posted July 9, 2007 try changing if ($val['rank'] == 4) { echo 'Person is Rank4'; } if ($val['rank'] == 3) { echo 'Person is Rank3'; } if ($val['rank'] == 2) { echo 'Person is Rank2'; } if ($val['rank'] == 1) { echo 'Person is Rank1'; } if ($val['rank'] == 0) { echo 'Person is Rank0'; } else { echo 'Unexpected error please contact the Admin'; } to: if ($val['rank'] == 4) { echo 'Person is Rank4'; }else if ($val['rank'] == 3) { echo 'Person is Rank3'; }else if ($val['rank'] == 2) { echo 'Person is Rank2'; }else if ($val['rank'] == 1) { echo 'Person is Rank1'; }else if ($val['rank'] == 0) { echo 'Person is Rank0'; } else { echo 'Unexpected error please contact the Admin'; } Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/#findComment-293294 Share on other sites More sharing options...
per1os Posted July 9, 2007 Share Posted July 9, 2007 Umm maybe it helps if you use the right if statement? <?php $val['rank'] = intval($val['rank']); // make sure it is in int. if ($val['rank'] == 4) { echo 'Person is Rank4'; }else elseif ($val['rank'] == 3) { echo 'Person is Rank3'; }else elseif ($val['rank'] == 2) { echo 'Person is Rank2'; }else elseif ($val['rank'] == 1) { echo 'Person is Rank1'; }else elseif ($val['rank'] == 0) { echo 'Person is Rank0'; } else { echo 'Unexpected error please contact the Admin'; } ?> Elseif's are the right type of if's for this statement. Quote Link to comment https://forums.phpfreaks.com/topic/59076-unexpected-problems-with-ifelseif-codes/#findComment-293397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.