| revisiting older topic- drop down menu email
|
Gilead

msg:4400639 | 8:12 pm on Dec 22, 2011 (gmt 0) | I heard back from the client yesterday that when it comes to the "email details" page, they want it sorted out by first name. Code: $sql=mysql_query("SELECT contactid, firstname, lastname FROM $table_name"); echo'<select name="ContactID">'; while ($row=mysql_fetch_assoc($sql)) { $id= htmlentities($row['contactid']); $firstname= htmlentities($row['firstname']); $lastname= htmlentities($row['lastname']); echo "<option value= '$id' >"; echo "$firstname$lastname "; echo "</option>"; } echo '</select>'; ?> <br /> <input type="submit" name="submit" value="Send Details"> </form> Choose which user you want to send details to. This does work, but I have tried ORDER BY firstname ASC. That didn't work. I tried moving the fields around so first name is first. What am I missing? Thanks!
|
Matthew1980

msg:4400665 | 9:38 pm on Dec 22, 2011 (gmt 0) | Hi there Gilead, You're so close! Order by is the correct syntax to use, and this will sort the chosen field (in this case FirstName) into alphabetical order either ASCending or DESCending, so you'll need to alter that variable accordingly:- $query = "SELECT `contactid`, `firstname`, `lastname` FROM `".$table_name."` ORDER BY `firstname` DESC"; $sql = mysql_query($query); echo "<select name=\"ContactID\">"; while ($row=mysql_fetch_assoc($sql)){ $id = htmlentities($row['contactid']); $firstname = htmlentities($row['firstname']); $lastname = htmlentities($row['lastname']); echo "<option value = "\".$id."\" >"; echo $firstname." ".$lastname; //optional space between the names echo "</option>"; } echo "</select>"; ?> I've reformatted the way the concatenations were done so that everything is uniform - good coding practice.. Hopefully if you run this now you'll get the results as you're expecting. Cheers, MRb
|
Gilead

msg:4400922 | 4:27 pm on Dec 23, 2011 (gmt 0) | Matt- What separates '' from ``? Should I do that for all query calls? It did work BTW- Thanks!
|
rocknbil

msg:4400935 | 5:09 pm on Dec 23, 2011 (gmt 0) | The operative difference is this: ORDER BY firstname DESC In case you missed it, the desc (descending) does the "z"'s first. Use asc if you want it abc.... you can also order by lastname and first name order by lastname asc, firstname asc The backticks in mysql queries are recommended but not always needed. They allow you to use names with spaces, or names that may conflict with internal functions or reserved words. For example, date_timestamp() is a mysql function, if you have a field named date_timestamp your queries would likely fail, but using backticks would allow it. Look at them like a "safety wrapper" for table and field names. Quotes are not for db/table/field references, they are for non-numeric values. select * from `database_name`.`mytablename` where `database_name`.`mytablename`.`fieldname`='testme'; Whether to use them or not . . . for beginners, it gives you one more pair of string values to debug. The correct answer is probably yes, but they are not really needed in most cases.
|
Gilead

msg:4400937 | 5:15 pm on Dec 23, 2011 (gmt 0) | Thanks Bil
|
Matthew1980

msg:4400950 | 5:51 pm on Dec 23, 2011 (gmt 0) | Hi all, Thanks for letting me know that your code now functions as expected; wrt `` || '' this is just a preferential thing, I personally advocate this method of compatibility, and Rocknbil's explanation is correct (thanks for the clarity there Rocknbil) it's not generally needed it just provides the user with a way of making their code a little more versitile. Though really when designing a DB you should try to name the column names pertinant to their purpose, I usually prefix them with the acronym of the project with a following underscore: MFP_date_timestamp - but habit is hard to break so I usually end up with this: `MFP_date_timestamp` <<My First Project. But that example is just my preference, it's now upto you as to how you develop your programming style. As Rocknbil states "The correct answer is probably yes, but they are not really needed in most cases. " Anyway, happy coding & seasons greetings too :) Cheers, MRb
|
|
|