1

I would like to display a single data to 2 columns as mysql table result in my PHP file. I have tried with my code but the same data has been showed as result like <tr><td>result1</td><td>result1</td>.

I have tried with my code but with the following result:

| result 1 | result 1 |
| result 2 | result 2 |
| result 3 | result 3 |
| result 4 | result 4 |
| result 5 | result 5 |
| result 6 | result 6 |
| result ... | result ... |

But I need result as

| result 1 | result 2 |
| result 3 | result 4 |
| result 5 | result 6 |
| result 7 | result 8 |
| result ... | result ... |

Here is my code:

    <?php
        include('config.php');
        $data_content = '';
        $qry = "SELECT DISTINCT bankName FROM bankData ORDER BY bankName";
        $result = mysql_query($qry);
        while($row = mysql_fetch_array($result))
            {
                 $data_content.= "<a href='bank/".$row['bank_Name'].".php'> ".$row['bankName']."</a>";
            }
        mysql_close();
        ?>
<!DOCTYPE html>
<html lang="en">
<head>
<body>
<div>   
<table border="1">
    <?php
         for($i=0; $i<=1; $i++)
            {
            echo "<tr>";
            for($j=0; $j<=1; $j++)
            {
             echo "<td>";
             echo $data_content;
             echo "</td>"; 
            }
             echo "</tr>";
            }
    ?>
</table>
</div>
</body>
</html>

Help me please.

1
  • 1
    Your for loop isn't doing anything? Commented May 14, 2015 at 3:04

1 Answer 1

2

Put the loop down below:

<table border="1">
<?php
$i = 0;
while($row = mysql_fetch_array($result))
    {
         // Odd row opens
         if (++$i % 2 != 0) echo "<tr>";
         echo "<td><a href='bank/".$row['bankName'].".php'> ".$row['bankName']."</a></td>";
         // Even row closes
         if ($i % 2 == 0) echo "</tr>";  
    }
    // If you have an odd number of results, add a blank column and close the last row
    if ($i % 2 != 0)  echo "<td></td></tr>";
?>
</table>

(++$i % 2 != 0) increments $i and checks if it is odd. If it is odd, it will open the table row with </tr> and the next iteration will close a table row with </tr>.

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

2 Comments

it works but had some issue. The first row's 2nd columns shows empty value.
Thanks for the code.. Now its perfectly works for me.. Added up-vote for your answer.. :-)

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.