Jump to content

Recommended Posts

Hi,

 

The code for displaying a results set in multiple columns (http://www.phpfreaks.com/forums/index.php?topic=95426.0) works really well and displays

 

1 2 3 4

5 6 7 8

 

Does anyone know how to change so it displays as

 

1 3 5 7

2 4 6 8

 

Any help would be greatly appreciated

 

thanks

 

a

Thanks for the reply fugix,

 

I've got a membership list which I am returning

 

I want my table to have 4 columns and, say, 20 rows at the moment...

 

Member 1        Member 3

Member 2        Member 4

..

Member 20      Member 40

 

 

I'm not sure how to

open the row, populate the 1st table cell then close the row

open the 2nd row etc

then when I get to the 21st member I will have to re-open the first row and populate the 2nd cell....

 

I'm not sure also by what you mean when u say order by odd/even ids

 

thanks

 

a

if so...you can change

 

$query = "SELECT product FROM selling_items ORDER BY prod_id";

 

to

$query = "SELECT product FROM selling_items WHERE MOD(<row>,2) = 1 ORDER BY prod_id";

 

for odds

 

and

$query = "SELECT product FROM selling_items WHERE MOD(<row>,2) = 0 ORDER BY prod_id";

 

for evens

 

 

 

 

ok...

the code is as follows...

 

   $num_results = mysql_num_rows($res);
   $num_cols = 4;
   $num_rows_decimal = $num_results / $num_cols;
   $num_rows = ceil($num_rows_decimal);

   $rows = array();

   $j = 1;
   while($row = mysql_fetch_assoc($res))
   {
   $data = $row['firstname'].' '.$row['lastname'];
   @$rows[$j] .= ' <td class="member">'.$data."</td>";
   
   if($j == $num_rows)
   $j = 0;
   
   $j++;
   }
   
   echo '<table class="losfinal">
    <thead>
       <tr>
          <th colspan="4">Players</th>
       </tr>
    </thead>';
   
   foreach($rows as $row)
   {
   echo " <tr>" . $row . " </tr>";
   }

   echo '</table>';

 

The live page where its running is http://www.horeswoodgaa.com/membership.php and as i said it works fine in Firefox but not in IE8 or Chrome 10 (all on Windows)

 

Go to the 2011 Membership table and cells and border is missing.

 

thanks for your help.

 

a

 

Okay, I see the problem. My original code which you're basing your code off of is not smart enough to output the desired amount of <td></td> to complete the table structure. That is why your table borders are not matching up. I'll see if I can fix my original code.

ah ok.....well thanks v much for looking at it........i've been looking around for a while today on code that prints the result set top down and then left to right.....yours was by far the best i've seen....

 

just another quick question for you...i've never seen the @ symbol used like this before.....

 

@$rows[$j] .= ' <td class="member">'.$data."</td>";

 

i'm googling now to see what it means......maybe you could explain too....

 

thanks again for all your help....

 

a

Okay I have fixed the code.

Change this line

@$rows[$j] .= ' <td class="member">'.$data."</td>";

to

@$rows[$j][] = ' <td class="member">'.$data."</td>";

 

Now change the foreach loop to

    foreach($rows as $row)
    {
        echo " <tr>" . implode("\n", $row);

        // if the current row doesn't have enough columns 
        // then output the required number of blank cells to complete the row
        if($j = (count($row) < $num_cols))
        {
            for($i = 0; $i < $j; $i++)
                echo '<td class="member"></td>';
        }

        echo " </tr>";
    }

 

The @ symbol is used for error suppression.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.