Jump to content

[SOLVED] While loop question.


zhTonic

Recommended Posts

hey guys,

 

I'm coming up to a part in my application where i will have to pull multiple results for a category, but the category is part of the query itself.

 

It's structured like this.

 

CATEGORY

result

 

CATEGORY

result

 

I have no problem with looping that, but my question is what if i have more then one "result"? How can i make it loop more then one result under a category without it making a new

 

CATEGORY

result

 

every time?

 

ideally it would look like this..

 

CATEGORY

result

result

result

 

CATEGORY

result

result

Link to comment
Share on other sites

Ok sure here is the query and my while loop, i'll also explain a bit about how it's getting the appropriate data.

 

SQL QUERY:

$sellsql2 = mysql_query("SELECT * FROM auctions WHERE selling='".$sbcheck."' AND serverid='".$serverid."' AND itemtype='".$itemid."' AND jobclass='".$jobclass."'");

 

Ok now basically it reads from all of the auctions and each auction will have an itemid which will grab the according data from the items table.

 

Now lets say i have 4 auctions that have the same exact item, we'll call it "Big Axe".

 

Well what i want to do is if there is more then one i don't want it to loop it and have it like this:

 

Big Axe

auction1

 

Big Axe

auction2

 

etc..

 

I would like it to be

 

Big Axe

auction1

auction2

auction2

 

etc.

 

and if there are more then one type of item up for auction it would display like this:

 

Big Axe

auction1

auction2

 

Mega Axe

auction1

 

and so on...

 

 

while($sellsqlly1 = mysql_fetch_array($sellsql2)){
$itemid = $sellsqlly1["itemid"];
$itnfosql = mysql_query("SELECT * FROM items WHERE id='".$itemid."'");
$itnfosqlly = mysql_fetch_array($itnfosql);
//ITEM RESULTS//
$itemname = $itnfosqlly["name"];
//AUCTION RESULTS//
$stats = $sellsqlly1["stats"];
$price = number_format($sellsqlly1["price"]);
$topbid = number_format($sqllsqlly1["topbid"]);
$auctioner = $sellsqlly1["Auctioner"];
if(!$topbid){
$topbid = "";
} else {
$topbid = $topbid;
}
echo "<tr class=\"misc1 normal\">
<td colspan=\"5\">
<a name=\"an21679\"></a>
<a href=\"?action=auctions\"><img border=\"0\" src=\"item/item-1679-1.gif\" title=\"$itemname\" align=\"absmiddle\" class=\"mr10\"/>
<strong>$itemname</strong></a>
</td>
</tr>

<tr>
<td class=\"normal\"><a href=\"?action=auctions\">$stats </a></td>
<td class=\"ps2 normal\"><a href=\"?action=auctions\">$price</a></td>
<td class=\"ps1 normal\"><a href=\"?action=auctions\">$topbid</a></td>
<td class=\"center ps2 small\">1 hour, 38 minutes</td>
<td class=\"normal small\"><a href=\"?action=basilid&user=$auctioner\">$auctioner</a></td>
</tr>
<tr><td colspan=\"5\" class=\"sep\"></td></tr>";

}

Link to comment
Share on other sites

First, add a group by category/itemtype/whateveritisi'mtolazytocheckyourtabledesignsorry to the query

 

I use "category" in the code below for the category. eg. big axe

I use "item" in the code below for whatever value you want.

 

Then, the general idea of what you'd want to do is this... (untested)

 

<?php
// query and assign the result to $result

$category = 0;

while($row = mysql_fetch_assoc($result)) {
if($category != $row['category']) {
	echo '<b>' . $row['category'] . '</b><br />';
	$category = $row['category'];
}
echo $row['item'] . '<br />';
}
?>

Link to comment
Share on other sites

There are a lot of ways to do it  a group by is not the right way because GROUP BY is meant to be used to GROUP items ie when you go to find MAX(Field)  based by UserID  you use group by not in this case

 

 

If your categories is a secondary table you should use a LEFT JOIN here to join the auctions into the cat's table.

 

If not you should use an ORDER BY category and then you are all set to get they are in order by category.  You will need to use a logical statement in the php in the while loop to see if the category changes from each itteration if it does splash out the category heading.

Link to comment
Share on other sites

There are a lot of ways to do it  a group by is not the right way because GROUP BY is meant to be used to GROUP items ie when you go to find MAX(Field)  based by UserID   you use group by not in this case

 

 

If your categories is a secondary table you should use a LEFT JOIN here to join the auctions into the cat's table.

 

If not you should use an ORDER BY category and then you are all set to get they are in order by category.  You will need to use a logical statement in the php in the while loop to see if the category changes from each itteration if it does splash out the category heading.

 

What would the logical statement look like? just to note that there would be thousands of category names depending on the itemid.

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.