Page 1 of 1

ORDER by problem

Posted: Mon Nov 14, 2011 4:53 pm
by ShawnH20
I have this code, I have used html radio buttons, and i want to order each and put the results into a table:

Code: Select all

<html>
<title>My Roster</title>
<h1>my_roster</h1>
<form method= "post" action= "showroster.php">

	<input type= 'radio' name= "mid" value= 'Jersey Number'>Jersey Number
	<input type= 'radio' name= "birthday" value= 'Birthday'>Birthday
	<input type= 'radio' name= "artist_ln" value= 'Athletes Name'>Athletes Name
	<input type= 'radio' name= "format" value= 'Position'> Position

	<p><input type= "submit" name= "submit" value= "order" </p><br>
</html>
</form>

[syntax]<?

$table_name = "players";
$database = "my_roster";
$user = "Shawn";
$pass = "1234";


// Create the connection
$connection = mysql_connect( "localhost", $user, $pass );
$db = mysql_select_db( $database );


$db = @mysql_select_db($database, $connection) or die(mysql_error());

$sql = "SELECT * FROM $table_name ORDER BY ";<<<<<<<<<<<<<<<<<<<What do I need here to make sure they all get ordered via submit button?



$result = @mysql_query($sql,$connection) or die(mysql_error());
?>

<head><h3>My_roster</h3></head>

<?

echo "<table border='20'>
<tr>
<th>Jersey Number</th><th>Position</th><th>Athlete Name</th><th>Birthday</th><th>Stats</th>
</tr>";

while($field = mysql_fetch_array($result))
{

echo "<tr>";
echo "<td>" . $field['mid'] . "</td>";
echo "<td>" . $field['format'] . "</td>";
echo "<td>" . $field['artist_ln'] . "</td>";
echo "<td>" . $field['birthday'] . "</td>";
echo "<td>" . $field['notes'] . "</td>";
echo "</tr>";
}

echo "</table>";

while ($row = mysql_fetch_array($result)) {

$mid = $row['mid'];
$format = $row['format'];
$artist_ln = stripslashes($row['artist_ln']);
$birthday = stripslashes($row['birthday']);
$notes = stripslashes($row['notes']);

$display_block .="<p><strong>$mid</strong> by $artist_ln<br> $notes <em>(birthday:$birthday, position:$format)</em></p>";

}

?>

<html>
<body style="background-color:gainsboro;">
<? echo "$display_block"; ?>
<a href= "myroster.html"> Go Back to Roster<br>
</html>
</body>[/syntax]

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:14 pm
by Celauran
You need your radio buttons to have the same name but different values.

Code: Select all

<html>
<title>My Roster</title>
<h1>my_roster</h1>
<form method= "post" action= "showroster.php">

        <input type='radio' name="order" value="mid">Jersey Number
        <input type='radio' name="order" value="birthday">Birthday
        <input type='radio' name="order" value="artist_ln">Athletes Name
        <input type='radio' name="order" value="format"> Position

        <p><input type= "submit" name= "submit" value= "order" </p><br>
</html>
</form>
Then you can order by $_POST['order']

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:28 pm
by ShawnH20
Okay thanks Celauran, now I have this error...
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\xampp\htdocs\showroster.php on line 16

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:37 pm
by Celauran
Could you maybe post the relevant code?

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:38 pm
by ShawnH20

Code: Select all

<html>
<title>My Roster</title>
<h1>my_roster</h1>
<form method= "post" action= "showroster.php">

	<input type= 'radio' name= "order" value= 'mid'>Jersey Number
	<input type= 'radio' name= "order" value= 'birthday'>Birthday
	<input type= 'radio' name= "order" value= 'artist_ln'>Athletes Name
	<input type= 'radio' name= "order" value= 'format'> Position

	<p><input type= "submit" name= "submit" value= "order" </p><br>
</html>
</form>

Code: Select all

<?

$table_name = "players";
$database	= "my_roster";
$user		= "Shawn";
$pass		= "1234";


// Create the connection
$connection = mysql_connect( "localhost", $user, $pass );
$db = mysql_select_db( $database );


$db = @mysql_select_db($database, $connection) or die(mysql_error());

$sql = "SELECT * FROM $table_name ORDER BY $_POST['order']";



$result = @mysql_query($sql,$connection) or die(mysql_error());
?>

<head><h3>My_roster</h3></head>

<?

echo "<table border='20'>
<tr>
<th>Jersey Number</th><th>Position</th><th>Athlete Name</th><th>Birthday</th><th>Stats</th>
</tr>";

while($field = mysql_fetch_array($result))
  {

   echo "<tr>";
   echo "<td>" . $field['mid'] . "</td>";
   echo "<td>" . $field['format'] . "</td>";
   echo "<td>" . $field['artist_ln'] . "</td>";
   echo "<td>" . $field['birthday'] . "</td>";
   echo "<td>" . $field['notes'] . "</td>";
   echo "</tr>";
  }

echo "</table>";

while ($row = mysql_fetch_array($result)) {

$mid = $row['mid'];
$format = $row['format'];
$artist_ln = stripslashes($row['artist_ln']);
$birthday = stripslashes($row['birthday']);
$notes = stripslashes($row['notes']);

$display_block .="<p><strong>$mid</strong> by $artist_ln<br> $notes <em>(birthday:$birthday, position:$format)</em></p>";

}

?>

<html>
<body style="background-color:gainsboro;">
<? echo "$display_block"; ?>
<a href= "myroster.html"> Go Back to Roster<br>
</html>
</body>

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:44 pm
by Celauran
Change line 16 to this:

Code: Select all

$sql = "SELECT * FROM $table_name ORDER BY {$_POST['order']}";
Also make sure you escape your $_POST data.

Re: ORDER by problem

Posted: Mon Nov 14, 2011 5:57 pm
by ShawnH20
Thanks, its always those brackets that gets me! Thanks for the help though appreciate it! I also had an odd question but when my table comes up I have a message saying this:
Notice: Undefined variable: display_block in C:\xampp\htdocs\showroster.php on line 62
Do you know what the problem is? and how I can maybe fix that?

Re: ORDER by problem

Posted: Mon Nov 14, 2011 6:03 pm
by Celauran
You're trying to use a variable that may not have been set. Try this instead:

Code: Select all

<?php echo (isset($display_block)) ? $display_block : ""; ?>

Re: ORDER by problem

Posted: Mon Nov 14, 2011 6:09 pm
by ShawnH20
Cool, Thanks!!!!!!