display results into tables

They have: 18 posts

Joined: Feb 2005

Ive read somewhere about this result, but I cannot find it again.

My script queries a data file, and displays one after the other until all results are shown. The results can be veried up to many hundreds showing.

How to divide the results into seperate 4 rows evenly as possible?

My script so far:

<?php
list($first) = explode('/', substr($PATH_INFO,1));
$num_results=1000;
$lines = file('/AAACars.data');
$num=count($lines);
$count=0;
foreach (
$lines as $line) {
$parts = explode('|', $line);
if (
$parts[2] == $first) {
$count++;
if(
$count<=$num_results)

echo
"<a href=\"/rars.$parts[0].html/$parts[0]/$parts[2]\">$parts[1]</a>";

}}
?>

Busy's picture

He has: 6,151 posts

Joined: May 2001

just add echo ""; above the other echo and echo ""; after it, although this would create 1000 table cells :eek: so depending on how many you wanted across you could do another $count_td so if $count_td == amount wide echo a which will create columns.
Thats the easy way, if you wanted it listed downwards in one column then again in another column beside it you have to use a nested table and a bit of math. get the result, divide by two and then echo the end of the nested table then the of the main table then the begining of the second nested table until you get to the end then finish off the tables.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

if(is_int($count_td / 4))
{
echo "\n";
}

They have: 18 posts

Joined: Feb 2005

Thanks for the replies,
So far my script does the actions but is abit backwards.
I do need the results side by side, about 4 columns wide/ ?deep.

$NUMBER_OF_ROWS = 4; // number
$lines = file('/rars.data');
$num = count($lines);
$num_cols = ceil($num/$NUMBER_OF_ROWS);
echo '';
for($row = 0; $row < $NUMBER_OF_ROWS; $row++){
echo '';
for($col = 0; $col < $num_cols; $col++){
$n = $num_cols*$row + $col;
if($num <= $n){
$result = ' ';
} else {
$parts = explode('|', $lines[$n]);
$result = ''.$parts[1].'';
}
echo ''.$result.'';
}
echo '';
}
echo '';

However, my script is not displaying the right structure or results.

1. It is not using the URL explode and search function to get my results.
list($first) = explode('/', substr($PATH_INFO,1));
if ($parts[2] == $first) {

ex. http://url.com/orders.php/Paid
Paid in the URL is "$first"
Paid is also "$parts[2]"

2. All my results are showing one-after-the-other across, making the page to wide.

Any help on this matters is surely appreciated.

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Well, this actually made me find out something interesting. Ceil and Round convert values to float, and you have to use intval to convert them back to intergers:

<?
$NUMBER_OF_ROWS = 4; // number
$num = count($what_to_count);
$n = 0;
$num_cols = intval(ceil($num/$NUMBER_OF_ROWS));
echo $num_cols;
echo '<table border="1">';
while($n < $num)
{
$i = ($n/$num_cols) * 1;
$j = (($n+1)/$num_cols) * 1;
if(is_int($i)){
echo "<tr><td>";
}else{
echo "<td>";
}
echo "Data ($n)";
if(is_int(($j)/$num_cols)){
echo "</td></tr>";
}else{
echo "</td>";
}
echo "\n";
$n++;
}
?>
'

naoshad's picture

He has: 23 posts

Joined: Feb 2005

I think you should use a while loop!

They have: 8 posts

Joined: Feb 2005

<?php
define
(\"NUM_COLS\", 4);

echo \"<table border='1'> \n\";

$col = 1;
foreach (
$items as $item) {
    if (
$col == 1) echo \"\t<tr>\";

    echo \" <td>
$item</td> \";

    if (++
$col == 4) { echo \"</tr>\n\"; $col = 1; }
}

echo \"</table> \n\";
?>

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.