Jump to content


Photo

just using one query


  • Please log in to reply
3 replies to this topic

#1 presso

presso
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 22 October 2006 - 07:54 PM

I am wondering what the best way of pulling the data i need out of one query where i need two different instances of the data. ie

$query =  $DB->query("SELECT name, location, date FROM table ORDER BY name DESC LIMIT 10");

now say i am using a query like that , the first output sorts the display by name , but what if i want to just a little further down the page want to display the same info but sorted by location , do i have to do another query or can i just use the first query. If somebody could point out the obvious for me would be great.

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 22 October 2006 - 08:14 PM

foreach ($query['location'] as $val) {
   $sortloc[] = $val;
}
sort($sortloc);

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 presso

presso
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 22 October 2006 - 09:55 PM

ahhhh , nice and easy , i knew it would be but could not quite figure it out , thanks for that  :)

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 October 2006 - 04:30 AM

although... if you have the exact same info further down the page, except sorted a different way, wouldn't it be better to make the list titles (name, location, date) links that the user can click on, and when they do, the page reloads, but runs the query with a different sort by in it? that way you would have less stuff taking up your page and less bandwidth being used (for having more stuff being displayed). example:

.
.
$titles = array('name','location','date');
$sortby = (in_array($_GET['sortby'],$titles)) ? $_GET['sortby'] : 'name';
$query =  $DB->query("SELECT name, location, date FROM table ORDER BY $sortby DESC LIMIT 10");
.
.
// use this in your list titles/headers/whatever you call it
echo "<a href = '{$_SERVER['PHP_SELF']}?sortby=name'>Name</a>";
.
.


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users