while loop in PHP

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
Delphine
Forum Newbie
Posts: 3
Joined: Thu Oct 07, 2010 12:35 am

while loop in PHP

Post by Delphine »

hii all..

Am doing a while loop to display data from my table. below is the output am having:
It is in the form of a table where P and T are 2 different columns
P T
001 t001
001 t002
001 t003

I want to do it like this:
P T
001 t001
t002
t003

I want to display the value of P only once in its respective column.Please help..

Any help will be greatly appreciated.. Thx in advance

regards
Delphine
adil
Forum Newbie
Posts: 6
Joined: Wed Oct 13, 2010 3:46 am

Re: while loop in PHP

Post by adil »

plz paste here your while loop code then i can help you
Delphine
Forum Newbie
Posts: 3
Joined: Thu Oct 07, 2010 12:35 am

Re: while loop in PHP

Post by Delphine »

Here's the code for the while loop:

Code: Select all

while ($sqlRow=mysql_fetch_array($sqlResult))
{
?>
    <tr>
        <td>
    <?php
        
        echo $sqlRow['projectName'];

    ?>
        </td>
        <td><?php echo $sqlRow['taskName'] ?></td>
i want to display the projectName only once..
thx..
kalpesh.mahida
Forum Commoner
Posts: 36
Joined: Wed Oct 06, 2010 7:09 am

Re: while loop in PHP

Post by kalpesh.mahida »

Hi,

Bellow code snipest is the answer to your question

Code: Select all

$arrProjectInfo = array(
					'1'=>array('ProjectName'=>'001','ProjectTask'=>'t1001'),
					'2'=>array('ProjectName'=>'001','ProjectTask'=>'t2001'),
					'3'=>array('ProjectName'=>'001','ProjectTask'=>'t3001'),
					'4'=>array('ProjectName'=>'002','ProjectTask'=>'t1002'),
					'5'=>array('ProjectName'=>'002','ProjectTask'=>'t2002'),
					'6'=>array('ProjectName'=>'002','ProjectTask'=>'t3002'),
					'7'=>array('ProjectName'=>'003','ProjectTask'=>'t1003'),
					'8'=>array('ProjectName'=>'003','ProjectTask'=>'t2003'),
					'9'=>array('ProjectName'=>'004','ProjectTask'=>'t1004')
					);
$currentProjectName='';
$previousProjectName='';
echo '<table>';
foreach ($arrProjectInfo as $key => $value):
	$currentProjectName = $value['ProjectName'];
	echo '<tr>';
		echo '<td>';
			if($previousProjectName != $currentProjectName):
				echo $value['ProjectName'];
				$previousProjectName = $currentProjectName;
			endif;
		echo '</td>';
		echo '<td>';
			echo $value['ProjectTask'];
		echo '</td>';
	echo '</tr>';
	
	if($previousProjectName=='')$previousProjectName=$currentProjectName;
endforeach;
echo '</table>';
Hope this will help you.

Kalpesh Mahida
Post Reply