Jump to content

looping through a database to get results


svgmx5

Recommended Posts

This is probably a simple question yet i can't find the answer to it.

 

I have a database that is filled with items and each item has a category field ..so its the item id, item name and item category.

 

Now i want to be able to display all those items, but arrange them by category with out having to call various SQL queries.

 

i'm trying to make it look something like this:

 

CATEGORY

 

item, item, item, item

 

CATEGORY

 

item, item, item, item

 

and so on...

Link to comment
Share on other sites

Here's one way (off the top of my head)

<?php
$q = "select item_id, item, category from your_table order by category";
$rs = mysql_query($q);
$tmp = array();
while ($rw = mysql_fetch_assoc($rs)) {
    if (!array_key_exists($rw['category'],$tmp) {
        $tmp[$rw['category']] = array();
    }
    $tmp[$rw['category']][] = $rw['item'];
}
foreach ($tmp as $cat => $items) {
   echo $cat . "<br><br>" . implode(',',$items) . "<br><br>\n";
}
?>

 

Note: completely untested & no error checking coded.

 

Ken

Link to comment
Share on other sites

okay so i have another issue....

 

while thats working i'm trying to display all the items in a list enviornment. howoever i'm having problems understanding how

i can. i've tried using explode instead of implode, but it just returns an array.

 

 

This is the code i have right now

<?php
					$get_cats = "SELECT * FROM services ORDER BY industry";
					$run_get = mysql_query($get_cats) or die(mysql_error());
					$tmp = array();

					while($rw = mysql_fetch_assoc($run_get)){
						    if (!array_key_exists($rw['industry'],$tmp)) {
								$tmp[$rw['industry']] = array();
							}
							$tmp[$rw['industry']][] = $rw['category'];
					}

					foreach ($tmp as $cat => $items) {
					 	echo'
							<div class="resume_category_selection">
								<h3>'.$cat.'</h3>
								<table width="750" cellspacing="0" cellpadding="0" border="0">
									<tr>
										<td>
											<ul>
												<li>'. implode(',',$items) .'</li>
											</ul>
										</td>
									</tr>
								</table>
							</div>
							'; 
					}
				?>

 

with this all the elements are displayed in a single <li> tag

 

Like i said i've tried exploding it instead but i still can't get it to work

Link to comment
Share on other sites

i noticed i forgot to put the code of explode

foreach ($tmp as $cat => $items) {
						$services = explode(' ', $items);
					 	echo'
							<div class="resume_category_selection">
								<h3>'.$cat.'</h3>
								<table width="750" cellspacing="0" cellpadding="0" border="0">
									<tr>
										<td>
										<ul>
											<li>'.$services.'</li>
										</ul>
										</td>
									</tr>
								</table>
							</div>
							'; 
					}

 

Link to comment
Share on other sites

okay you guys...i just have one more problem that i need help with...i'm trying to put them all in a form..which i realized i should f stated from the beginning, but that wasn't my main plan...so, sorry about all this extra steps, i truly do appreciate the help..

 

anyway like i was saying i'm trying to make checkboxes with the name next to them.

 

I've been playing with it and i'm almost there, but not there yet, i'm doing something wrong but i can't find it

 

anyway this should be the last one!

foreach ($tmp as $cat => $items) {
echo'
<div class="resume_category_selection">
	<h3>'.$cat.'</h3>
	<table width="750" cellspacing="0" cellpadding="0" border="0">
		<tr>
			<td>
				<ul>
					<li><input type="checkbox" name="service" value="'.implode('"/>',$items).'"/>'. implode('</li><li><input type="checkbox" name="service" value="',$items).'</li>
				</ul>
			</td>
		</tr>
	</table>
</div>
'; 
}

 

 

 

Link to comment
Share on other sites

This one is slightly harder. You can't use a plain implode to do it, you have to use a foreach loop. Also, for checkboxes, all the "names" can't be the same unless you're using an array as the name:

<?php
echo'
<div class="resume_category_selection">
	<h3>'.$cat.'</h3>
	<table width="750" cellspacing="0" cellpadding="0" border="0">
		<tr>
			<td>
				<ul>
					<li>';
        foreach ($items as $item) {
            echo '<input type="checkbox" name="service[]" value="'. $item .'"/>' . $item .'<br />';
        }
        echo '</li>
				</ul>
			</td>
		</tr>
	</table>
</div>
'; 
}
?>

 

Ken

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.