denoteone Posted June 2, 2009 Share Posted June 2, 2009 Is there anyway that I can shorten the fallowing code? lets say i didn't know the column names? if($table == "TBPlayer"){ if($parm1 == '' or $parm2 == ''){ }else{ $query = "SELECT * FROM '$table' WHERE '$parm1' = '$var1' and '$parm2' = '$var2'"; $result = mysql_query($query)or die (mysql_error()); echo "<table width=100%>" while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "<tr> <td>{$row['entryID']}</td> <td>{$row['playerID']}</td> <td>{$row['locationID']}</td> <td>{$row['playerName']}</td> <td>{$row['playerOrientation']}</td> <td>{$row['playerResolution']}</td> <td>{$row['playerModel']}</td> <td>{$row['ipAddress']}</td> <td>{$row['signCount']}/td> <td>{$row['postDate']}</td></tr>"; } echo "</table>"; } { Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2009 Share Posted June 2, 2009 $row is an array, right? If you just want to display each column value in the order that the columns are in the table, you can iterate over the $row array using a foreach() loop. If the order that you want display the information in is different than the column order, you either need to specify the columns in the SELECT term or have an array with the key names in the order that you want and iterate over the array of key names and access the corresponding elements in the $row array. Quote Link to comment Share on other sites More sharing options...
denoteone Posted June 2, 2009 Author Share Posted June 2, 2009 So would something like this work? I am kinda confused about the $row = mysql_fetch_array($result, MYSQL_ASSOC)) and how it creates the array... $query = "SELECT * FROM '$table' WHERE '$parm1' = '$var1' and '$parm2' = '$var2'"; $result = mysql_query($query)or die (mysql_error()); echo "<table width=100%>" while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo "<tr>"; i = 0; foreach($row as $value){ echo "<td>$row[i]</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.