denoteone Posted June 1, 2009 Share Posted June 1, 2009 I am try to make a dynamic script that will print data from a table into a nice neat HTML table. The only problem is I am only given the table name and one search able parameter and the value of that parameter. So I do not know how many columns there are since It varies. below will maybe help you visualize what I am trying to do. url= http://www.mysite.com?table=visitors¶m1=name&var1=mike $table = $_GET['table']; $param1 = $_GET['param1']; $var1 = $_GET['var1']; //database connect code $query = "SELECT * FROM '$table' WHERE '$param1' = '$var1'"; $result = mysql_query($query)or die (mysql_error()); now I want to print the data that is returned so I have to first find out how many total columns there are and there names. would I use two foreach loops with in earch other? one for each line returned in the query? and then the value of the columns within that line? can anyone help me with this? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted June 1, 2009 Share Posted June 1, 2009 You can use mysql_fetch_row and then loop through that. Or before all that, run the MySQL command - DESC $table . That would tell you the fields. Loop through that to grab all the names of the columns. Display them on the screen and you'll know exactly what you have to work with. Then modify the SQL you have up there. Quote Link to comment Share on other sites More sharing options...
denoteone Posted June 2, 2009 Author Share Posted June 2, 2009 You can use mysql_fetch_row() and then loop through that. Thanks Ken2k7 but I am still a little unclear. i checked out php.net/mysql_fetch_row() and have a question. Would it be possible to do something like... $query = "SELECT * FROM '$table' WHERE '$param1' = '$var1'"; $result = mysql_query($query)or die (mysql_error()); foreach ($result as $line) { echo "<tr>"; foreach( $line as $values) //loop through table columns and print them in cells but I dont know how many times to loop in this one. { echo "<td> '{$result[i]}' </td>"; } echo "</tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
denoteone Posted June 2, 2009 Author Share Posted June 2, 2009 This is how i got it done incase anyone has this issue in the future. $table = $_GET['table']; $param1 = $_GET['param1']; $var1 = $_GET['var1']; $param2 = $_GET['param2']; $var2 = $_GET['var2']; $border = $_GET['border']; $color = $_GET['color']; require_once('visitors_connections.php'); mysql_select_db($database_visitors, $visitors); $query = "SELECT * FROM $table WHERE $param1 = '$var1' and $param2 = '$var2'"; $result = mysql_query($query)or die (mysql_error()); echo "<table width=100% style='font:12px arial; color:{$color}'>"; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "<tr>"; $i = "0"; foreach($row as $value){ echo "<td>{$value}</td>"; $i = $i + "1"; } echo "</tr>"; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.