0

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.

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.

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-->
1

2 Answers 2

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]

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']

Sign up to request clarification or add additional context in comments.

2 Comments

&i should be $i. But what's the difference between this and $col[$i] = $row? Also, why are you starting your array from 1 instead of 0, as is normal for numeric arrays?
@Barmar well, I started the array with index 1 just to keep some similarity to the example in the question (eg: he's variables were $col1 , $col2... so I used starting index 1 to have $col[1] , $col[2] ... ); this is the same reason why I didn't just used $col[] = $row[..] ;
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']);

7 Comments

I appreciate the reply but I'm really unsure how to integrate this to my existing code, it seems the '=' on the foreach line is an unexpected character, Im also not sure what the $colname variable is declared as
That was a typo, it should be =>.
the code you posted looks like pseudo-code, so I'm not sure how to integrate this. I'm just showing the general approach to building $column_array, what you do with it is up to you.
Im not sure exactly how you tell if it is pseudo-code, but I got most of it from the PHP documents. I'll keep researching, thanks for the reply.
It's not doing anything with the data, it uses generic column names instead of real names, etc.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.