Page 1 of 1

while loop in PHP

Posted: Wed Oct 13, 2010 3:13 am
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

Re: while loop in PHP

Posted: Wed Oct 13, 2010 3:49 am
by adil
plz paste here your while loop code then i can help you

Re: while loop in PHP

Posted: Wed Oct 13, 2010 4:25 am
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..

Re: while loop in PHP

Posted: Wed Oct 13, 2010 5:13 am
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