Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

displaying records horizontally and vertically

Posted on 2005-03-14
8
Medium Priority
?
255 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

577 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