Link to sort table column in PHP MYSQL -
<?php $con = mysql_connect ("localhost", "user", "pass") or die ('error: ' . mysql_error()); mysql_select_db ("members"); if(isset($_get['orderby'])){ $order = $_get['orderby']; $result = "select * persons order ".mysql_real_escape_string($order)." desc"; } else{ $result = mysql_query("select * persons"); } $num_rows = mysql_num_rows($result); $row_counter = 0; echo "<table width=600 border=0 cellspacing=0>\n"; echo "<tr>\n <th> </th>\n <th>first name</th>\n <th>last name</th>\n <th>email address</th>\n <th>city</th>\n <th>state</th>\n <th><a href='index.php?orderby=submitdate'>date</a></th>\n </tr>"; while($row = mysql_fetch_array($result)){ if($row_counter % 2){ $row_color="bgcolor='#ffffff'"; } else{ $row_color="bgcolor='#f3f6f8'"; } echo "<tr ".$row_color.">"; echo "<td class='id'>" . $row['id'] . "</td>\n"; echo "<td>" . $row['firstname'] . "</td>\n"; echo "<td>" . $row['lastname'] . "</td>\n"; echo "<td>" . $row['email'] . "</td>\n"; echo "<td>" . $row['city'] . "</td>\n"; echo "<td>" . $row['state'] . "</td>\n"; echo "<td>" . $row['submitdate'] . "</td>\n"; echo "</tr>"; $row_counter++; } echo "</table>"; mysql_close($con); ?
>
i cannot figure out why link sort query not working. ideas? pre-thanks!
your if else block wrong. in 1 case $result query in result-set. check code below.. fixed.
<?php $con = mysql_connect ("localhost", "user", "pass") or die ('error: ' . mysql_error()); mysql_select_db ("members"); if(isset($_get['orderby'])){ $order = $_get['orderby']; $sql = "select * persons order ".mysql_real_escape_string($order)." desc"; } else{ $sql = "select * persons"; } $result = mysql_query($sql); $num_rows = mysql_num_rows($result); $row_counter = 0; echo "<table width=600 border=0 cellspacing=0>\n"; echo "<tr>\n <th> </th>\n <th>first name</th>\n <th>last name</th>\n <th>email address</th>\n <th>city</th>\n <th>state</th>\n <th><a href='index.php?orderby=submitdate'>date</a></th>\n </tr>"; while($row = mysql_fetch_array($result)){ if($row_counter % 2){ $row_color="bgcolor='#ffffff'"; } else{ $row_color="bgcolor='#f3f6f8'"; } echo "<tr ".$row_color.">"; echo "<td class='id'>" . $row['id'] . "</td>\n"; echo "<td>" . $row['firstname'] . "</td>\n"; echo "<td>" . $row['lastname'] . "</td>\n"; echo "<td>" . $row['email'] . "</td>\n"; echo "<td>" . $row['city'] . "</td>\n"; echo "<td>" . $row['state'] . "</td>\n"; echo "<td>" . $row['submitdate'] . "</td>\n"; echo "</tr>"; $row_counter++; } echo "</table>"; mysql_close($con); ?>
Comments
Post a Comment