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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Certain muber in one line, before creating a new line.

Status
Not open for further replies.

leeboycymru

Programmer
Jan 23, 2007
185
GB
Hi,

I have a piece of code that simply draws a number of elements out of the database. What I need this code to do is to display for example 5 elements from the db, then on the 6th it creates a new line for another 5 and so on.

Here is the code:

<?
$cnt=0;
while($cnt<$top_count)
{
$qw=mysql_query("select * from tbl_touroperators where Nom_Top = '$top_array[$cnt]'") or die (mysql_error());
$rw=mysql_fetch_assoc($qw);
echo "<td class='middletext'><a href='[Web_Top]' target='_blank' class='bigweblinks'>$top_array[$cnt],</td>";
$cnt=$cnt+1;
}
//$top
?>


Cheers

Lee
 
Maintain a counter that is incremented every time a value is printed. When that counter gets to the value you choose, output a "<TR>" or "<BR" tag as appropriate and reset the counter back to its starting value.


Want the best answers? Ask the best questions! TANSTAAFL!
 
Thanks you very much for replying, but although that does sound very straight forward, i am struggling with it.

I have picked this code up from a previous site, and although I am familiar with ASP I'm not that familiar with PHP as of yet. So could you give me a starter using the code above, of what you ar saying.

Thank you

Lee
 
Syntactically, VBScript/ASP and PHP are not dissimilar. And the solution to this problem would be very much alike, regardless of which of the two languages involved.

You already have a variable $cnt that you initialize to zero before your while-loop. You're already incrementing that variable in the loop.

Compare that variable's value to the value you want. And when it compares favorably print the newline or <tr> and reinitialize the variable.

Hint:

Check out:




Want the best answers? Ask the best questions! TANSTAAFL!
 
hopefully this is obvious but jic: you control the new lines by closing the table row. </tr>. your sample code does not contain the <tr> tags.

i'm not sure why you are doing a db call for each iteration but there is probably a good reason. you might consider testing to see whether the structure is optimal or whether you could pull your data in a single query.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top