Question

I want to display data in two columns as below

Entry 1                                 Entry 2
entry 1 description                     entry 2 description

Entry 3                                 Entry 4
entry 3 description                     entry 4 description

Entry 5                                 
entry 5 description                     

Now In asp.net its prestty easy, just get a datalist make some settings and give data source to it and it will render it for you.

But in PHP how to display this kind of list, can anyone give me some code for this?

Thanks

Was it helpful?

Solution

i assume that you want draw an html table.. which is so easy to handle html layout & variable column number. here is a sample code that will do exact what you need..

        /* populate sample data */
    for($i=1; $i<10; $i++) {  $data_array[]=array('Entry '.$i,'entry '.$i.' description');  }

    /* how many columns */
    $column_number='3';

    /* html table start */
    ?><table border="1" cellspacing="5" cellpadding="5"><?php

    $recordcounter=1;  /* counts the records while they are in loop */
    foreach($data_array as $record) { 

        /* decide if there will be new Table row (<TR>) or not ... left of division by column number is the key */
        if($recordcounter%$column_number==1){ echo "<tr>"; }
        ?>          
            <td> 
                <?=$record[0]?> 
                <br />
                <?=$record[1]?>
            </td>
        <?php
        /* decide if there will be end of table row */
        if($recordcounter%$column_number==0){ echo "</tr>"; }
        $recordcounter++;  /* increment the counter */
    }

    if(($recordcounter%$column_number)!=1){ echo "</tr>"; }  

    ?></table><?php

OTHER TIPS

I suppose you're asking how to build the HTML output?

<?php $array = array('Item 1' => 'Description 1', 'Item 2' => 'Description 2'); ?>

<dl>
<?php foreach ($array as $item => $description) : ?>
    <dt><?php echo $item; ?></dt>
    <dd><?php echo $description; ?></dd>
<?php endforeach; ?>
</dl>

You can easily style this using CSS, floating them to make columns or what have you.

If you're using a framework it may include a helper of some sort to do this, but standard PHP does not.

Does that answer your question?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top