Required Help in MYSQL Query.............

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Required Help in MYSQL Query.............

Post by manojsemwal1 »

iam using the following the query ....
SELECT district,block, SUM( IF( Approval = 'D', 1, 0 ) ) AS `Direct` , SUM( IF( Approval = 'C', 1, 0 ) ) AS `Complete` , SUM( IF( Approval = 'A', 1, 0 ) ) AS 'Approved' FROM studentregdtb GROUP BY block, District"

in query result is district iterate n times , i want to print the district only once time when the district value change it print and again break.
query.JPG
query.JPG (17.83 KiB) Viewed 578 times
see the image

how we can do it in php.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Required Help in MYSQL Query.............

Post by jayshields »

You'll just need to use PHP to help you with your formatting.

For example, loop through the query, storing the district each time, if the district is different from the last one, break and print it - if it's not different from the last one just print the row without the district.
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: Required Help in MYSQL Query.............

Post by manojsemwal1 »

Thanks for your reply .....
i tried in php but its not print properly coding are

while($row = mysql_fetch_array($sql))
{
// print_r($row);
echo"<tr ><td class='x-blue_dg_td dg_center dg_nowrap' > $row[district]</td>";
<td class='x-blue_dg_td dg_center dg_nowrap' >$row[block]</td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$row[Direct]</td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$row[Complete]</td>
<td class='x-blue_dg_td dg_center dg_nowrap' >$row[Approved]</td>";
}
User avatar
SimpleManWeb
Forum Commoner
Posts: 57
Joined: Wed Dec 30, 2009 4:15 pm
Location: New Hampshire, USA

Re: Required Help in MYSQL Query.............

Post by SimpleManWeb »

Try something like this:

Code: Select all

 
     $LastDistrict = "";
     while($row = mysql_fetch_array($sql)) {
        if ($row['district] != $LastDistrict) {
            ?>
                 <tr ><td class='x-blue_dg_td dg_center dg_nowrap' colspan="4" > $row[district]</td>
            <?php
           $LastDistrict = $row['district'];
        }
        ?>
          <tr>
              <td class='x-blue_dg_td dg_center dg_nowrap' ><?php echo $row[block]; ?></td>
              <td class='x-blue_dg_td dg_center dg_nowrap' ><?php echo $row[Direct]; ?></td>
              <td class='x-blue_dg_td dg_center dg_nowrap' ><?php echo $row[Complete]; ?></td>
              <td class='x-blue_dg_td dg_center dg_nowrap' ><?php echo $row[Approved]; ?></td>
          </tr>
      }
 
I didn't test that code, but it should work.

Hope this helps
manojsemwal1
Forum Contributor
Posts: 217
Joined: Mon Jun 29, 2009 4:13 am
Location: India

Re: Required Help in MYSQL Query.............

Post by manojsemwal1 »

Thanks Dude its work fine...................

i appreciate your work........be in touch.........

Thanks
Post Reply