1

I'm not sure how difficult this but I have an array and would like to put it into an html table. I need to have two array strings per row, so if this were the array:

   $array1 = array(
     1 => 'one',
     2 => 'two',
     3 => 'three',
     4 => 'four',
     5 => "five",
     6 => 'six',
    );

And I need the html table to look like this:

| one |  two |
|three| four |
|five | six  |

This is my code:

$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect(); 

    $sql = "SELECT ID, movieno
            FROM movies
            ORDER BY ID DESC
            LIMIT 6 ";

    $rows = $db->query($sql);

    print '<table width="307" border="0" cellspacing="5" cellpadding="4">';
    while ($record = $db->fetch_array($rows)) {
        $vidaidi = $record['movieno'];
        print <<<END
        <tr>
            <td>
                <a href="http://www.youtube.com/watch?v=$vidaidi" target="_blank">

                <img src="http://img.youtube.com/vi/$vidaidi/1.jpg" width="123" height="80"></a>   
            </td> 
        </tr>
    END;
    }
    print '</table>';  

I want to put it on two columns.

1
  • if i print ou $record i get: Resource id #5 if i print $vidaidi i get Array ( [ID] => 61 [movieno] => VpWnUkUdUA ) hmmm.. i recive only 1 row in array! how to extract all the data in 1 array? Commented Mar 18, 2010 at 7:08

5 Answers 5

2

Try this code ...

<?php

$array1 = array(
                1 => 'one',
                2 => 'two',
                3 => 'three',
                4 => 'four',
                5 => "five",
                6 => 'six',
               );

$val = current  ( $array1 )  ;
print "<table border=1>";
while ( $val )
{
  print "<tr> <td> $val </td> ";
  $val = next ( $array1 ) ;
  print "<td> $val </td> </tr> ";
  print "\n";
  $val = next ( $array1 );
}

print "</table>";

?>
Sign up to request clarification or add additional context in comments.

Comments

2
$array1 = array(
     1 => 'one',
     2 => 'two',
     3 => 'three',
     4 => 'four',
     5 => "five",
     6 => 'six',
    );

echo '<table>';
for ($i = 1; $i <= 7; $i++) {
  if ($i % 2 == 1) echo '<tr>';
  echo "<td>{$array1[$i]}</td>";
  if ($i % 2 == 2) echo '</tr>';
}
echo '</table>';

Comments

0

You can do it with a multidimensional array like this: http://www.terrawebdesign.com/multidimensional.php

Apart from writing your own code to create , and tags though I don't think there is a built in way.

You can use print_r() to print the contents of the array as a built in way of viewing the array.

Comments

0

You can do something like:

print "<table>";
for($i=1;$i<=count($arr);$i++) {
        if($i%2)
                print"<tr><td>$arr[$i]</td>";
        else
                print"<td>$arr[$i]</td></tr>\n";
}
print "</table>";

Comments

0
echo "<table>";
for($i=0;$i+=2;$i<count($array1))
{
   echo "<tr><td>".$array1[$i]."</td><td>".isset($array1[$i+1])?$array1[$i+1]:'no value'."</td></tr>";
}
echo "</table>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.