?
Solved

displaying records horizontally and vertically

Posted on 2005-03-14
8
Medium Priority
?
249 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:abenbow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13535152
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13535163
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13535181
And maybe the <TR> and </TR> tag also should be in the while -loop and not outside.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:abenbow
ID: 13535254
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
 
LVL 32

Accepted Solution

by:
Batalf earned 2000 total points
ID: 13535278
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
 

Author Comment

by:abenbow
ID: 13535467
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
 

Author Comment

by:abenbow
ID: 13535483
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
 
LVL 32

Expert Comment

by:Batalf
ID: 13535504
Glad I could help!

Thanks for the "A" grade

Batalf
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question