0

I have 2 forms, on 2 different pages. I have to use a select list from the previous page to make the user select the State they live in. According to that state, i will go in my database and display all the CLUBS in their State at the TOP of the list and the rest below them in the same list. The problem or query i have is, i know how to display all clubs that from their state, but i have no idea how to display them at the top of the list while showing all other clubs. Can anyone help?

<?php
session_start();

$states = $_SESSION["stateslected"];

$conn = mysql_connect("localhost", "xxxx", "xxxx"); 
mysql_select_db("xxxx", $conn) 
or die ('Database not found ' . mysql_error() ); 

$sql = mysql_query("SELECT clubName FROM teams WHERE homeState = '$states'  ");


echo "<select name = 'clubs'>\n";

while ($data = mysql_fetch_array($sql, MYSQL_ASSOC))
{
  echo "<option value='{$data['clubName']}'>{$data['clubName']}</option>\n";
}

echo "</select>\n";
mysql_close($conn);
?>

So with that, if the user selects OK on previous page, it should display

  1. OKC Thunder (Oklahoma Team)
  2. LA Lakers (Random Team)
  3. NY Knicks (Random Team)
  4. etc.. (Random Team)

instead of just

  1. OKC Thunder (Oklahoma Team)

OR

If a user selects CAL (California), it should display

  1. LA Lakers (California Team)
  2. LA Clippers (California Team)
  3. MI Heat (Random Team)
  4. BK Nets (Random Team)
  5. etc..

instead of just

  1. LA Lakers (California Team)
  2. LA Clippers (California Team)
roro
  • 713
  • 2
  • 6
  • 19
  • 2
    `SELECT clubName FROM teams ORDER BY homestate = '$states' DESC` – piotrm May 29 '14 at 10:17
  • Please be aware that the mysql extension (supplying the mysql_ functions) has been deprecated since 2012, in favor of the mysqli and PDO extensions. It's use is highly discouraged. See http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Oldskool May 29 '14 at 12:15

3 Answers3

0
$sql = mysql_query("SELECT clubName FROM teams WHERE homeState = '$states'  order by homeState DESC");
0

Try this query,

$sql = mysql_query("SELECT clubName FROM teams WHERE homeState like "%'.$states.'%" 
        order by clubName asc ");
Jitendra Yadav
  • 896
  • 1
  • 6
  • 14
0

You still need to get all clubs with all matches on top.

Try this.

(SELECT clubName FROM teams WHERE homeState = '$state' ORDER BY clubName ASC)    
 UNION 
(SELECT clubName FROM teams WHERE homeState != '$state' ORDER BY clubName ASC)
noia_0328
  • 104
  • 4