Jump to content

Create Html Table Using Php


filoaman

Recommended Posts

Hi friend

I try to create a simple html table and put on every cell data from three arrays

my arrays are:

 

$one = array('title1','title2','title3','title4','title5','title6');
$two = array('image1','image2','image3','image4','image5','image6');
$three = array('text1','text','text3','text4','text5','text6');

 

What i like to create is a table like this:

 

-----------------------------------

| title1 | title2 | title3 |

-----------------------------------

| image1 | image1 | image3 |

-----------------------------------

| text1 | text2 | text3 |

-----------------------------------

| title4 | title5 | title6 |

-----------------------------------

| image4 | image5 | image6 |

-----------------------------------

| text4 | text5 | text6 |

-----------------------------------

 

In other words i need a script to create a rows take the first 3 data from every array put them on cells and every 3 cells create a new set of rows.

 

Until now i'm able using the following cote to put data from only one array

 

$numFiles = count($one);

$start = 0;
$end = $numFiles;
$split = 3;

print "<table><tr>";

for($i = $start; $i < $end; $i++) {
print "<td>".$one[$i]."</td>";
if(($i) % ($split) == $split-1){
print "</tr><tr>";
}
}

print"</tr></table>";

 

If i use something like this:

 

 

 


$numFiles = count($one);
$start = 0;
$end = $numFiles;
$split = 3;

print "<table><tr>";

for($i = $start; $i < $end; $i++) {
print "<td>".$one[$i]."</td>";
if(($i) % ($split) == $split-1){
print "</tr><tr>";
for($i = $start; $i < $end; $i++) {
print "<td>".$two[$i]."</td>";
if(($i) % ($split) == $split-1){
print "</tr><tr>";
}
}
}
}

print"</tr></table>";

 

I get this:

 

-----------------------------------

| title1 | title2 | title3 |

-----------------------------------

| image1 | image1 | image3 |

-----------------------------------

| image4 | image5 | image6 |

-----------------------------------

 

I know that i'm close but i need some help...

Link to comment
Share on other sites

Write a single loop, and use the index for all three arrays. Collect each row in a separate variable, and output all three when you get to the split.

 

Something like this:

$one = array('title1','title2','title3','title4','title5','title6');
$two = array('image1','image2','image3','image4','image5','image6');
$three = array('text1','text','text3','text4','text5','text6');

$numFiles = count($one);

$start = 0;
$end = $numFiles;
$split = 3;

print "<table>";

$titles = $images = $texts = '';
for($i = $start; $i < $end; $i++) {
   $titles .= "<td>".$one[$i]."</td>";
   $images .= "<td>".$two[$i]."</td>";
   $texts .= "<td>".$three[$i]."</td>";

   if(($i % $split) == $split-1){
       print "<tr>" . $titles . "</tr><tr>"  . $images . "</tr><tr>" . $text . "</tr>"
       $titles = $images = $texts = '';
   }
}

// Just in case the count is not a multiple of the split
if (! empty($titles)) {
   print "<tr>" . $titles . "</tr><tr>"  . $images . "</tr><tr>" . $text . "</tr>"
}
print"</table>";

Link to comment
Share on other sites

Great Help thank you.

I only had to change some typos. The working code is this

$one = array('title1','title2','title3','title4','title5','title6');
$two = array('image1','image2','image3','image4','image5','image6');
$three = array('text1','text','text3','text4','text5','text6');


$numFiles = count($one);


$start = 0;
$end = $numFiles;
$split = 3;


print "<table>";


$titles = $images = $texts = '';
for($i = $start; $i < $end; $i++) {
       $titles .= "<td>".$one[$i]."</td>";
       $images .= "<td>".$two[$i]."</td>";
       $texts .= "<td>".$three[$i]."</td>";


       if(($i % $split) == $split-1){
               print "<tr>" . $titles . "</tr><tr>"  . $images . "</tr><tr>" . $texts . "</tr>";
               $titles = $images = $texts = '';
       }
}


// Just in case the count is not a multiple of the split
if (! empty($titles)) {
       print "<tr>" . $titles . "</tr><tr>"  . $images . "</tr><tr>" . $texts . "</tr>";
}
print"</table>";

Link to comment
Share on other sites

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.