4images Forum & Community

General / Allgemeines => Programming => Topic started by: Sun Zaza on January 16, 2010, 07:10:23 PM

Title: Getting information in 2 columns instead of 1(List)
Post by: Sun Zaza on January 16, 2010, 07:10:23 PM
Hello guys,

I have a simple question (Not for me anyway :oops:):

On my gallery I made a list of all my images (Without thumb). Something like:
1- image1
2- image2
3- image3
...
...
...


Till now I have no problem.

Now I want to have this in 2 columns instead of 1.

I am uisng this code:

Code: [Select]
$video_list .= "<tr><td valign=\"top\" class=\"sortable\">".$i++."- <a href=\"".$site_sess->url(ROOT_PATH."details.php?image_id=".$image_row['image_id'])."\">".$image_row['image_name']."</a></td> 
   
</td></tr>
   ";                   
}     
}
  $video_list .= "</table>\n";
 
$site_template->register_vars("video_list", $video_list);
unset($video_list);

Many thanks in advance,
Cruxy
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Rembrandt on January 16, 2010, 08:47:47 PM
so.. test it :)

 $count = 0;
 $trcounter = 0;  
  $video_list = "<table>\n";
while ($image_row = $site_db->fetch_array($result)){
  $video_list .= ($trcounter++ % 2 == 0) ? "<tr>" : "";
  $video_list .= "<td valign=\"top\" class=\"sortable\">".$i++."- <a href=\"".$site_sess->url(ROOT_PATH."details.php?image_id=".$image_row['image_id'])."\">".$image_row['image_name']."</a></td>\n";
  $video_list .= ($trcounter++ % 2 == 0) ? "</tr>" : "";
$trcounter++;  
}      
  $video_list .= "</table>\n";
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Sun Zaza on January 16, 2010, 10:18:30 PM
Thanks Andy. Almost how I wandted.

I believe your code should look like this:

$i = 1;
  $trcounter = 0; 
  $video_list = "<table>\n";
 
while ($image_row = $site_db->fetch_array($result)){

  $video_list .= ($trcounter++ % 2 == 0) ? "<tr>" : "";
  $video_list .= "<td valign=\"top\" class=\"sortable\">
  ".$i++."- <a href=\"".$site_sess->url(ROOT_PATH."details.php?image_id=".$image_row['image_id'])."\">".$image_row['image_name']."</a></td>\n";
 
}
}
  $video_list .= "</table>\n";

Now I encounter another problem. I got 2 colomn like this:

image1image2
image3image4
image5image6

instead of:

image1image4
image2image5
image3image6

How can we solve that? Because you have to go throuw the first column till the end and then you jump the second column.

Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Rembrandt on January 17, 2010, 08:33:32 AM

//$rows=40; //Wrap after how many lines
$rows= ($total_images/2) ;

 $count = 0;
 $sx = 0;  
  $video_list = "<div style=\"float:left;\">";
while ($image_row = $site_db->fetch_array($result)){
  $video_list .= ($sx%$rows == 0) ? "</div>":"";
  $video_list .= ($sx%$rows == 0) ? "<div style=\"float:left\">":"";
  $video_list .= "<span style=\"margin-right:20px\"  >".$i++."- <a href=\"".$site_sess->url(ROOT_PATH."details.php?image_id=".$image_row['image_id'])."\">".$image_row['image_name']."</a></span><br>";
$sx++;
}
  $video_list .= "</div style=\"clear:both\">\n";
  
$site_template->register_vars("video_list", $video_list);
unset($video_list);
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Sun Zaza on January 17, 2010, 10:57:34 AM
Good morning Andy,

I love it when you don't use tables in your code. Let take 4images to another level :wink:

A small correction:

1- use $count or $i in your code instead of both.

2- Change:
$count = 0;

to

$count = 1;

Otherwise your list will start with the number 0.

So your code will look like this:



//$rows=40; //Wrap after how many lines
$rows= ($total_images/2) ;

 $count = 1;
 $sx = 0; 
  $video_list = "<div style=\"float:left;\">";
while ($image_row = $site_db->fetch_array($result)){
  $video_list .= ($sx%$rows == 0) ? "</div>":"";
  $video_list .= ($sx%$rows == 0) ? "<div style=\"float:left\">":"";
  $video_list .= "<span style=\"margin-right:20px\"  >".$count +."- <a href=\"".$site_sess->url(ROOT_PATH."details.php?image_id=".$image_row['image_id'])."\">".$image_row['image_name']."</a></span><br>";
$sx++;
}
  $video_list .= "</div style=\"clear:both\">\n";
 
$site_template->register_vars("video_list", $video_list);
unset($video_list);

This code is working perfect and solve the earlier problem (I will use it for another project :wink:), but now you can get more that 2 columns. Because if you have 90 images, you will get 3 columns:40, 40 and 10.


Let say for our template we want only 2 columns.

How can we do that?
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Rembrandt on January 17, 2010, 11:29:56 AM
.....
How can we do that?
look at the top of the code:
$rows= ($total_images/2) ;

 90 / 2 = 45
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Sun Zaza on January 17, 2010, 11:35:35 AM
.....
How can we do that?
look at the top of the code:
$rows= ($total_images/2) ;

 90 / 2 = 45

But if you have 91 images, you will get: 45, 45 and 1 (In 3 columns). Let's say we want 2 columns: 45 and 46. (Like in categories in the homepage).

PS: I tried to study the function (get_categories) in the functions.php, but it was too much for my poor PHP knowledge.
Title: Re: Getting information in 2 columns instead of 1(List)
Post by: Rembrandt on January 17, 2010, 11:41:18 AM
test...


$rows= $total_images/2 +1 ;