displaying records horizontally and vertically

right

trying to display my records in a 3 column table, and its not working!

well it is, I get the same 3 records in 1 row then it loops for ever!

code so far is

<?
//Retrieve data from database for links
            mysql_select_db($database_bc, $bc);
            $query_link_course = "select * from bc_links where link_type = 'course' order by link_type asc, link_name asc" ;
            $link_course = mysql_query($query_link_course, $bc) or die(mysql_error());
            $row_link_course = mysql_fetch_assoc($link_course);
            $totalRows_link_course = mysql_num_rows($link_course);            
                  

$num_cols = 3;
$num_rows = ($totalRows_link_course/$num_cols);

?>
<table>
<? while ($num_rows != 0) { ?>
      <tr>
            <? $i = 0;
            while ($row_link_course = mysql_fetch_assoc($link_course)){
                  while ($i != $num_cols){?>
            <td>
                  <a href="<? echo stripslashes($row_link_course[link_url]) ?>" target="_blank"><img src="images/banners/<? echo stripslashes($row_link_course[link_image]) ?>" alt="<? echo stripslashes($row_link_course[link_name]) ?>" border="0"></a>
            </td>
                  <? $i=$i+1;
                   }
             } ?>
      </tr>
      <? $num_rows = $num_rows-1; ?>
      <? } ?>
</table>

anyone point out where I have gone wrong (apart from getting up this morning)


cheers

Andy
abenbowAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BatalfCommented:
As far as I can see, you don't need the $i variable



<?
//Retrieve data from database for links
mysql_select_db($database_bc, $bc);
$query_link_course = "select * from bc_links where link_type = 'course' order by link_type asc, link_name asc" ;
$link_course = mysql_query($query_link_course, $bc) or die(mysql_error());
$row_link_course = mysql_fetch_assoc($link_course);
$totalRows_link_course = mysql_num_rows($link_course);          
               

$num_cols = 3;
$num_rows = ($totalRows_link_course/$num_cols);

?>
<table>
<? while ($num_rows != 0) { ?>
     <tr>
          <?
          while ($row_link_course = mysql_fetch_assoc($link_course)){
             ?>
              <td>
                   <a href="<? echo stripslashes($row_link_course[link_url]) ?>" target="_blank"><img src="images/banners/<? echo stripslashes($row_link_course[link_image]) ?>" alt="<? echo stripslashes($row_link_course[link_name]) ?>" border="0"></a>
              </td>
               <?
       
           } ?>
     </tr>
     <?
    } ?>
</table>
0
BatalfCommented:
Sorry, this is the right one. The previous will also go into an infinite loop.


<?
//Retrieve data from database for links
mysql_select_db($database_bc, $bc);
$query_link_course = "select * from bc_links where link_type = 'course' order by link_type asc, link_name asc" ;
$link_course = mysql_query($query_link_course, $bc) or die(mysql_error());
$row_link_course = mysql_fetch_assoc($link_course);
$totalRows_link_course = mysql_num_rows($link_course);          
               

$num_cols = 3;
$num_rows = ($totalRows_link_course/$num_cols);

?>
<table>
     <tr>
          <?
          while ($row_link_course = mysql_fetch_assoc($link_course)){
             ?>
              <td>
                   <a href="<? echo stripslashes($row_link_course[link_url]) ?>" target="_blank"><img src="images/banners/<? echo stripslashes($row_link_course[link_image]) ?>" alt="<? echo stripslashes($row_link_course[link_name]) ?>" border="0"></a>
              </td>
               <?
       
           } ?>
     </tr>
</table>
0
BatalfCommented:
And maybe the <TR> and </TR> tag also should be in the while -loop and not outside.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

abenbowAuthor Commented:
it works but it just gives me a 1 column table, I want 3 columns

ie

a b c
d e f
g h i

and so on
0
BatalfCommented:
Try this:

<?
//Retrieve data from database for links
mysql_select_db($database_bc, $bc);
$query_link_course = "select * from bc_links where link_type = 'course' order by link_type asc, link_name asc" ;
$link_course = mysql_query($query_link_course, $bc) or die(mysql_error());
$row_link_course = mysql_fetch_assoc($link_course);
$totalRows_link_course = mysql_num_rows($link_course);          
               

$num_cols = 3;
$num_rows = ($totalRows_link_course/$num_cols);

$counter=0;
?>
<table>
<TR>  
<?
while ($row_link_course = mysql_fetch_assoc($link_course)){
 $counter++;
 if($counter%$num_cols==0){
       if($counter>1)echo "</TR>";
       echo "<tr>";      
       
 }
 ?>
  <td>
       <a href="<? echo stripslashes($row_link_course[link_url]) ?>" target="_blank"><img src="images/banners/<? echo stripslashes($row_link_course[link_image]) ?>" alt="<? echo stripslashes($row_link_course[link_name]) ?>" border="0"></a>
  </td>
   <?        
}
?>
</table>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abenbowAuthor Commented:
that kind of works

its missing record 1 out but in a strange way

basically instead of

a b c
d e f
g h i

I'm getting

b c
d e f
g h i


If I set $counter=-1;

then I get

b c d
e f g
h i

so still missing record 1 but now the format is correct.


There is definitely something in record 1 (I checked)
0
abenbowAuthor Commented:
ahh! figured it

in the original there are 2

$row_link_course = mysql_fetch_assoc($link_course)

1 in my database string and 1 in your code so its losing the first record

this works!


thx very much :)

<?
//Retrieve data from database for links
mysql_select_db($database_bc, $bc);
$query_link_course = "select * from bc_links where link_type = 'course' order by link_type asc, link_name asc" ;
$link_course = mysql_query($query_link_course, $bc) or die(mysql_error());
$totalRows_link_course = mysql_num_rows($link_course);          
               

$num_cols = 3;
$counter=-1;
?>
<table>
<tr valign="top">  
<?
while ($row_link_course = mysql_fetch_assoc($link_course)){
      $counter++;
      if($counter%$num_cols==0){
          if($counter>1)echo "</tr>";
            echo "<tr>";      
       }
 ?>
  <td valign="top">
       <a href="<? echo stripslashes($row_link_course[link_url]) ?>" target="_blank"><img src="images/banners/<? echo stripslashes($row_link_course[link_image]) ?>" alt="<? echo stripslashes($row_link_course[link_name]) ?>" border="0"></a>
  </td>
   <?        
}
?>
</table>
0
BatalfCommented:
Glad I could help!

Thanks for the "A" grade

Batalf
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.