Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] help with nested loop syntax


[SOLVED] help with nested loop syntax

Reply
  #1 (permalink)  
Old 08-23-08, 01:58 PM
glass satellite glass satellite is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] help with nested loop syntax

hi there, i'm looking for a bit of guidance setting up some nested loops. my attempts have yet to yield the desired result so thought i better get some expert advice!

the page in question is:
http://www.softstarrecords.com/links.php

as you can see, there are 4 div.columns and each item (MySql record) is wrapped in a div.box that lives inside a column. what i'd like to do is set up some loops to divide the number of records by 4 and dynamically distribute the boxes so there are the same number in each column, while accounting for the remainder of the division. i'd post code, but frankly my attempts have been pretty off the mark so i'm thinking maybe it's better to just start from scratch with some suggestions.

all of this is so new records can be added to the MySql database and the PHP will continue to evenly fill out the page irregardless of how many records.

if my intent isn't clear please ask for further clarification, and as always thanks so much for your time and energy.
Reply With Quote
  #2 (permalink)  
Old 08-25-08, 08:23 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

//connect to db and run query for results first...
//create while loop to add all rows to an array (in this example I'm using $rows as the array)
//also add the divs to either side of the row content if desired
$colcount 4;  #Number of columns
$items ceil(count($rows)/$colcount);  #round up number of items per column
$cols = array();

for(
$j 0$j $items$j++)
{
    for(
$i 0$i $colcount$i++)
    {
        
$current = (($j $colcount) + $i);
        
        
//Is array item set
        
if(isset($rows[$current]))
        {
            
$cols[$i][$j] = $rows[$current];
        }else{
            
//Exit both loops
            
break 2;
        }
    }
}

echo 
'<pre>'.print_r($cols,true).'</pre>'
That should do it. you will then have 4 columns with all the data
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #3 (permalink)  
Old 08-25-08, 11:26 PM
glass satellite glass satellite is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
solid gold

this will help me gain a better understanding of loops, thanks for the help.
Reply With Quote
  #4 (permalink)  
Old 08-26-08, 03:00 AM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
No problem
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #5 (permalink)  
Old 08-27-08, 07:48 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You can also organize your data in columns directly from the database without using an array.
This little program displays the records in vertical columns from top to bottom, left to right in alphabetical order.
You can limit the number of records to display by changing the value of $records_to_display.
And you can specify the number of columns by changing the value of $cols.
You should be able to insert this code into your existing code with very little effort.

PHP Code:

<html>
<head>
<style>
.column {
    width: 23%;
    height:  87%;
    float:  left;
    margin-right: 10px;
    border:1px solid #000;
    }
.lbox {
    margin-bottom: 10px;
    border: 1px solid #666;
    padding: 4%;
    background: #000;
    float: left;
    width: 91%;
   height:100px;
   overflow:auto;
    }    
</style>
</head>
<body>
<?php
$server 
"";   // Mysql host name
$user "";     // Mysql user name
$password ""// Mysql password
$db "";       // Mysql database name
$table "";    // Mysql table name
$column_name "";   // Mysql table column name
$records_to_display 999// Set this value to limit number of records to display. Set to high value to display all records.
$cols 4// Set this value for number of columns to display
mysql_connect($server,$user,$password) or die("Connect error: ".mysql_error());
mysql_select_db($db) or die("DB error: ".mysql_error());
$results mysql_query("SELECT $column_name FROM $table ORDER BY $column_name LIMIT $records_to_display");
$num_records mysql_num_rows($results);
$rows ceil($num_records/$cols);
echo 
'<div>';
for(
$i=0;$i<$num_records;$i+=$rows)
{
 echo 
'<div class="column">';
 for(
$j=0;$j<$rows;$j++)
 {
  
$row mysql_fetch_assoc($results);
  echo 
$r $row '<div class="lbox">'.$row[$column_name].'</div>' '<div class="lbox"></div>';
  }
 echo 
'</div>';
 }
echo 
'</div>';
?>
</body>
</html>
__________________
Jerry Broughton
Reply With Quote
  #6 (permalink)  
Old 08-27-08, 12:38 PM
glass satellite glass satellite is offline
Newbie Coder
 
Join Date: Aug 2008
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
thank you, i'll give this code a spin as well, appreciate the suggestion.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Assembly Language Loop Help J8II Other Languages 3 05-27-08 12:05 AM
Syntax Error Nikas Database 4 05-15-08 10:48 AM
infinite loop versus guarded blocks UnrealEd Everything Java 0 11-11-07 02:53 PM
Update multiple rows outside loop - need help ElvansX PHP 1 12-03-06 01:55 AM
While loop jaishalg PHP 1 11-23-04 03:36 PM


All times are GMT -5. The time now is 07:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.