Help with Loop count?

I have this SQL query that gathers set information for the Salesperson($user) that is logged in and viewing theri need information.
Everything works weell, except my Loop Count.

I don’t know, what I am doping incorrectly. It returns only two values which are the same.

But the Query returns 10 values for that user. When I run the query direct in SQL DB.

What am I missing?







  <div class='page_content clearfix'>
                  <div class='grid_8 first'>
				




<?php



$salesPersonId = & JFactory::getUser()->get('username');
//$merchantId = JFactory::getSession()->get('merchantId');  
$db =& JFactory::getDBO();
$query = "	SELECT XXX_merchant.*, XXX_merchant.name as MercName, XXX_merchant.address as MercAddress 
			FROM XXX_sales_person, jXXX_merchant 
			WHERE XXX_sales_person.user_name = '$salesPersonId'
			AND XXX_merchant.sales_person_id = XXX_sales_person.id";
$db->setQuery( $query );
$row  = $db->loadObject();
//echo $query;


//echo $query;
//echo '<br>';
//echo $salesPerson->name;


?>
<?php
   

	$link = 'index.php?option=XXX&controller=deal&task=view&id=' . $row->id;
	
	$count = 0; 
        foreach ( $row as $row): 



		$link = '/index.php?option=XXX&controller=merchant&task=edit&cid[]=' . $row->id;
?>

 <div class='quickie_login grid_9 last' style="width: 140px">
                    <div class='text_center' id='login_banner'>
                      <div class='page_content text_center'>
					      <div class="avatar" style="float: left; margin-top: -10px; padding: 0px;">
                          <a class="k2Avatar ubAvatar" href="<?php echo $link; ?>" title="<?php echo JText::_($row->MercName); ?> Edit Account" target="_blank">
<span class="ubName" style="display: block;  margin-top: -20px;"><b><?php echo $row->MercName; ?></b> </span>				
		<?php 
		if(!empty($row->logo_url))
		{
			$imagePathArr = unserialize(urldecode($row->logo_url));
			$link='http://';
			$link.= $_SERVER["SERVER_NAME"].$link_server.DS;
			$link.=$imagePathArr[0];
			$link =str_replace("\\\\","/",$link);
			
		?>
			<img src='<?php echo $link; ?>' alt="<?php echo $row->name; ?>" style="max-width: 100px; height:100px;"/>
		<?php 
	}
		?>



</a>
</div>


<div class="sales-system2">

<div style="display: block; margin-top: 0px;">

</div>
</div>		


</div>
</div>


<?php endforeach;?>






I’m not really clear on what you are asking but it may be because you are not incrementing $count at the end of the foreach loop.

Add $count++ before the closing bracket.

Found your code really difficult to understand without putting it into some kind of readable layout first:


<div class="page_content clearfix">
<div class="grid_8 first">

<?php
$salesPersonId = & JFactory::getUser()->get('username');
//$merchantId = JFactory::getSession()->get('merchantId');  
$db =& JFactory::getDBO();
$query = "SELECT XXX_merchant.*, XXX_merchant.name as MercName,
		XXX_merchant.address as MercAddress 
		FROM XXX_sales_person, jXXX_merchant 
		WHERE XXX_sales_person.user_name = '$salesPersonId'
		AND XXX_merchant.sales_person_id = XXX_sales_person.id";
$db->setQuery( $query );
$row  = $db->loadObject();
//echo $query;
//echo '<br>';
//echo $salesPerson->name;

$link = 'index.php?option=XXX&controller=deal&task=view&id=' . $row->id;
$count = 0; 
foreach ($row as $row): 
	$link = '/index.php?option=XXX&controller=merchant&task=edit&cid[]=' . $row->id;
?>
<div class="quickie_login grid_9 last">
	<div class="text_center" id="login_banner">
		<div class="page_content text_center">
			<div class="avatar">
      	<a class="k2Avatar ubAvatar" href="<?php echo $link; ?>" title="<?php echo JText::_($row->MercName); ?> Edit Account" target="_blank">
					<span class="ubName"><b><?php echo $row->MercName; ?></b></span>				
					<?php 
					if(!empty($row->logo_url)) :
						$imagePathArr = unserialize(urldecode($row->logo_url));
						$link='http://';
						$link.= $_SERVER["SERVER_NAME"].$link_server.DS;
						$link.=$imagePathArr[0];
						$link =str_replace("\\\\","/",$link);
					?>
					<img src="<?php echo $link; ?>" alt="<?php echo $row->name; ?>"/>
					<?php endif; ?>
				</a>
			</div>
			<div class="sales-system2">
				<div></div>
			</div>		
		</div>
	</div>
</div>
<?php
$count++;
endforeach;
?>

I’ve added a missing closing div tag on line 47, and incremented the count at line 49. I’ve also removed the inline styles. They should be on a separate stylesheet really, which also makes the code easier to read. Plus it’s probably best to use double quote marks for HTML attributes, or at least stick to one convention, rather than using single quotes sometimes and double quotes at other times.

Also by wrapping php code in php tags rather than code tags you get nice colours to help you see what’s going on.

Anyway, i hope that’s useful to you.