简体   繁体   中英

How to use mysql_field_name or mysqli_fetch_field to display Column Name And Value Side By Side

I have to write an application that displays a multicolumn value table in two columns(I posted a question concerning that earlier,but i have done some research which has sharpened my perspective). Each individual has a single entry in the table.The task is to display the values in these columns in a two column table.

The columns are for subjects that the individual has registered for. ie.

       MATH111,MATH112,ENG111,ENG112

Etc.These columns are dynamic in nature,because they are based on column headings in an excel sheet which are being loaded into a database. In light of this, i can not "hardcode" the name of the table in my html

          <th>MATH111</th><th>MATH112</th>........

I need to grab the column names at run time,display the name of the column " AND "the value contained in it. The only fixed value in the table is the reg_no of the student. All other values are arbitrary(including the number of columns in the table).

The basic structure of the table is:

       id int auto_increment primary key,
       reg_no varchar(25),
       arbitrary col1 int,
       arbitrary col2 int,
       arbitrary col3 int,
       arbitrary col4 int,
       arbitrary  colx....

In line with the above if i query arbitrary col1 i will get the score obtained in the course that arbitrary col1 represents.

I need to display them in a web-page in the follwing format:

      echo '<table><tr><th>Course</th><th>Score</th></tr>';

My query to get the data from the table is

         $sql="select * from '.$table_name.' where reg_no='.$reg_no.';
    ...................................(blah...)

The good part is that i know the number of columns in the table.(As this figure is saved during table creation). A little research i did yielded the finding that i could use the 'mysql_field_name' function. Or the mysqli_fetch_field function.

The mysql_field_name will be used in this way(which in my opinion is easier):

        $result=mysql_query($sql, $link); 
        $row=mysql_fetch_array($result); 

From here on...i will need some tips:

I want to display " BOTH " The field name and the value of the field. So this is how i intend to do it.Please correct me if i am wrong.

$numberofcolumnsintable=$numberofcolumnsintable++;

 while(int i=1;i<=$numberofcolumnsintable;$i++)
        (I am starting from the index 1(second column because i do not want to display the reg_no or its data)
     {   echo"<tr><td>".mysql_field_name($result,$i)."</td>"<td>".$result[$i]."</td></tr>";
      }
echo "</table>";

Am I doing it right. I am more comfortable with mysqli functions...but it uses the mysqli_fetch_field function which returns an object with properties of the column..which i have not yet figured out how to access. Any help on that will also be grateful. So back to my main question. Is the code i showed above sufficient to display the column name and its value side by side?.Thanks

Use mysql(i)_fetch_assoc instead of mysql(i)_fetch_array . That way you'll get an associative array with both the column name and the value. A simple foreach will then do it:

$rows = mysqli_query('select * from tbl');
foreach($rows as $column => $value) {
    echo $column . ": " . $value . "<br />";
}

$rows may look something like this:

array(
    array('reg_no' => '321', 'col1' => 'foo'),
    array('reg_no' => '654', 'col1' => 'knox')
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM