Jump to content

php sorting


astani

Recommended Posts

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">

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.