Mysql data display in a HTML Table layout, with alt row bg color Using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vivekneo
    New Member
    • Sep 2010
    • 18

    Mysql data display in a HTML Table layout, with alt row bg color Using php

    Hi, iam trying to display student names along with their course selected and status of their selection, from mysql db, the result has to be in tabular format, with alternate row bg color, here is my code, please help me out...
    Please share a complete code, i will be very thankful
    Code:
    <? 
    include('config.php');
    
    echo "<a href='Student Details Form.php'>New Record</a>"; 
    
    echo "<table border=2 >"; 
    echo "<tr>"; 
    echo "<td><b>Sl No.</b></td>"; 
    echo "<td><b>Student Name</b></td>"; 
    echo "<td><b>Course Applied</b></td>"; 
    echo "<td><b>Status</b></td>"; 
    echo "</tr>"; 
    
    $result = mysql_query("SELECT `stud_name` AS 'Student Name', `course_studying` AS 'Course Applied', `status` AS 'Status' FROM `cjet`.`student_details` WHERE `status` = 'Selected'") or trigger_error(mysql_error()); 
    while($row = mysql_fetch_array($result)){
    
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value);
     } 
    echo "<tr bgcolor= '$bgcolor'>";  
    echo "<td valign='top'>" . nl2br( $row['stud_id']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['stud_name']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['course_studying']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['status']) . "</td>";  
    echo "<td valign='top'><a href=edit.php?id={$row['stud_id']}>Edit</a></td><td><a href=delete.php?id={$row['stud_id']}>Delete</a></td>"; 
    echo "</tr>"; 
    } 
    echo "</table>"; 
    echo "<a href='Student Details Form.php'>New Record</a>"; 
    ?>
  • paulrajj
    New Member
    • Sep 2008
    • 47

    #2
    Code:
    <? 
    include('config.php');
     
    echo "<a href='Student Details Form.php'>New Record</a>"; 
     
    echo "<table border=2 >"; 
    echo "<tr>"; 
    echo "<td><b>Sl No.</b></td>"; 
    echo "<td><b>Student Name</b></td>"; 
    echo "<td><b>Course Applied</b></td>"; 
    echo "<td><b>Status</b></td>"; 
    echo "</tr>"; 
     
    $result = mysql_query("SELECT `stud_name` AS 'Student Name', `course_studying` AS 'Course Applied', `status` AS 'Status' FROM `cjet`.`student_details` WHERE `status` = 'Selected'") or trigger_error(mysql_error()); 
    $i=0;
    while($row = mysql_fetch_array($result)){
    ($i%2 == 0) ? $bgcolor = "#ff0000" : $bgcolor = "#0000ff";
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value);
     } 
    echo "<tr bgcolor= '$bgcolor'>";  
    echo "<td valign='top'>" . nl2br( $row['stud_id']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['stud_name']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['course_studying']) . "</td>";  
    echo "<td valign='top'>" . nl2br( $row['status']) . "</td>";  
    echo "<td valign='top'><a href=edit.php?id={$row['stud_id']}>Edit</a></td><td><a href=delete.php?id={$row['stud_id']}>Delete</a></td>"; 
    echo "</tr>"; 
    $i++;
    } 
    echo "</table>"; 
    echo "<a href='Student Details Form.php'>New Record</a>"; 
    ?>
    Change the colors as you like.

    Comment

    • Vivekneo
      New Member
      • Sep 2010
      • 18

      #3
      Thank you boss,

      well i have one more question, how do i display Sl No., right now as you can see i am using the following code to display Serial Number - 'stud_id'
      Code:
      echo "<td><b>Sl No.</b></td>";
      corresponding to
      Code:
      echo "<td valign='top'>" . nl2br( $row['stud_id']) . "</td>";
      the stud_id value changes if i delete a student from the table, or when the list has only 'Selected' students name, how to implement a Sequential numbering.

      I hope iam clear with the above question

      Comment

      • paulrajj
        New Member
        • Sep 2008
        • 47

        #4
        Hi,

        you can simply show the looping variable as serial number.

        Code:
         echo "<td><b>". $i ."</b></td>";

        Comment

        • Vivekneo
          New Member
          • Sep 2010
          • 18

          #5
          Hi, i tried both the above solutions - 1) to change the alternate row bg color & 2) serial number,

          doesn't work,
          can anybody please look into this here is my updated code
          Code:
          <style type="text/css">
          table.tbg {
          	text-align:center;
          }
          </style>
          <? 
          include('config.php');
          
          echo "<a href='Student Details Form.php'>New Record</a>"; 
          
          echo "<table border=2 class='tbg'>"; 
          echo "<tr>"; 
          echo "<td><b>Sl No.</b></td>"; 
          echo "<td><b>Student Name</b></td>"; 
          echo "<td><b>Course Applied</b></td>"; 
          echo "<td><b>Status</b></td>"; 
          echo "</tr>"; 
          
          $result = mysql_query("SELECT stud_id, stud_name, course_studying, status FROM cjet.student_details  WHERE status = 'Selected' ") or trigger_error(mysql_error()); 
          
          $i=0;
          
          while($row = mysql_fetch_array($result)){
          
          ($i%2 == 0) ? $bgcolor = "#ff0000" : $bgcolor = "#0000ff";
          
          foreach($row AS $key => $value) { $row[$key] = stripslashes($value); 
           } 
          echo "<tr bgcolor= '$bgcolor'>";  
          echo "<td valign='top'><b>". $i ."</b></td>";  
          echo "<td valign='top'>" . nl2br( $row['stud_name']) . "</td>";  
          echo "<td valign='top'>" . nl2br( $row['course_studying']) . "</td>";  
          echo "<td valign='top'>" . nl2br( $row['status']) . "</td>";  
          echo "<td valign='top'><a href=edit.php?id={$row['stud_id']}>Edit</a></td><td><a href=delete.php?id={$row['stud_id']}>Delete</a></td>"; 
          echo "</tr>"; 
          } 
          echo "</table>"; 
          echo "<a href='Student Details Form.php'>New Record</a>"; 
          ?>

          Comment

          Working...