如何为每行数据运行while循环并回显每列数据的数组?

时间:2022-06-13 15:44:18

I'm trying to run a while loop for each row returned from my query, and at the same time print an array of all the vertical columns values? Here is my code so far, although I'm not sure how to achieve the array.

我正在尝试为查询返回的每一行运行while循环,同时打印所有垂直列值的数组?这是我的代码到目前为止,虽然我不知道如何实现数组。

The thing is I dont want the array of each column value inside the while loop. Basically Im trying to display a html table row of each database row values. So I already have the values displaying in the rows from the while loop, but how to access the vertical column of data and show that as an array in my code to create a graph.

问题是我不希望while循环中的每个列值的数组。基本上我试图显示每个数据库行值的html表行。所以我已经在while循环的行中显示了值,但是如何访问垂直数据列并在我的代码中将其显示为数组以创建图形。

Comments in the code also..

代码中的评论也..

<?php
mysql_select_db("db_name", $con);
$qry = "SELECT * FROM tbl_name WHERE col_name='$col_name'";
$result = mysql_query($qry);
    if (!$result) exit("The query didnt succeded");
    else {

    <!-- my while loop using each individual row from the database -->

    while ($row = mysql_fetch_array($result)) {
        $col1   = $row['col-name1'];
        $col2   = $row['col-name2'];
        $col3   = $row['col-name3'];
        $col4   = $row['col-name4'];
        $col5   = $row['col-name5'];

        include 'file_to_be_looped.php';

?>
    <br/>
    <br/>
    <?php 
  }

}

  ?>

<!-- then just get a list of all values from a single column of the same query-->

<?php echo $col1 ?>   <!--this echo produces the last value in the array, so I think im close-->

2 个解决方案

#1


1  

Try this:

尝试这个:

$col=array();
$i=1;
while ($row = mysql_fetch_array($result)) {
        $col[$i]   = $row['col-name1'];
        $i++;

And later you refer to col1 as $col[1]

之后你将col1称为$ col [1]

EDIT:

编辑:

maybe I missunderstood :

也许我误解了:

$col=array(array());
$i=1;
while ($row = mysql_fetch_array($result)) {
        $col[&i]['col-name1']   = $row['col-name1'];
        $col[$i]['col-name2']   = $row['col-name2'];
        $col[$i]['col-name3']   = $row['col-name3'];
        $col[$i]['col-name4']   = $row['col-name4'];
        $col[$i]['col-name5']   = $row['col-name5'];
        $i++;

and later you can refer to them as $col[1]['col-name1']

之后你可以将它们称为$ col [1] ['col-name1']

#2


0  

$column_array = array('col_name1' => array(),
                      'col_name2' => array(),
                      'col_name3' => array(),
                      'col_name4' => array(),
                      'col_name5' => array());

while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $colname => $value) {
        $column_array[$colname][] = $value;
    }
}

Now if you want to see all the values in a column you can do:

现在,如果要查看列中的所有值,您可以执行以下操作:

print_r($column_array['col_name1']);

#1


1  

Try this:

尝试这个:

$col=array();
$i=1;
while ($row = mysql_fetch_array($result)) {
        $col[$i]   = $row['col-name1'];
        $i++;

And later you refer to col1 as $col[1]

之后你将col1称为$ col [1]

EDIT:

编辑:

maybe I missunderstood :

也许我误解了:

$col=array(array());
$i=1;
while ($row = mysql_fetch_array($result)) {
        $col[&i]['col-name1']   = $row['col-name1'];
        $col[$i]['col-name2']   = $row['col-name2'];
        $col[$i]['col-name3']   = $row['col-name3'];
        $col[$i]['col-name4']   = $row['col-name4'];
        $col[$i]['col-name5']   = $row['col-name5'];
        $i++;

and later you can refer to them as $col[1]['col-name1']

之后你可以将它们称为$ col [1] ['col-name1']

#2


0  

$column_array = array('col_name1' => array(),
                      'col_name2' => array(),
                      'col_name3' => array(),
                      'col_name4' => array(),
                      'col_name5' => array());

while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $colname => $value) {
        $column_array[$colname][] = $value;
    }
}

Now if you want to see all the values in a column you can do:

现在,如果要查看列中的所有值,您可以执行以下操作:

print_r($column_array['col_name1']);