2012-04-10 5 views
0

저는 PHP를 처음 사용하고 일부 기본 기능을 수행하기 위해 충분히 배우 려합니다. 내 사용자가 자신을 편집 할 수있는 표를 만들 수 있었고 다시 표시 할 수 있지만 질문을 만났습니다. 아래 스크립트를 사용하여 다양한 제품에 대한 기술 수준을 입력 할 수 있습니다. 나는 그들이 "0"또는 공란을 입력하는 각 세포를 강조 할 수 있기를 원했다. 사용자 입력은 0-5 사이에 있습니다 (또는 아직 입력하지 않은 경우 비어 있음).MySQL 쿼리 출력을 기반으로 셀의 배경색을 설정하십시오.

이 모든 작업은 내 로컬 호스트에서 수행되므로 모든 보안 조치가 아직 이루어지지 않았 음을 인정합니다.

나는 많은 게시물을 읽고 나 자신을 알아 내려고 노력했지만 근본적으로 내가 잘못 생각한 것을하고있다.

위의 사항에 대한 도움을 주시면 감사하겠습니다. 나는 코딩을 도와 사람들을 위해 (페이팔을 통해) 맥주를 구입하는 것으로 알려져했습니다 여기에 :)

데이터베이스의 결과를 인쇄 할 기존 코드 : 어쩌면

<?php 
//This will connect to the database in order to begin this page 
mysql_connect("localhost", "root", "time2start") or die (mysql_error()); 
//Now we will select the database we need to talk to 
mysql_select_db("joomla_dev_15") or die (mysql_error()); 
$query = "SELECT * FROM enterprise_storage WHERE id=1"; 
$result = mysql_query($query) or die (mysql_error()); 
echo "<table border='1'>"; 
echo "$row"; 
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th>  <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th>Luke Soares</th> <th>Josh Wenger</th> </tr>"; 
// keeps getting the next row until there are no more to get 
while($row = mysql_fetch_array($result)) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['model']; 
echo "</td><td>"; 
echo $row['beeg']; 
echo "</td><td>"; 
echo $row['hamke']; 
echo "</td><td>"; 
echo $row['jaczyk']; 
echo "</td><td>"; 
echo $row['jontow']; 
echo "</td><td>"; 
echo $row['macdonald']; 
echo "</td><td>"; 
echo $row['munozcano']; 
echo "</td><td>"; 
echo $row['shaffer']; 
echo "</td><td>"; 
echo $row['soares']; 
echo "</td><td>"; 
echo $row['wenger']; 
echo "</td></tr>"; 
} 
echo "</table>"; 
?> 
<FORM> 
<INPUT TYPE="BUTTON" VALUE="Return to the Home Page"  ONCLICK="window.location.href='http://localhost/~user/joomla15/custom/skilldisplay.php'"> 
</FORM> 

답변

1

은 이

같은

while($row = mysql_fetch_array($result)) { 
// Print out the contents of each row into a table 
echo "<tr>"; 
foreach($row as $content) { 
    if($content == 0) { 
     echo "<td style='background-color:gray;'>"; 
    } 
    else { 
     echo "<td style='background-color:green;'>"; 
    } 
    echo $content . "</td>"; 
} 
echo $row['wenger']; 
echo "</td>"; 
} 
echo "</tr></table>"; 
+0

이 일 중 하나가 작동하지 않았다. 나는 당신이 여기서 말한 것처럼 붙여 넣었습니다. 저장 후 페이지가로드되지 않습니다. (나는 또한 "회색"을 "빨간색"으로 변경했습니다) – user1267673

+0

무엇이 오류입니까? – Dion

+0

은'background-color'이어야합니까? 'color'는 셀이 아닌 텍스트의 색을 제어합니다. – octern

0

시도 뭔가 생성 된 문서의이 추가

<style type="text/css"> 
    .red{ background-color: red; } 
</style> 

이 당신의 PHP입니다 :

<?php 
// sanitize value 
$value = trim($row['model']); 
$class = (empty($value)) ? 'red' : ''; 

// display 
echo "<td class=\"$class\">$value</td>"; 
... 
?> 
+0

불행히도 그건 효과가 없습니다. 나는 $ row [hamke] 행에 그것을 "0"으로 명확하게 적용했지만 색깔은 변하지 않았습니다. 시도해 줘서 고마워! – user1267673

+0

데이터를 잘못 보거나 오해하고 있습니다. 다른 공백이나 특수 문자가없는 한'empty (0)'과'empty ('0')'는 false로 평가됩니다. 위의 업데이트 된 게시물보기, 나는 물건을 단순화하고 데이터에 트림을 추가했습니다. – Alex

0

좋아, 그래서 마침내 작업을 얻을 수 있었다. 위의 두 응답은이 작업을 수행하는 올바른 방법을 파악하는 데 도움이되었습니다.

물론 내 접근 방식이 최선의 방법은 아닐지 모르지만 테스트를 거쳐 내 필요에 맞게 작동합니다. 향후 수색자를 들어, 여기에 내가 무슨 짓을했는지 :

<?php 
    //This will connect to the database in order to begin this page 
    mysql_connect("localhost", "root", "time2start") or die (mysql_error()); 
    //Now we will select the database we need to talk to 
    mysql_select_db("joomla_dev_15") or die (mysql_error()); 
    $query = "SELECT * FROM enterprise_storage"; 
    $result = mysql_query($query) or die (mysql_error()); 
    echo "<table border='1'>"; 
    echo "$row"; 
    echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th><a href='http://localhost/~user/joomla15/custom/updateform.php'>Luke Soares</a></th> <th>Josh Wenger</th> </tr>"; 
    // keeps getting the next row until there are no more to get 
    while($row = mysql_fetch_array($result)) { 
// Print out the contents of each row into a table 
echo "<tr><td>"; 
echo $row['model']; 
echo "</td>"; 
if ($row['beeg'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['beeg'] ; 
}else{ 
    echo '<td>' .$row['beeg']; 
} 
echo "</td>"; 
if ($row['hamke'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['hamke'] ; 
}else{ 
    echo '<td>' .$row['hamke']; 
} 
echo "</td>"; 
if ($row['jaczyk'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['jaczyk'] ; 
}else{ 
    echo '<td>' .$row['jaczyk']; 
} 
echo "</td>"; 
if ($row['jontow'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['jontow'] ; 
}else{ 
    echo '<td>' .$row['jontow']; 
} 
echo "</td>"; 
if ($row['macdonald'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['macdonald'] ; 
}else{ 
    echo '<td>' .$row['macdonald']; 
} 
echo "</td>"; 
if ($row['munozcano'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['munozcano'] ; 
}else{ 
    echo '<td>' .$row['munozcano']; 
} 
echo "</td>"; 
if ($row['shaffer'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['shaffer'] ; 
}else{ 
    echo '<td>' .$row['shaffer']; 
} 
echo "</td>"; 
if ($row['soares'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['soares'] ; 
}else{ 
    echo '<td>' .$row['soares']; 
} 
echo "</td>"; 
if ($row['wenger'] == '0'){ 
    echo '<td bgcolor="#FF0000">' . $row['wenger'] ; 
}else{ 
    echo '<td>' .$row['wenger']; 
} 
echo "</td></tr>"; 

} 에코 "";

?>

관련 문제