Jump to content

select columns in table from array list


Juarez
Go to solution Solved by Juarez,

Recommended Posts

Hi 

I have a task that a user provides a text file with a list of mysql column names from a table. I need to select and display the results from the selected columns. the column names vary each time they are provided. I have decided to load the column name list to an array and would like to use this to  echo out the columns. I have made an attempt at this below but I'm not sure how to make it work. 

Any help would be appreciated. 



//load patient list to array 

$file = fopen("upload/PatList.txt", "r");
$all_pats = array();
while (!feof($file)) {
   $all_pats[] = fgets($file);
   }
fclose($file);

//var_dump($all_pats);

}



// select columns from array elements
 $qry=mysql_query("SELECT *
FROM mytable", $con);

 while($row=mysql_fetch_array($qry))
 {
echo "<td>".@$row{$column_name}. "<br></td>";

}
Link to comment
Share on other sites

One loop for the rows in the resultset, another loop inside for the different columns in $all_pats.

 

There is probably a cleverer way that I'm not thinking of.

 

[edit] I'm a little concerned about the whole "someone is providing the column names in an uploaded text file" bit. Can you elaborate on that?

Edited by requinix
Link to comment
Share on other sites

The table has  20 columns. id, column1, column2, column3... etc. The text file has a list with maybe 5 columns created by the user. I will echo out  the ID column by default  (sorry not shown in the code above)  and also echo out  the 5 columns named in the text file/ array ie column2, column4, column5, column8, column9. 

Link to comment
Share on other sites

thanks for your help so far it works very well. Only I am having a problem displaying my selected columns listed in the array @cols in my output column. I am trying to use a for each loop within the while mysql_fetch_array  loop to echo each column and header , but without success. This is what I have so far..

 


// load column names from text file into array

$cols = file('upload/PatList.txt');
array_shift($cols);  // remove header
$cols = array_map('trim', $cols);
$selection = join(',', $cols);

// create query

$qry=mysql_query("SELECT ID, $selection
FROM mytable", $con);


// display table 
echo "<table border='3'  width = '50'>";

// show headers 
echo "<tr>  <th>ID</th>  <th>$cols</th> </tr>";


//display query results in table

 while($row=mysql_fetch_array($qry))
 {
echo "<tr>";
echo "<td>".$row{'ID'}. "<br></td>";

foreach($row as $cols) {
echo "<td>".$cols."<br></td>";
}



echo "</tr>";
}
echo "</table>";
Link to comment
Share on other sites

avoid fetch_array, it fetches the twice (with numeric keys and fieldname keys)

 

try

// load column names from text file into array

$cols = file('upload/PatList.txt');
array_shift($cols);  // remove header
$cols = array_map('trim', $cols);
$selection = join(',', $cols);

// create query

$qry=mysql_query("SELECT ID, $selection
FROM mytable", $con);


// display table 
echo "<table border='3'  width = '50'>";

// show headers 
echo "<tr>  <th>ID</th>  <th>" . join('</th><th>', $cols) . "</th> </tr>";


//display query results in table

while($row=mysql_fetch_row($qry))
{
    echo "<tr><td>" . join('</td><td>', $row) . "</td></tr>";
}
echo "</table>";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.