PDA

View Full Version : How to generate table according to fields selected? (Not MySQL-related)


jianneng
07-18-2003, 03:41 PM
Hi, I got a MySQL database table with 10 fields, which correspond to 10 columns of data if you output the query in PHP pages.

My idea is (in Dreamweaver), to create 10 checkboxes so that the user can choose which column of data they want to display. If this is something of less than 5 fields, I know the if/else statements can be used (though a bit tedious)...but this is 10 fields, so I just wonder if there is a better way of doing this.

Thanks in advance.


Lim.

Spookster
07-18-2003, 04:13 PM
Well basically what you will want to do is dynamically generate the SQL for the query.

You can do that by looping through the checkbox fields submitted


<form>
<input type="checkbox" name="column1"><br />
<input type="checkbox" name="column2"><br />
<input type="submit" value="submit">
</form>



$sql_start = "SELECT ";

for($i = 1; $i < sizeof($_POST); $i++){
$columns .= $_POST['column' . $i] . ",";
}

$sql_end = " FROM tablename";

//note you will need to add in some code to parse the $columns
//string to remove the last comma that was put in by the loop.
// i am just too lazy to look up the string parsing functions right now

$query = $sql_start . $columns . $sql_end;



That's just an idea though. Might have some syntactical errors in there as I am in a hurry at the moment.

jianneng
07-18-2003, 04:22 PM
Thanks moderator. I will try to figure that out.