Forums

php loop inside body area

mja1356 04 May, 2009
Hi
I'm so confused about use of php loop inside body in CC.
if I use loop (eg for) outside of <tr> tag it double repeats everything.if i use loop inside <td> tag in table it repeats all fields in each row instead of assign each value to corresponded row.
what's the solution?
do lot nhap khau, annie, do boi nu, thoi trang tre em, thoi trang be trai, do dung cho me sau sinh, thoi trang cong so, vay lien cong so, ban buon quan ao, do lot[/url]
this is my code.

     <?php
        $db = mysql_connect("localhost", "joomla", "123456");
         mysql_select_db('joomla');
        $query="select distinct(cf_id),tip,HG from jos_chronoforms_betslip ";
        $result = mysql_query($query);
		
         $num = mysql_num_rows($result);
		
    
		 ?>
	 <tr dir='ltr'>
    
      <td>{cat}</td>
      <td>{recordtime}</td>
      <td>{game}</td>
	  <td>
	  
	
		<?php
		for($i = 0; $i < $num; $i ++)	
       {
		 $t_rows = mysql_fetch_row($result);
		   if ( $t_rows[2]=='1')
		   {
		   
            echo "{host}";
          } else {
            echo "{guest}";
          }
		}
     
    ?>
	</td>
	  
 <td>{tip}</td>
  <td>{win}</td>
   <td>{loss}</td>
    <td>{ex1}</td>
	 <td>{odd}</td>
	 <td>
	 <?php
      
		
		 echo $t_rows[10];
		 /*
		   if ( $t_rows[10]=='full' || $t_rows[10]=='Over/Under')
		   {
		
            echo "{ftga}-{ftgb}";
          } else {
		
            echo "{htga}-{htgb}";
          }
	*/	 
      
    ?>
	</td>

 <td>{stats}</td>
      </tr>
	  
mja1356 04 May, 2009
Hi
I appreciate a simple example of how to use loop(for,foreach,...) inside chronoconnectivity body area
GreyHead 04 May, 2009
Hi mja1356,

I think that the loop code is repeated for each record identified by the WHERE clause.

By adding your own database query inside the loop it will be repeated for each record.

So, basically - don't use a for each inside the loop!

Bob
mja1356 04 May, 2009
thanks Bob
But if i don't use any loop inside the code my variables repeat in all records.
look at this code:

     <?php
        $db = mysql_connect("localhost", "joomla", "123456");
         mysql_select_db('joomla');
        $query="select HG,tip from jos_chronoforms_betslip ";
        $result = mysql_query($query);
		
         $num = mysql_num_rows($result);
		 $t_rows = mysql_fetch_row($result);
    
		 ?>
	 <tr dir='ltr'>
    
      <td>{cat}</td>
      <td>{recordtime}</td>
      <td>{game}</td>
	  <td>
	  
	
		<?php
		
		
		   if ( $t_rows[0]=='1')
		   {
		   
            echo "{host}";
          } else {
            echo "{guest}";
          }
		
     
    ?>
	</td>
	  
 <td>{tip}</td>
  <td>{win}</td>
   <td>{loss}</td>
    <td>{ex1}</td>
	 <td>{odd}</td>
	 <td>
	 <?php
      
		
		 echo $t_rows[1];
		 /*
		   if ( $t_rows[1]=='ft' || $t_rows[1]=='Over/Under')
		   {
		
            echo "{ftga}-{ftgb}";
          } else {
		
            echo "{htga}-{htgb}";
          }
	*/	 
      
    ?>
	</td>

 <td>{stats}</td>
      </tr>
	  


$t_rows[0] and [1] repeat in all records with value of first record.
GreyHead 04 May, 2009
Hi mja1356,

As I said "By adding your own database query inside the loop it will be repeated for each record."

That's exactly what is happening. You need to do your databse lookup using the WHERE box outside the loop. Or if you need to check a second database table (I don't think that you do) then do that in the header.

Bob

PS For security please use Joomla database code.
mja1356 05 May, 2009
Hi
I didn't understand your post reply , yes CC use a loop.and that problem solved.
but another problem.
I use this code in body area

<?php
		
		$host='{HG}';
		echo $host;
		if ($host=1)
		echo 'hi';
		elseif ($host=2)
		echo 'bye';
     
    ?>

the $host variable change in every record and works fine but the if condition doesn't work.
I mean in record that $host=2 the elseif statement doesn't work and write 'hi' in output.
mja1356 06 May, 2009
Hi
sorry for repeating my problem๐Ÿ˜Ÿ ๐Ÿ˜Ÿ ๐Ÿ˜Ÿ but probably i must declare it clearer.
this is my code in body area
     
	 <tr dir='ltr'>
    
      <td>{cat}</td>
      <td>{recordtime}</td>
      <td>{game}</td>
	  <td>
	  
	
		<?php
		
		$host="{HG}";
		echo $host;
		if ($host=2)
		echo 'hi';
		elseif ($hoost=1)
		echo 'bye';
   
    ?>
	</td>
	  
 <td>{tip}</td>
  <td>{win}</td>
   <td>{loss}</td>
    <td>{ex1}</td>
	 <td>{odd}</td>
	 <td>
	 <?php
      $tip='{tip}';
		echo $tip;
		   if ( $tip=='ft' || $tip=='Over/Under')
		   {
		
            echo '{ftga}-{ftgb}';
          } else {
		
            echo '{htga}-{htgb}';
          }
 
      
    ?>
	</td>

 <td>{stats}</td>
      </tr>
	  


my problem is that if statement doesn't work .I mean when I echo $host variable it changes in every record in output but it doesn't execute php code inside if statement.
I can do this with CF very easily but because i have many records in DB i need CC pagination to limit output.
anyway can i use some kind of pagination in CF?๐Ÿคจ
This topic is locked and no more replies can be posted.