...

View Full Version : Foreach items and list in several columns



levani
03-29-2010, 03:36 PM
I'm trying to foreach items got from database in three columns but without any success... How can I do something like this:

Item 1________Item 5________Item 9
Item 2________Item 6________Item 10
Item 3________Item 7________Item 11
Item 4________Item 8________Item 12


Can anyone please help?

angst
03-29-2010, 03:40 PM
can you post your code?

levani
03-29-2010, 03:43 PM
Here it is:


<?php

$items = $wpdb->get_results("SELECT * FROM wp_mwarm");
foreach ($items as $list) {

echo '<li><a href="?id_type=' . $list->ID . '">' . $list->name . '</a>';

}

?>

angst
03-29-2010, 03:48 PM
are there only three records being called, if so you could do it like this;


<?php
echo "<table><tr>";
$items = $wpdb->get_results("SELECT * FROM wp_mwarm");
foreach ($items as $list) {

echo '<td><a href="?id_type=' . $list->ID . '">' . $list->name . '</a></td>';

}
echo "</tr></table>";
?>


or are they're several records and you want several rows with 3 columns per row?

levani
03-29-2010, 03:56 PM
The records are retrieved from database and I don't know the number in advance as they are changed dynamically. I want to list them in thee columns. For example if there are total 30 records they should be divided into three parts. The first 10 records goes to first column, records from 10 to 20 into second column and so on...

Here I'll post an image:

http://i40.tinypic.com/zin49x.png

Assuming the records are the car models...

angst
03-29-2010, 04:07 PM
something like this should work ( untested, might be a bit of tweaking )



$result = mysql_query("SELECT * FROM wp_mwarm");
$NumberOfResults = mysql_num_rows($result);
$RecordsPerCol = round( $NumberOfResults / 3, 0 );

$Counter = 0;
echo "<table border='1'><tr>";

while($row = mysql_fetch_array($result)){

$Counter++;
if($Counter == 1){
echo "<td>";
}

echo $row['name'] . "<br />";

if($Counter == $RecordsPerCol){
echo "</td>";
$Counter = 0;
}
}
echo "</tr></table>";

abduraooft
03-29-2010, 04:44 PM
It'd better for you to consider as three unordered list. Find the total number of records and then divide it by 3 to get the number of items to be added to each list. Then use 3 seperate loops to echo the list items under each list.
After that you may set float:left;width:33%; to the list to align them side by side.

@angst: The data doesn't look like tabular, and hence using a table wouldn't be semantic.

levani
03-29-2010, 04:47 PM
$result = mysql_query("SELECT * FROM wp_mwarm");
$NumberOfResults = mysql_num_rows($result);
$RecordsPerCol = round( $NumberOfResults / 3, 0 );

$Counter = 0;
echo "<table border='1'><tr>";

while($row = mysql_fetch_array($result)){

$Counter++;
if($Counter == 1){
echo "<td>";
}

echo $row['name'] . "<br />";

if($Counter == $RecordsPerCol){
echo "</td>";
$Counter = 0;
}
}
echo "</tr></table>";

This code works fine. Thanks!!!

sitNsmile
03-29-2010, 04:58 PM
something like this should work ( untested, might be a bit of tweaking )



$result = mysql_query("SELECT * FROM wp_mwarm");
$NumberOfResults = mysql_num_rows($result);
$RecordsPerCol = round( $NumberOfResults / 3, 0 );

$Counter = 0;
echo "<table border='1'><tr>";

while($row = mysql_fetch_array($result)){

$Counter++;
if($Counter == 1){
echo "<td>";
}

echo $row['name'] . "<br />";

if($Counter == $RecordsPerCol){
echo "</td>";
$Counter = 0;
}
}
echo "</tr></table>";


by doing this way, you've just ruined the function of OO style coding.

angst
03-29-2010, 05:19 PM
by doing this way, you've just ruined the function of OO style coding.

excuse me? I offered an example of how it could be done. it wasn't my intention to write the code for him exactly how they needed the end result to be.

your more then welcome to write it out again and save your rude comments for someone else.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum