RE: Accessing data from next row? (mysql)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I just did something similar where I had a left join
that returns several rows for each record. So to make
sure that each record is outputted once and contains
the correct 'categories' at the end of the while i
store the current $id as $prev_id. This way I can
check to see if we're working on the same listing
($id==$prev_id) and just adding a category or if it is
a new listing ($id!=$prev_id).


table listings contains company info
table categories contains the categories that they may
beloing to 
table l_categories ties the two together

QUERY:
SELECT * FROM listings l
	LEFT JOIN l_categories lc ON l.l_id = lc.l_id
	LEFT JOIN categories c ON lc.cat_id = c.cat_id
	WHERE l.b_id = '.$_SESSION[b_id].'
	ORDER BY l.l_id

	$prev_id = '';
	while ($row = mysql_fetch_array($result))
	{
		if ($row[l_id] == $prev_id)
		{
			$listing .= '- '.$row[category].'<br>';
		}
		elseif ($row[l_id] != $prev_id)
		{
			if ($prev_id != '')
			{
				// not first listing
				$content .= $listing.'</td></tr>
				<tr><td>[<a
href="edit.php?l_id='.$prev_id.'">Modify</a>]
				</td></tr>
				<tr><td>&nbsp;</td></tr>';
				$listing = '';
			}
			
			$listing .= '<tr><td bgcolor="#EEEEEE">
			<strong>'.$row[l_name].'</strong>
			</td></tr>
			<tr><td>
			'.$row[l_address].'<br>
			'.$row[l_city].', '.$row[l_state].'
'.$row[l_zip].'<br>
			<strong>Phone:</strong>
'.format_phone($row[l_phone]).'<br>
			<strong>Toll-Free:</strong>
'.format_phone($row[l_tollfree]).'<br>
			<strong>Fax:</strong>
'.format_phone($row[l_fax]).'<br>
			<strong>Email:</strong> '.$row[l_email].'<br>
			<strong>Description:</strong>
'.$row[l_description].'<br>
			<strong>Categories:</strong><br> -
'.$row[category].'<br>';
		}

Then after the while loop I do this to finish off the
list and provide the edit link

	$content .= $listing.'</td></tr>
	<tr><td>[<a
href="edit.php?l_id='.$prev_id.'">Modify</a>]
	</td></tr>
	<tr><td>&nbsp;</td></tr>';
	$listing = '';

olinux


--- "Snijders, Mark" <Mark.Snijders@atosorigin.com>
wrote:
> if you want to access stuff from row 3 while being
> in row 2.. you first have
> to put it all into an array.. and then you coul do
> that.. but i can't comup
> with something where you need something like that..
> a strange idea...?
> 
> -----Original Message-----
> From: Leif K-Brooks
> [mailto:eurleif@buyer-brokerage.com]
> Sent: maandag 14 oktober 2002 9:47
> To: holmes072000@charter.net
> Cc: php-db@lists.php.net
> Subject: Re:  Accessing data from next row?
> (mysql)
> 
> 
> I know that, but what if I need to access data from
> the next row?  If I 
> have 3 rows, is there a way to access something from
> row 3 while the 
> loop is in row 2, but still have the loop go to row
> 3 when it's done 
> with row 2?
> 
> John W. Holmes wrote:
> 
> >>Using mysql, how do I access the data of the next
> row using code
> >>something like this:
> >>$result = mysql_query("select column from table
> where
> >>whatever='whatever'");
> >>while($array = mysql_fetch_array($result)){
> >>//Whatever
> >>}
> >>    
> >>
> >
> >Each iteration of the while loop will fetch a row
> for you and it's
> >contents will be in the $array[] array. Try "echo
> $array['column'];"
> >inside of your while to see what I'm talking about.
> >
> >---John Holmes...
> >
> >
> >
> >  
> >
> 
> -- 
> The above message is encrypted with double rot13
> encoding.  Any
> unauthorized attempt to decrypt it will be
> prosecuted to the full extent
> of the law.
> 
> 
> 
> 
> -- 
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 


__________________________________________________
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos & More
http://faith.yahoo.com

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux