Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Display dynamic data horizontally

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
How do I display dynamic data horizontally. I want to display a talbe 4 cells across and 7 cells down.
Code:
 <table width="500" height="33" cellpadding="2" >
   
         
         <?php do { ?>
           
               <tr>  <td> <a href="bags.php?bag_brand_id=<?php echo $row_Bags['bag_brand_id']; ?>"><?php echo $row_Bags['bag_brand_title']; ?></a>
         </td>

            <?php } while ($row_Bags = mysql_fetch_assoc($Bags)); ?> </tr>
  </table>
This displays them in cells vertically.
 
You need to keep a counter and then add the appropriate html tags when it reaches 4. Like so:

Code:
<table width="500" height="33" cellpadding="2" >
   
         
         <?php 
[red]$counter=0;[/red]

do { 
[red]if($counter>=4){ echo "<tr>"; $counter=0;}[/red]

?>
           
                 <td> <a href="bags.php?bag_brand_id=<?php echo $row_Bags['bag_brand_id']; ?>"><?php echo $row_Bags['bag_brand_title']; ?></a>
         </td>

            <?php 
[red]
counter++;
if($counter>=4){echo "</tr>";}[/red]

} while ($row_Bags = mysql_fetch_assoc($Bags)); ?> </tr>
  </table>

Parts in red are what yu need to add to your code.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It does not seem to be working for me?
 
print "<html><body>\n";
print "<table width=\"500\" height=\"33\" cellpadding=\"2\" >\n";
for ($i = 0;$i <= mysql_num_rows($result);$i++)
{
print "<tr>\n";
print "<td><a href=\"bags.php?bag_brand_id=".mysql_result($result,$i,'bag_brand_id')."\">".mysql_result($result,$i,'bag_brand_title')."</a></td>\n";
$i++;
print "<td><a href=\"bags.php?bag_brand_id=".mysql_result($result,$i,'bag_brand_id')."\">".mysql_result($result,$i,'bag_brand_title')."</a></td>\n";
print "</tr>\n";
}
print "</table>\n";
print "</body></html>";


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
this prints every thing to screen, Is that what it is suppose to do?
 
this prints every thing to screen?

Well, it's suppose to generate a 2 column listing of the brand title hyperlinked to bags.php with its brand id.

My bad. You requested for 4 columns instead of 2. Just repeat the

Code:
$i++;
print "<td><a href=\"bags.php?bag_brand_id=". mysql_result($result,$i,'bag_brand_id'). "\">".mysql_result($result,$i,'bag_brand_title')."</a></td>\n";
print "</tr>\n";

another 2 times. Then again, maybe I'm not getting your requirements right.


--== Anything can go wrong. It's just a matter of how far wrong it will go till people think its right. ==--
 
here is my starter for ten:

i think things will be clearer if you reverse the looping structure (i hate end-conditionals anyway)

Code:
<?php 
//step one - get the recordset into an array
while ($row = mysql_fetch_assoc($Bags)):
 $bag_ar[] = $row;
endwhile;
//step two - ascertain the number of rows
$cnt = count($bags_ar);
// step three - discern the max number of table rows to deal with smaller recordsets
$max_table_rows = (ceil($cnt/4) > 7) ? 7 : ceil ($cnt/4);
//now start the outer loop (for the rows)
$c = 0; //instantiate a counter
$t_contents = ""; // instantiate a content variable
for ($o_loop=0; $o_loop<$max_table_rows; $o_loop++):
 //now start the inner loop for the columns
 $t_contents .= "<tr>";
 for ($cols=0; $cols<4; $cols++):
   if ($c <= $cnt):
     $t_contents .= "<td>&nbsp;</td>";
   else:
     $t_contents .= <<<STR
<td>
<a href="bags.php?bag_brand_id={$bag_ar[$c]['bag_brand_id']}>  {$bag_ar[$c]['bag_brand_title']}</a>
</td>

STR;
   endif;  
   $c++;
 endfor;
   

endfor; //end outer loop
?>
 
Had a couple errors in my original coding, now this should actually give you 4 cells across (4 columns) by 7 down. (7 rows).

Code:
<table width="500" height="33" cellpadding="2" >
   
         
         <?php
[red]$counter=0;[/red]
do {
[red]if($counter>=4){ echo "<tr>"; $counter=0;}[/red]

?>
           <td> <a href="bags.php?bag_brand_id=<?php echo $row_Bags['bag_brand_id']; ?>"><?php echo $row_Bags['bag_brand_title']; ?></a>
         </td>

            <?php

[red]$counter++;
if($counter>=4){echo "</tr>";}[/red]

} while ($row_Bags = mysql_fetch_assoc($Bags)); ?> </tr>
  </table>

I've tested it and it works, given yor query actually returns 28 results if it doesn't then you will have less rows.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
@vacunita
how does your code start the first table row? do you need to include a "<tr>" in the starting html?

and is there a risk that the last row will have two closing tags if there are exactly 28 elements (or a multiple of 4)?

and how does your code constrain the table to 7 rows if there are more than 28 results in the recordset?
 
I did forget to account for the starting <tr> tag and the final closing one in the event that the number of records is not exactly divisble by 4.
The following code accounts for both things. And works perfectly in firefox.

Code:
<table width="500" height="33" cellpadding="2" >
   <tr>
         
         <?php

$counter=0;
do {

if($counter>=4){ echo "<tr>"; $counter=0;}

?>
           <td> <a href="bags.php?bag_brand_id=<?php echo ...; ?>"><?php echo ...; ?></a>
         </td>

            <?php

$counter++;
if($counter>=4){echo "</tr>";}

} while (...); 

if($counter<=3){
echo "</tr>";
}
?>
  </table>



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
for some reason the cut and paste of the code i posted above did not work as expected. i suspect this is pilot error.

the code i had intended to post is below. it is fully commented to make it easy to see what is going on.

Code:
<?php 
//step one - get the recordset into an array so you can address elements directly and easily
$num_cols = 4;// change this as you wish.

while ($row = mysql_fetch_assoc($Bags)):
 $bag_ar[] = $row;
endwhile;

//step two - ascertain the number of rows: you use this to make sure you close the last tow neatly
$cnt = count($bags_ar);

/* step three - discern the max number of table rows to deal with smaller recordsets.  
this is because you said you only wanted a 7 row table.  
if you have more than 28 records you will only get the first 28.  
if this is not what you want then comment the next line out and uncomment the one after.
*/
$max_table_rows = (ceil($cnt/$num_cols) > 7) ? 7 : ceil ($cnt/$num_cols);
//$max_table_rows = ceil ($cnt/$num_cols);
//now start the outer loop (for the rows)
$c = 0; //instantiate a counter
$t_contents = "\r\n<table width=\"500\">"; // instantiate a content variable
for ($o_loop=0; $o_loop<$max_table_rows; $o_loop++):
 //now start the inner loop for the columns
	$t_contents .= "\r\n<tr>";
	for ($cols=0; $cols<$num_cols; $cols++):
	if ($c >= $cnt):
		//this handles events where the recordset ends in the middle of a row. 
		//ie you have 25 records and you need to blank the last three cells.
		$t_contents .= "<td>&nbsp;</td>"; 
	else:
		$t_contents .= <<<STR
	<td>
		<a href="bags.php?bag_brand_id={$bag_ar[$c]['bag_brand_id']}">  {$bag_ar[$c]['bag_brand_title']}</a>
	</td>

STR;
	endif;
	$c++;	//increment the counter
	endfor;
	$t_contents .= "\r\n<tr>"; //close the row
endfor; //end outer loop
$t_contents .= "\r\n</table>"; //close the table
echo $t_contents; //output the table
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top