Hi all,

I am having quite some trouble with doing a certain task in php that im sure someone would know how to do it.

To start, I want to have my script create a table for every 12 keys in an array.

Here is the beginning of the code:

if (strpos($addon, "gallery")){
$gallery_filename = explode("|", $row_2['gallery_filename']);
$gallery_desc = explode("|", $row_2['gallery_desc']);
$photo_count = $row_2['photo_count'];
$arraycount = count($gallery_filename);

$gallery_filename and $gallery_desc are in an array.

WHAT I WANT TO DO is for every 12 keys found in the array it would create the following...

$table = <table id=\"gallery_desc\" width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr>
    <td align=\"center\" width=\"33%\"><img src=\"images/".$gallery_filename[0]."\" alt=\"".$gallery_desc[0]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td align=\"center\"><img src=\"images/".$gallery_filename[1]."\" alt=\"".$gallery_desc[1]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td align=\"center\"><img src=\"images/".$gallery_filename[2]."\" alt=\"".$gallery_desc[2]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td valign=\"top\" width=\"33%\">".$gallery_desc[0]."</td>
    <td valign=\"top\" >".$gallery_desc[1]."</td>
    <td valign=\"top\" >".$gallery_desc[2]."</td>
  </tr>
  <tr>
    <td width=\"34%\"><img src=\"images/".$gallery_filename[3]."\" alt=\"".$gallery_desc[3]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[4]."\" alt=\"".$gallery_desc[4]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[5]."\" alt=\"".$gallery_desc[5]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$gallery_desc[3]."</td>
    <td>".$gallery_desc[4]."</td>
    <td>".$gallery_desc[5]."</td>
  </tr>
  <tr>
    <td><img src=\"images/".$gallery_filename[6]."\" alt=\"".$gallery_desc[6]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[7]."\" alt=\"".$gallery_desc[7]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[8]."\" alt=\"".$gallery_desc[8]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$gallery_desc[6]."</td>
    <td>".$gallery_desc[7]."</td>
    <td>".$gallery_desc[8]."</td>
  </tr>
  <tr>
    <td><img src=\"images/".$gallery_filename[9]."\" alt=\"".$gallery_desc[9]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[10]."\" alt=\"".$gallery_desc[10]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$gallery_filename[11]."\" alt=\"".$gallery_desc[11]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$gallery_desc[9]."</td>
    <td>".$gallery_desc[10]."</td>
    <td>".$gallery_desc[11]."</td>
  </tr>
</table>"

so if there are more than 12 keys it would create another table with the corresponding keys 12 - 23 and than 24-33. But if there are not more than say 12 keys it would stop at the first table.

For now I am using this do this: if ($arraycount>0) {//$table="table here"} and than if ($arraycount>11) {//$table.="table here"} .

Instead of doing this over and over again would there be a way to do this with either while() or foreach() .

I am sorry if this doesn't make much sense, I can tryt o explain it more clearly if needed. Thanks for any help at all.

See if this helps. Notheless I failed to see entirly what you want to do.

if($arraycount<12){
print 'No table';
} else {

for($i=1;$i<=$arraycount;$i++)

######## you may right like this #####
if($i%12 == 0){
// change table name here
// it depends how you will assign table name
}
############ end#################

############You may also go like this ##########
if($i>1 && $i<=12){
$tablename = 'thistbale'
} else if($i>12 && $i<=24){{
$tablename = 'thistbale';
}
################You may also go like this ################

}

yes do the divisable by 12 thing

array_chunk() would be very useful here.

thanks everyone for the input, what I would like to do is stay away from having to enter in every table. array_chunk() looks like it is exactly what I need as it renames the array keys as well.
Thank you very much kkeith29.

If that doesn't work I will probably just use some if and else statements.

Thanks

Hey again, sorry repost to this thread but I am having some troubles still getting this one function to work.

To explain a bit better, I want to create a table for every 12 array items. Here is what I have right now.

$gallery_filename = explode("|", $row_2['gallery_filename']);
$gallery_desc = explode("|", $row_2['gallery_desc']);
$photo_count = $row_2['photo_count'];

$arraycount = count($gallery_filename);
$file_chunk = array_chunk($gallery_filename, 12);
$dec_chunk = array_chunk($gallery_desc, 12);
for ( $i = 0 ; $i =< $arraycount ; $i=$i + 12) {
$gallery_table .= "<h2>Photo Gallery</h2>
<table id=\"dec_chunk\" width=\"700\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
  <tr>
    <td align=\"center\" width=\"33%\"><img src=\"images/".$file_chunk[0]."\" alt=\"".$dec_chunk[0]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td align=\"center\"><img src=\"images/".$file_chunk[1]."\" alt=\"".$dec_chunk[1]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td align=\"center\"><img src=\"images/".$file_chunk[2]."\" alt=\"".$dec_chunk[2]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td valign=\"top\" width=\"33%\">".$dec_chunk[0]."</td>
    <td valign=\"top\" >".$dec_chunk[1]."</td>
    <td valign=\"top\" >".$dec_chunk[2]."</td>
  </tr>
  <tr>
    <td width=\"34%\"><img src=\"images/".$file_chunk[3]."\" alt=\"".$dec_chunk[3]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[4]."\" alt=\"".$dec_chunk[4]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[5]."\" alt=\"".$dec_chunk[5]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$dec_chunk[3]."</td>
    <td>".$dec_chunk[4]."</td>
    <td>".$dec_chunk[5]."</td>
  </tr>
  <tr>
    <td><img src=\"images/".$file_chunk[6]."\" alt=\"".$dec_chunk[6]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[7]."\" alt=\"".$dec_chunk[7]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[8]."\" alt=\"".$dec_chunk[8]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$dec_chunk[6]."</td>
    <td>".$dec_chunk[7]."</td>
    <td>".$dec_chunk[8]."</td>
  </tr>
  <tr>
    <td><img src=\"images/".$file_chunk[9]."\" alt=\"".$dec_chunk[9]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[10]."\" alt=\"".$dec_chunk[10]."\" vspace=\"10px\" height=\"75px\" /></td>
    <td><img src=\"images/".$file_chunk[11]."\" alt=\"".$dec_chunk[11]."\" vspace=\"10px\" height=\"75px\" /></td>
  </tr>
  <tr>
    <td>".$dec_chunk[9]."</td>
    <td>".$dec_chunk[10]."</td>
    <td>".$dec_chunk[11]."</td>
  </tr>
</table>";
}

Is this code properly written, I am trying to understand how array_chunk works. Will array_chunk grab a new set of arrays everytime in the for() statement? Or is it going to reuse the same 12.

Any help is appreciated, but if it comes to it Iw ill just use else if statements as planned.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.