Alphabetize list/menu when drawing list/menu items from db

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Alphabetize list/menu when drawing list/menu items from db

Post by cdickson »

Once again I think I am a victim of DMX's coding.

I'm trying to alphabetize the list/menu items that are drawn from one of the tables in the db. I can get the information into the menu, but I can't get the ORDER BY function to work. DMX generates this code:

Code: Select all

<option value="<?php echo $row_rsCategories['cat_desc1'] ?>"></option>
When I try to order the list alphabetically, I added to the code as follows:

Code: Select all

<option value="<?php echo $row_rsCategories['cat_desc1'] ORDER BY 'cat_desc1' ASC ?>"></option>
This code results in a parsing error message. I don't think the answer is too far off.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

you need to put

ORDER BY 'cat_desc1' ASC

on the end of your MySQL query, NOT in your PHP code.

the following

Code: Select all

<option value="<?php echo $row_rsCategories['cat_desc1'] ORDER BY 'cat_desc1' ASC ?>"></option>
is total nonsense.

I am guessing you want to display a number of options. You will need a while statement that itterates through each one of the row from the results.

Need to see more code to help you out thi.

Mark
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

The html/php code for the form that I created to conduct the search follows. The PHP was created by DMX.

Code: Select all

<form name="categorySearch" method="post" action="../category.php">
                    <table width="400" border="0" cellspacing="1" cellpadding="2">
                      <tr>
                        <td class="bodytext"><div align="left">Search by Business Category</div>
                          <div align="left"></div>                          <div align="left">
                          </div></td>
                      </tr>
                      <tr>
                        <td class="bodytext"><div align="left">
                          <select name="selectCategory">
                            <?php
do {  
?>
                            <option value="<?php echo $row_rsCategories['cat_desc1']?>"></option>
                            <?php
} while ($row_rsCategories = mysql_fetch_assoc($rsCategories));
  $rows = mysql_num_rows($rsCategories);
  if($rows > 0) {
      mysql_data_seek($rsCategories, 0);
	  $row_rsCategories = mysql_fetch_assoc($rsCategories);
  }
?>
                          </select>
                        </div><div align="left">
                        </div></td>
                      </tr>
                      <tr>
                        <td class="bodytext"><input type="submit" name="Submit2" value="Go"></td>
                      </tr>
                      <tr>
                        <td class="bodytext"><div align="center">
                        </div></td>
                      </tr>
                    </table>
                  </form>
Thanks for the help - it is frustrating being so new at this.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Where is the query?

Mark
cdickson
Forum Contributor
Posts: 120
Joined: Mon Apr 05, 2004 1:30 pm
Location: Michigan, USA

Post by cdickson »

Sorry. :oops:

Code: Select all

<?php 
foreach ($_GET as $key => $value) { 
   $_GET[$key] = trim($value); 
} 
mysql_select_db($database_hschamber, $hschamber);
$query_rsCategories = "SELECT * FROM Category1";
$rsCategories = mysql_query($query_rsCategories, $hschamber) or die(mysql_error());
$row_rsCategories = mysql_fetch_assoc($rsCategories);
$totalRows_rsCategories = mysql_num_rows($rsCategories);
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

change

Code: Select all

$query_rsCategories = "SELECT * FROM Category1";
to

Code: Select all

$query_rsCategories = "SELECT * FROM Category1 ORDER BY 'cat_desc1' ASC ";
Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try $query_rsCategories = "SELECT * FROM Category1 ORDER BY cat_desc1";

[edit] Beaten to it ;)
Post Reply