Jump to content

Loop column after 2 results


coupe-r

Recommended Posts

Hello All.

 

Here is what I have:

 

Client ID            Company name              Client ID                Company name

 

I then need a script to echo 2 results per row then start a new row.

 

I've tried many things, but never could get a second row to appear.  Each row will have 4 columns.

 

Here is my code without the "guts"  My vars will be $id and $company. 

 

$result = mysql_query("SELECT * FROM client");

if(mysql_num_rows($result) < 1)

{

$results = 'You have 0 clients.';

}

else

{

$results = '';

$results .= '<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">';

$results .= '<tr>';

 

while($row = mysql_fetch_array($result))

{

 

}

$results .= '</table>';

}

 

Link to comment
https://forums.phpfreaks.com/topic/186347-loop-column-after-2-results/
Share on other sites

something like this should work

 

<?php
$result = mysql_query("SELECT * FROM client");
   if(mysql_num_rows($result) < 1)
   {
      $results = 'You have 0 clients.';
   }
  else
  {
     $results = '';
     $results .= '<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">';
     $results .= '<tr>';
     $i=1;
     while($row = mysql_fetch_array($result))
     {
        if ($i%2 == 0) echo "</tr><tr>";
        echo "<td>" . $row['fieldname'] . "</td>";
        $i++;
     }
     $results .= '</tr></table>';
  }   
      
?>

Thanks for the reply.

 

I had something like that before, but here is what it echos

 

10001 Company 1 10002 Disney Inc. 10004 a

 

It should only have 10001 and 10002 on that link and then a seperate line for 10004, but it does not create a new row.

 

Here is the code now:

 

$results = '';

$results .= '<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">';

$results .= '<tr>';

$i=1;

 

while($row = mysql_fetch_array($result))

{

if ($i%2 == 0)

{

echo "</tr><tr>";

}

 

$results .= "<td>".$row['client_id']."</td>";

$results .= "<td>".$row['client_company']."</td>";

 

$i++;

}

$results .= '</tr></table>';

it should have been

 

<?php
$results = '';
         $results .= '<table width="100%" border="0" cellpadding="0" cellspacing="0" align="center">';
         $results .= '<tr>';
         $i=0;

            while($row = mysql_fetch_array($result))
            {
               if ($i%2 == 0)
               {
                  $results .= "</tr><tr>";
               }
               
               $results .= "<td>".$row['client_id']."</td>";
               $results .= "<td>".$row['client_company']."</td>";
               
               $i++;
            }
         $results .= '</tr></table>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.