Make two columns of almost equal size with the data from tbl

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Make two columns of almost equal size with the data from tbl

Post by xionhack »

Hello. I have a table that has a list of songs. I want with that data to make 2 columns to list them in a website. I tried this:

Counted the rows
Divided the rows by 2
Took the floor of that for the first column
Then substracted that floor by the amount counted for the second column

But I havent been able to display them in the html. Can somebody help me?
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Make two columns of almost equal size with the data from tbl

Post by iankent »

xionhack wrote:Hello. I have a table that has a list of songs. I want with that data to make 2 columns to list them in a website. I tried this:

Counted the rows
Divided the rows by 2
Took the floor of that for the first column
Then substracted that floor by the amount counted for the second column

But I havent been able to display them in the html. Can somebody help me?
Please give code samples, and examples of the data you have. Why have you counted, divided, floored and subtracted? What are you trying to achieve?
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Make two columns of almost equal size with the data from tbl

Post by xionhack »

Code: Select all

 
global $connection;
 
$count = "SELECT COUNT(name)
         FROM playlist";
$count = mysql_query($count, $connection);
confirm_query($count);
$count = mysql_fetch_array($count);
            
$songs = "SELECT name
              FROM playlist";
$songs = mysql_query($songs, $connection);
confirm_query($songs);
            
$column1 = floor($count['COUNT(name)'] / 2);
            
$column2 = $count['COUNT(name)'] - $column1;
            
$output .= "<table>";
I know this code is totally wrong in the loop part! but this is all i have!     
            
while($song_list = mysql_fetch_array($songs))
{
         for($i = 1; $i <= $column1; $i++){
        $output .= 
            "<tr>
               <td>" .
                $i . " " . $song_list['name'] .
               "<td>
             <tr>";
                          }             
}
        
$output .="</table>";
return $output;
 
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Make two columns of almost equal size with the data from tbl

Post by iankent »

xionhack wrote:

Code: Select all

            
$column1 = floor($count['COUNT(name)'] / 2);
$column2 = $count['COUNT(name)'] - $column1; 
            
while($song_list = mysql_fetch_array($songs))
{
         for($i = 1; $i <= $column1; $i++){
        $output .= 
            "<tr>
               <td>" .
                $i . " " . $song_list['name'] .
               "<td>
             <tr>";
                          }             
}
        
$output .="</table>";
return $output;
 
I think I see what you're trying to do. You want to display the list in two HTML columns side by side? Do you want the order to go down or across? I.e., should it go:
1,2
3,4
4,5
or
1,4
2,5
3,6
?

Depending on which of those you want the code will be slightly different.

Also, in your code you're using while($line = mysql_fetch_array) and inside that doing a loop from 1 to half of the total items in the list. the while() loop is enough to go through every result in $songs, so there's no need to do another loop inside it.

edit: also, if you're not using the LIMIT keyword in your SQL query you don't need to do a separate query to get the total items. you can just get the total rows returned using mysql_num_rows().

You need to find some way to output the correct HTML structure within the while() loop, or remove the while() loop entirely and use the for() loop to go through the items instead (just make sure the numbers are right otherwise you may find a result missing occasionally)
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Make two columns of almost equal size with the data from tbl

Post by xionhack »

I want it in this way

1,4
2,5
3,6

I read what u said. Im going to try to work on it. Please let me know if u have any idea! thanks!
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Make two columns of almost equal size with the data from tbl

Post by iankent »

I'd do it using the while loop and a simple counter, e.g.

Code: Select all

 
// transfer the result to an array
$rows = array();
$row = 0;
while($line = mysql_fetch_assoc($result)) {
    $row++;
    $rows[$row] = $line;
}
 
// start outputting the table
echo "<table>";
// find out how many items in the first column
$half = floor(count($rows)/2);
// loop through the first half of the array
for($i = 1; $i <= $half; $i++) {
    echo "<tr>";
    // output the item for column 1
    echo "<td>".$row[$i]['title']."</td>";
    // output the item for column 2 (current item + half)
    echo "<td>".$row[$i+$half]['title']."</td>";
    echo "</tr>";
}
echo "</table>";
 
might need a bit of tweaking but I think the idea is right :) you may also want to check if the variable actually exists (e.g., if you have 49 items then $row[$i+$half] won't exist on the 25th iteration) and maybe output &nbsp; if not

edit: there's quite probably neater or faster ways of doing this, my code feels a bit messier than it should :p

hth
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Make two columns of almost equal size with the data from tbl

Post by xionhack »

Thanks! it worked! just that got me a lil bit confused! it had to be " $rows[$i]['name']" not " $row[$i]['name']"! Thanks!
Post Reply