465Media
465Media

Reputation: 31

Sorting Using PHP

I am a php novice so please be kind.... Need to sort date, after location has been selected.

    <div id="enroll_now">
  <form name="enroll" id="enroll" method="post" action="<?php echo get_permalink(762); ?>">
    <p>
      <label >Class Title</label>
      <input type="text" name="class_title" value="<?php echo $title;?>">
      </input>
    </p>
    <p>
      <label>Choose Location</label>
      <select name="location" id="location" >
        <option selected="selected" value="0">--Select Location--</option>
        <?php
        $result = mysql_query("SELECT * from wp_location WHERE class_id=$page_id ORDER BY location ASC");
        while($row = mysql_fetch_array($result))
        {
            echo "<option value=".$row['id'].">".$row['location']."</option>";

        }

    ?>
      </select>
    </p>
    <p>
      <label>Choose Date</label>
      <select name="choose_date" id="choose_date">
        <?php
        if (isset($datesavailable) && is_array($datesavailable)) {
        foreach($datesavailable as $val)
        {

        echo' <option value="'.$val.'">'.$val.'</option>\n';
 }
 }
    ?>
      </select>
    </p>
    <p>
      <input type="image" src="<?php bloginfo('template_url'); ?>/images/confirm.png" alt="Confirm Information" />
    </p>
  </form>
</div>

The locations sorts fine, but then when I get to the date, I need them to sort by closest date first. Any help would be appricated. Thanks.

Upvotes: 1

Views: 155

Answers (3)

Jorge Zapata
Jorge Zapata

Reputation: 2336

I examined your page and see that you're sending a POST (id & location) when the event change is triggered in your combo. My suggestion is to query the database like this

SELECT * from wp_location WHERE class_id=$_POST['id'] AND location=$_POST['location'] ORDER BY date

I'm assuming date field in your database is DATE, DATETIME or TIMESTAMP type.

Upvotes: 0

abruski
abruski

Reputation: 871

Use ORDER BY somecolumn in your query; http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

Upvotes: 1

smp7d
smp7d

Reputation: 5032

If the dates are just an array, you can use asort or arsort.

http://php.net/manual/en/function.asort.php

Upvotes: 0

Related Questions