0

I really need your help. I am trying to echo a table from my database but it first of all comes with my table names from the database which I don't want and also the css is weird. I would like my table to have a different heading in each column then the data displayed below the corresponding column header in a uniform manna.

the php code is here

    <div id="content-1">
        <div>
            <?php 

                $table = 'std_details';
                // sending query for table
                $result = mysql_query("SELECT * FROM {$table}");

                if (!$result) {

                    die ("Query to show fields from table failed");

                }

                $fields_num = mysql_num_fields($result);

                echo "<h1>All Students</h1>";
                echo "<table border='2' class='gridtable'><tr>";

                // printing table headers
                for ($i=0; $i < $fields_num; $i++) { 

                    $field = mysql_fetch_field($result);
                    echo "<td>{$field->name}&nbsp;&nbsp;</td>";

                }

                echo "</tr>\n";

                // printing table rows
                while ($row = mysql_fetch_row($result)) {

                    echo "<tr>";

                    // $row is array.... foreach( .. ) puts every element
                    // of $row to $cell variable
                    foreach ($row as $cell) {

                        echo "<td>&nbsp;\t$cell</td>";

                    }
                    echo "</tr>\n";
                }

                mysql_free_result($result)
            ?>
        </div>
    </div>

And the table css is

    table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
    }
    table.gridtable th {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #dedede;
    }
    table.gridtable td {
        border-width: 1px;
        padding: 8px;
        border-style: solid;
        border-color: #666666;
        background-color: #ffffff;
    }

Thanks guys. I really need help!

enter image description here

5
  • Not sure what you mean by "the css is weird". And we can't see your database, so we don't know what comes out of it and what to replace it with! Also, mysqli is better than mysql, which is deprecated. And you have SQL injection and even HTML injection. Commented Jul 28, 2015 at 9:21
  • If you want separate heading then don't pull from database. Keep you column heading static and load data from database Commented Jul 28, 2015 at 9:49
  • this is the image for my database @MrLister Commented Jul 28, 2015 at 10:08
  • Please note that mysql is not supported anymore. Use PDO or mysqli instead. Commented Jul 28, 2015 at 10:24
  • how can I use PDO or mysqli? am new yo databases. I just want to finish this then i start something new. Please help me and I use PDO Commented Jul 28, 2015 at 11:38

2 Answers 2

1

I cant understand why you are using mysql instead of mysqli any way this may help you

  <!DOCTYPE html>
    <html>
    <head>
     <style>
      table.gridtable {
        font-family: verdana,arial,sans-serif;
        font-size:11px;
        color:#333333;
        border-width: 1px;
        border-color: #666666;
        border-collapse: collapse;
       }
      table.gridtable th {
         border-width: 1px;
         padding: 8px;
         border-style: solid;
         border-color: #666666;
         background-color: #dedede;
       }
     table.gridtable td {
         border-width: 1px;
         padding: 8px;
         border-style: solid;
         border-color: #666666;
         background-color: #ffffff;
       }

    </style>
  <!--- uncomment these line if you are loading css from external file --->
  <!--- <link href="Your css file" rel="stylesheet"> --->
  </head>
<div id="content-1">
      <div>
           <?php 

            $table = 'std_details';
            // sending query for table
            $result = mysql_query("SELECT * FROM {$table}");

                if (!$result) {

                  die ("Query to show fields from table failed");

                }

               $fields_num = mysql_num_fields($result);

               echo "<h1>All Students</h1>";
               echo "<table border='2' class='gridtable'><tr>";

               // printing table headers
               for ($i=0; $i < $fields_num; $i++) { 

                $field = mysql_fetch_field($result);
                echo "<td>{$field->name}</td>";

            }

            echo "</tr>";

            // printing table rows
            while ($row = mysql_fetch_row($result)) {

                echo "<tr>";

                // $row is array.... foreach( .. ) puts every element
                // of $row to $cell variable
                foreach ($row as $cell) {

                    echo "<td>&nbsp;\t$cell</td>";

                }
                echo "</tr>";
            }
            echo "</table>";

            mysql_free_result($result)
        ?>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

How do I get to use mysqli? I have never used it. What's the difference? @GULIM
because mysql is deprecated just google for it , the only difference is the syntax
1

For the tablename in field name you can use a simply sustr($field->name, strlen ( $fieldName ........)) For the header use <th> and not <td> and don't use tab for cells value. try like this

// printing table headers
            for ($i=0; $i < $fields_num; $i++) { 





                $field = mysql_fetch_field($result);

              // eliminate the tablename
              $name = substr($field->name, strlen ( $fieldName ) - strlen('yourTableName),strlen ( $fieldName ) -1  )
                // echo the header th
                echo "<th>{$name}&nbsp;&nbsp;</th>";

            }

            echo "</tr>\n";

            // printing table rows
            while ($row = mysql_fetch_row($result)) {

                echo "<tr>";

                // $row is array.... foreach( .. ) puts every element
                // of $row to $cell variable
                foreach ($row as $cell) {

                    // echo the cell td
                    echo "<td>$cell</td>";

                }
                echo "</tr>\n";
            }

3 Comments

yes is substr. (the basic php str function) i have update the answer
so how does PDO or mysqli come in?
If I may ask, do I have to replace '$fieldName' with the heading I want for that specific column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.