Page 1 of 1
Alphabetize list/menu when drawing list/menu items from db
Posted: Tue Apr 06, 2004 10:33 am
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.
Posted: Tue Apr 06, 2004 10:47 am
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
Posted: Tue Apr 06, 2004 11:00 am
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.
Posted: Tue Apr 06, 2004 11:04 am
by JayBird
Where is the query?
Mark
Posted: Tue Apr 06, 2004 11:22 am
by cdickson
Sorry.
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);
?>
Posted: Tue Apr 06, 2004 11:24 am
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
Posted: Tue Apr 06, 2004 11:25 am
by markl999
Try $query_rsCategories = "SELECT * FROM Category1 ORDER BY cat_desc1";
[edit] Beaten to it
