I have a table that sorts the name by ASC order but when i click the button it doesn't work. I tried doing the same with 2 buttons and checked some of the codes available but it doesn't work at all. Any help?
PHP Code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myfeeds";
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed");
}
$db = mysql_select_db("myfeeds", $conn);
if (!$db) {
die("Can't select database");
}
if (isset($_POST['asc'])) {
$result = mysql_query("SELECT * FROM websites ORDER BY name ASC");
} else {
$result = mysql_query("SELECT * FROM websites ORDER BY name DESC");
}
if (!$result) {
die("Failed to show queries from table");
}
$num = mysql_numrows($result);
mysql_close();
?>
Here's the button:
SORT BY:
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<button type="submit" id="asc" name="asc">ASC</button>
</form>
Table:
<table cellpadding="0">
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
<th>Description</th>
<th>Logo</th>
</tr>
<?php
$i = 0;
while ($i < $num) {
$f5 = mysql_result($result, $i, "id");
$f1 = mysql_result($result, $i, "name");
$f2 = mysql_result($result, $i, "url");
$f3 = mysql_result($result, $i, "description");
$f4 = mysql_result($result, $i, "image");
?>
<tr>
<td><?php echo $f5; ?></td>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo "<img src='$f4'>"; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
mysql_numrowsis invalid. That should read asmysql_num_rows. Plus, where are you echoing your results? It's not in what you posted. You need to echo those.<?phptagerror_reporting(E_ALL); ini_set('display_errors', 1);see if it yields anything. Alsoor die(mysql_error())tomysql_query(). Doingdie("Message X")doesn't help.<input type="submit"><button type="submit" id="asc" name="asc">ASC</button>is valid. Had the OP used<button type="button" id="asc" name="asc">ASC</button>then it would not have worked. OP's error is either elsewhere and is not showing it, or if it's the exact code that is using, isn't doing anything with the query.