2011-09-16 9 views
2

나는 PHP를 통해 야구 팀 라인업을 인쇄하고 있습니다. 누락 된 플레이어 6 (또는 누락 된 위치)에 대한 자리 표시자를 인쇄하고 싶습니다.조건이있는 경우 인쇄 배열

플레이어 1 -> 플레이어 5가 정상적으로 인쇄되면 플레이어 6이 아닌 인쇄소 소유자, 플레이어 7 -> 플레이어 9가 정상입니다 인쇄. 나는 코드를 단순화하려고 노력했다. 나는이 모든 방법으로이 문제를 해결하려고 노력했지만 계속 붙어 있습니다.

CODE: 

$rot  = array(); 
$pos  = array(); 
$jn    = array(); 

$x = 1; 
// loads up the arrays from the db 
while ($rot[$x], $pos[$x], $jn[$x])= $r->fetch_row()) { 
$x++; 
} 

// counts the actual number of players in linuep 
// used for validation and error display 
$num_players = mysqli_num_rows($r); 

// controls the lineup position 
for ($i = 1; $i <= 15; $i++){ 
    if($rot[$i] == $i) { 
    //prints player    
$lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>"; 
    } else { 
     // prints place holder 
     $text = "This Position needs to be filled before the next game."; 
     $lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>"; 
    } 
} 

는 또한 일치하는 위치를 발견하고 광고를 출력하도록 배열 ROT [] 반복이 시도하지만 실제로 반복 홀더를 출력한다.

// controls the lineup position 
for ($x = 1; $x <= 15; $x++){ 

for ($i = 1; $i <= ($num_players+1); $i++) { 
    if ($x == $i) { 
     //prints player    
     $lineup .= "<div data-fp='" . $pos[$i] . "'>" .$jn[$i]. "</div>"; 
    } else { 
     // prints place holder 
     $text = "This Position needs to be filled before the next game."; 
     $lineup .= "<div id='pid' data-rel='".$text."' data-fp='' data-pid='' data-jn='' title=''>x</div>"; 
    } 
    } 
} 
+2

아무도 아무것도 말하지 않는 더 자세한 설명 변수 이름, $ 부패, $의 요, $ 볼때, $ r을 사용하십시오. – thedaian

+4

@thedaian : 오, 그것은 나에게 뭔가를 말하고 있습니다. –

+1

이것은 JavaScript와 어떤 관련이 있습니까? –

답변

0

무엇에 대해 :

# index all players by position while taking them from the database 

$players = array(); 
while ($row = $r->fetch_row()) { 
    list($rot, $pos, $jn) = $row; 
    $players[$pos] = compact(array('rot', $pos, $jn); 
} 

... 

# line-up players 
for ($pos = 1; $pos <= 15; $pos++) 
{ 
    $playerExists = isset($players[$pos]); 

    if ($playerExists) 
    { 
     # do this ... 
    } 
    else 
    { 
     # do that ... 
    } 
} 
0

난 당신이 모든 숫자 요소가 가득 배열을 만들 생각 (즉, 당신은 항상 1 (15)를 통해이있을 것이다) 당신의 실수는 경우에 ($ 썩음 [$ i]를 == $ I) {

데이터베이스에서 배열을 채울 때이 줄을 추가 : $playertoid = array_flip($pos); # pos is the player number array?

즉을while (($rot[$x], $pos[$x], $jn[$x])= $r->fetch_row()) { $x++; } $playertoid = array_flip($pos);

이제 색인이 플레이어 번호 인 역방향 조회 테이블을 만들었습니다.

는 함께 if($rot[$i] == $i) { 라인을 교체 : 이 if (isset($playertoid[$i])) {