2014-04-09 2 views
-5

나는 내가 만든 바위 가위 게임을위한 리더 보드를 만들고 있습니다. SQL 테이블에서 데이터를 검색하여 HTML 테이블에 삽입해야합니다. 테이블에서 값을 검색하는 코드가 있지만 CSS 스타일 시트가 아닌 페이지의 왼쪽 위에 인쇄됩니다.PHP와 MYSQL을 사용하여 HTML 테이블을 채울 필요가 있습니다.

<?php 
      $connect = mysql_connect("localhost","username", "passworkd"); 
      if (!$connect) { 
       die(mysql_error()); 
      } 
      mysql_select_db("username"); 
      $results = mysql_query("SELECT * FROM highscore ORDER BY Score"); 
      while($row = mysql_fetch_array($results)){ 
      ?> 
       <tr> 
       <td><?php echo $row['Name']?></td> 
       <td><?php echo $row['Score']?></td> 
       <td><?php echo $row['Date']?></td> 
       </tr> 
        } 
        ?> 
      <?php 


<html> 
<head> 

<link rel="stylesheet" href ="css/table.css"> 
<title>Leaderboards - Rock, Paper, Scissors</title> 
</head> 
<body> 
<div class="easyTable" > 
       <table > 
        <tr> 
         <td colspan="3"><a style="color: white;"href="TypingTest.html">Typing Test, WPM - Easy<a></td> 
        </tr> 
        <tr id="easyTitle"> 
         <td style="color: white;"> <?php echo $row['Name']?> 
          Name 
         </td> 
         <td style="color: white;"><?php echo $row['Score']?> 
          Score 
         </td> 
         <td style="color: white;"><?php echo $row['Date']?> 
          Date 
         </td> 
        </tr> 
        <tr> 
         <td > 

         </td> 
         <td> 

         </td> 
         <td> 

         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 3 
         </td> 
         <td> 
          Row 3 
         </td> 
         <td> 
          Row 3 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 
        <tr> 
         <td > 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
         <td> 
          Row 2 
         </td> 
        </tr> 

       </table> 
      </div> 
+1

당신은 html과 php 사이의 줄을 흐리게하고 있습니다.' Ibu

+0

그걸 수정 했는데도 여전히 내 테이블의 행을 채우지 못합니다. – user278153

+0

mysql_ *을 사용하지 마십시오. 사용되지 않습니다. – ThePixelPony

답변

0

즉각적인 질문에 답하기 위해 HTML 문서 나 테이블을 시작하기 전에 테이블 행을 반향시키고 있습니다. 따라서 PHP는 테이블 정보를 다른 것보다 먼저 인쇄합니다. 훨씬 더 큰 문제는 비 PHP 코드로 채워진 두 번째 PHP 여는 태그가 있다는 것입니다. MySQLi 나 PDO 대신에 MySQL 함수를 사용하기도합니다. 이것은 나쁜 습관입니다.

다음은 HTML 표에서 PHP 변수를 가져 오기 위해 수행해야하는 작업에 대한 간단한 예입니다.

<?php $connect = mysql_connect("localhost","username", "passworkd"); 
       if (!$connect) { 
        die(mysql_error()); 
       } 
       mysql_select_db("username"); 
       $results = mysql_query("SELECT * FROM highscore ORDER BY Score"); 
?> 
<!DOCTYPE html> 
<html> 
    <head><!--header stuff--></head> 
    <body> 
     <table> 
      <?php while($row = mysql_fetch_array($results)): ?> 
       <tr> 
        <td><?php echo $row['Name']; ?></td> 
        <td><?php echo $row['Score']; ?></td> 
        <td><?php echo $row['Date']; ?></td> 
       </tr> 
      <?php endwhile; ?> 
     </table> 
    </body> 
</html> 
1

는 다음과 같은 시도 : 당신이 순차적으로 해석하는 경우

<?php 
    $connect = mysql_connect("localhost","username", "passworkd"); 
    if (!$connect) { 
     die(mysql_error()); 
    } 
    mysql_select_db("username"); 
    $results = mysql_query("SELECT * FROM highscore ORDER BY Score"); 
?> 

<html> 
<head> 

<link rel="stylesheet" href ="css/table.css"> 
<title>Leaderboards - Rock, Paper, Scissors</title> 
</head> 
<body> 
<div class="easyTable" > 
    <table > 
     <tr> 
      <td colspan="3"><a style="color: white;"href="TypingTest.html">Typing Test, WPM - Easy<a></td> 
     </tr> 
     <?php while($row = mysql_fetch_array($results)) : ?> 
     <tr id="easyTitle"> 
      <td style="color: white;"> <?php echo $row['Name']?> 
       Name 
      </td> 
      <td style="color: white;"><?php echo $row['Score']?> 
       Score 
      </td> 
      <td style="color: white;"><?php echo $row['Date']?> 
       Date 
      </td> 
     </tr> 
     <?php endwhile;?> 
    </table> 
</div> 

이 이해가됩니다. 쿼리는 상단에서 실행됩니다 : mysql_query("SELECT * FROM highscore ORDER BY Score");. 그런 다음 HTML이 인쇄되기 시작합니다. 그런 다음 while 루프로 실행됩니다.

또한 PDO와 mysqli_에 대해 배우고 mysql_ 명령을 피해야합니다.

+0

당신이 한 일에 대한 설명이 도움이 될 수 있습니다. – Ibu

관련 문제