Current location: Hot Scripts Forums » Programming Languages » PHP » Filter the results from a recordset


Filter the results from a recordset

Reply
  #1 (permalink)  
Old 09-17-09, 12:45 PM
dmrgraphics dmrgraphics is offline
New Member
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Filter the results from a recordset

This is for a schedule of people serving on a particular day. Just to pick a day I will choose Oct 4. I have a table called 'Oct 4' that has two fields 'department' and 'name'. There are like 12 different departments and each has at least 2 names of people that are serving in that department on any particular day.

The simple way to do this would be to just display department & name for each record, sort it by the department name, and list them down the page. However if there is multiple people serving in one department I only want to show the department heading once, then the names. I don't really need to get the department headings from the database, those can just be on the page itself.

The code below is what I am using for one section of this 'Childrens Ministry'. It works, except it is only showing one result under one heading. If I put this inside a repeat region, it will repeat the whole entire list of headings, including the next record in the list.

Code:
<div align="center"><strong>CHILDREN</strong><br />
Nursery: <br />
<?php

if($row_Recordset1['department'] == 'Nursery') {

echo $row_Recordset1['names']."</p>";

}?>
<br />
Baby Ministers:<br />
<?php

if($row_Recordset1['department'] == 'Baby Minister') {

echo $row_Recordset1['names']."</p>";

}?>
<br />
Pre Wee Church<br />
<?php

if($row_Recordset1['department'] == 'Pre Wee Church') {

echo $row_Recordset1['names']."</p>";

}?>
<br />
Wee Church<br />
<?php

if($row_Recordset1['department'] == 'Wee Church') {

echo $row_Recordset1['names']."</p>";

}?>
<br />
Childrens Church<br />
<?php

if($row_Recordset1['department'] == 'Childrens Church') {

echo $row_Recordset1['names']."</p>";

}?>
</div>
It seems like it should work but it isn't.
Reply With Quote
  #2 (permalink)  
Old 09-17-09, 01:19 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
PHP Code:

Childrens Church<br />

<?php

if($row_Recordset1['department'] == 'Childrens Church') {

echo 
$row_Recordset1['names']."</p>";

}
?>
... can be written more compactly like so:

PHP Code:

echo '<p><strong>'.$row_Recordset1['department'].'</strong><br />'.$row_Recordset1['names']."</p>"
You could put it in a loop like so:

PHP Code:

while ($row_Recordset1=mysql_fetch_array($rResult))

  echo 
'<p><strong>'.$row_Recordset1['department'].'</strong><br />'.$row_Recordset1['names']."</p>"
Reply With Quote
  #3 (permalink)  
Old 09-17-09, 01:27 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
wire got it

Last edited by carters-site; 09-17-09 at 01:28 PM. Reason: removed code.
Reply With Quote
  #4 (permalink)  
Old 09-18-09, 11:49 AM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
With Wirehopper's code, it will show the Department, and the name for every database entry. But, you want it to show the department only once, and then the names for that department, correct?

if so: (UNTESTED, but should work)

PHP Code:

while ($row_Recordset1=mysql_fetch_array($rResult)) {
    
$storage[$row_Recordset1['department']][$row_Recordset1['names']];
    }
    
foreach(
$storage as $dept => $name) {
    echo 
'<p><strong>' $dept '</strong></p>';
    foreach(
$name as $person => $empty) {
        echo 
'<p>' $person '</p>';
    }

Reply With Quote
  #5 (permalink)  
Old 09-18-09, 02:46 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Or:

PHP Code:

$storage[$row_Recordset1['department']][]=$row_Recordset1['names']; 

If this approach is used, the GROUP BY or ORDER BY on the query should include department.

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
if/else failing when query produces zero results simcomedia PHP 5 01-04-08 12:22 PM
Table filter results FINESTICE PHP 2 09-10-07 07:19 PM
Recordset filter plumbjet ASP 6 05-09-06 02:14 PM
filter results cooc PHP 1 08-25-04 05:44 AM
posting of results script Sir-ihlatrebon Script Requests 0 02-17-04 11:46 AM


All times are GMT -5. The time now is 07:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.