0

I want to be able to sort my table as ASC in a field when the submit button is clicked.

I have used an array function to get the information from a SQL database, I have tried making a sort function but when I click 'submit' nothing happens.

This is my attempt of creating the submit button:

 <h1> Courses </h1>
<form name="Table Properties" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Order by Week
<button type="submit" name="week" class="button" <?php echo $value='1'; ?>"> Sort Week </button>
</form>
<?php 
if(isset($_POST['week'])){
        $query="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
   }

When I click submit the table remains the same.

Here is my table connect information

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";
$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

I asked a similar question in the past but I think they misunderstood due to lack of full information.

4
  • 1
    I don't see anywhere you are executing your SQL order by week. Commented Feb 12, 2015 at 10:50
  • It's at the top. The second half of my code displays the table as it is. I want the user to be able to see the table and then if they want to order it they can click a button. Commented Feb 12, 2015 at 10:51
  • You are just defining the query, but never running it Commented Feb 12, 2015 at 10:52
  • Well if you could direct me to a website that teaches you how to run scripts via buttons that would be most helpful Commented Feb 12, 2015 at 10:56

4 Answers 4

1

You have to alter your original query. $sql = "SELECT * FROM classes";

Let's summarize :

Your form :

<h1> Courses </h1>
<form name="Table Properties" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Order by Week
<button type="submit" name="week" class="button" <?php echo $value='1'; ?>"> Sort Week </button>
</form>

When you submit :

<?php

$orderString = ''; // default 
if (isset($_POST['week'])){
    $orderString = "`classes`.`week` ASC";
}

Your display :

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";
// now check order
if ($orderString !== '') {
    // Concatenate
    $sql .= ' ORDER BY ' . $orderString;
}
$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

try this code

$con = mysql_connect("host", "username", "password");
if (!$con) 
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("username", $con);
$sql = "SELECT * FROM `classes`";

if(isset($_POST['week'])){
        $sql="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
   }

$myData = mysql_query($sql, $con);
echo "<table border=1>
<tr>
<th> Name 1 </th>
<th> Name 2 </th>
<th> Name 3 </th>
<th> Name 4 </th>
</tr>";
while ($record = mysql_fetch_array($myData)){
    echo "<tr>";
    echo"<td>" . $record['name 1'] . "</td>";
    echo"<td>" . $record['week'] . "</td>";
    echo"<td>" . $record['name 3'] . "</td>";
    echo"<td>" . $record['name 4'] . "</td>";
    echo "</tr>";
}
echo "</table>";
mysql_close($con);
}
?>

Comments

0

Add an ORDER BY clause to the SQL statement in the "table connect information".

1 Comment

Would that make the table displayed ordered? I want the table unordered at first, as it is being displayed on, and then when they click the submit button it will order.
0

You define your action, when the submit button is pressed, but don't actually use it. You don't need the php part after your form. Try to edit your table connect information:

...
mysql_select_db("username", $con);
if(isset($_POST['week'])){
    $sql="SELECT * FROM `classes` ORDER BY `classes`.`week` ASC";
}
else{
    $sql = "SELECT * FROM `classes`";
}
$myData = mysql_query($sql, $con);
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.