Domanda

Voglio visualizzare i dati in due colonne, come di seguito

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                     

Ora in asp.net sua prestty semplice, basta avere una datalist fare alcune impostazioni e dare origine dei dati ad esso e lo renderà per voi.

Ma in PHP come visualizzare questo tipo di lista, qualcuno mi può dare qualche codice per questo?

Grazie

È stato utile?

Soluzione

Suppongo che si vuole disegnare una tabella HTML .. che è così facile da maneggiare html layout e numero di colonna variabile. ecco un esempio di codice che farà esattamente quello che ti serve ..

        /* 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

Altri suggerimenti

Immagino che tu stai chiedendo come costruire l'output HTML?

<?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>

Si può facilmente stile di questo usando i CSS, galleggiante loro di rendere le colonne o quello che hai.

Se stai usando un quadro che può includere un aiuto di qualche tipo per fare questo, ma standard di PHP non lo fa.

Ritiene che la risposta alla tua domanda?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top