astani Posted September 15, 2008 Share Posted September 15, 2008 Hi, I am a php newbe I have a Posgresql db and am trying to do a sort routine based on project titles or project owners. I have an html dropdown for this. I have an array that displays the letters of the alphabet and would like to be able to pick from the dropdown and populate the names of projects alphabetically or names of projects by creator. Here is what I have so far, the queries don't seem to work or maybe just not displaying, any help would be apreciated. <h4 align="left">Sort By: </h4> <form name="jump"> <p align="left"> <select name="menu"> <option value="URL"> Project Name</option> <option value="URL"> Owner</option> </select> <?php $sort_query = pg_query("SELECT * FROM projects ORDER BY title"); $projByTitle = pg_fetch_array($sort_query); $sort_query1 = pg_query("SELECT * FROM users ORDER BY username"); $projByOwner = pg_fetch_array($sort_query1); ?> <input type="button" name="<?php if (jump.menu.selectedIndex == "0") { echo "$projByTitle"; } else { echo "$projByOwner"; } ?> " method = "post" value="GO"> Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/ Share on other sites More sharing options...
Maq Posted September 15, 2008 Share Posted September 15, 2008 This isn't PHP is it? You put it in <?php tags. Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-642247 Share on other sites More sharing options...
btherl Posted September 16, 2008 Share Posted September 16, 2008 I don't think pg_fetch_array() means what you think it means. It fetches a single row of data. For example: ?php $sort_query = pg_query("SELECT * FROM projects ORDER BY title"); while ($row = pg_fetch_array($sort_query)) { $projByTitle[] = $row; }?> Now $projByTitle will be an array of rows from the query. To check what you've fetched use this code: print "<pre>"; var_dump($projByTitle); print "</pre>"; I'm not sure what your next step should be as I'm unclear on what you want to do. Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-642465 Share on other sites More sharing options...
astani Posted September 17, 2008 Author Share Posted September 17, 2008 I was mixing in html and javascript I think, sorry for the confusion. I am a bit confused myself. Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-643718 Share on other sites More sharing options...
astani Posted September 17, 2008 Author Share Posted September 17, 2008 I don't think pg_fetch_array() means what you think it means. It fetches a single row of data. For example: ?php $sort_query = pg_query("SELECT * FROM projects ORDER BY title"); while ($row = pg_fetch_array($sort_query)) { $projByTitle[] = $row; }?> Now $projByTitle will be an array of rows from the query. To check what you've fetched use this code: print "<pre>"; var_dump($projByTitle); print "</pre>"; I'm not sure what your next step should be as I'm unclear on what you want to do. I am trying to sort and display results by project title or by owner (depending to what the user indicates in the dropdown). Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-643721 Share on other sites More sharing options...
btherl Posted September 18, 2008 Share Posted September 18, 2008 Aha. There's an issue there about the order in which things happen. A user can only select from a dropdown after the page has been displayed. Once they select from the dropdown you have two options 1. Use javascript to either sort the list or display an already sorted list 2. Have the user submit a form that tells php which list they want Option 1 is preferable from an interactive point of view, as the user doesn't have to reload the page. But option 2 does not require any javascript. I suggest you try a simpler page first. Make a page which has only a drop-down and two pieces of text, and make it so the drop-down displays one piece or the other. Once you can do that, you can modify that code to do what you want here. This is not really a postgres issue, so you might want to post again in the main PHP help forum. The reason is that you won't get much visibility here. Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-644321 Share on other sites More sharing options...
astani Posted September 18, 2008 Author Share Posted September 18, 2008 Aha. There's an issue there about the order in which things happen. A user can only select from a dropdown after the page has been displayed. Once they select from the dropdown you have two options 1. Use javascript to either sort the list or display an already sorted list 2. Have the user submit a form that tells php which list they want Option 1 is preferable from an interactive point of view, as the user doesn't have to reload the page. But option 2 does not require any javascript. I suggest you try a simpler page first. Make a page which has only a drop-down and two pieces of text, and make it so the drop-down displays one piece or the other. Once you can do that, you can modify that code to do what you want here. This is not really a postgres issue, so you might want to post again in the main PHP help forum. The reason is that you won't get much visibility here. Thanks for your help thus far, I will try option one and see how it goes. Quote Link to comment https://forums.phpfreaks.com/topic/124361-php-sorting/#findComment-644702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.