Page 1 of 1

Help with code - show results in 2 coluns.

Posted: Wed Dec 24, 2008 8:51 am
by alvaro
hi guys, i m a php beginner and i have a problem thats is.

Code: Select all

<td><? // COMEÇA LISTAR OPCIONAIS
    echo "<fieldset><legend><font color='$Cor1'>Features</font> </legend>";
    $op2 = explode("|",$dados[id_opcionais]);
    $total2 = count($op2);
    //echo $total;
 
        for($i2=0; $i2<$total2; $i2++) { 
        //echo $op[$i]."<br>";
            if($op2[$i2] != ""){
            $opc2 = mysql_fetch_array(mysql_query("SELECT * FROM $tabela7 WHERE id='$op2[$i2]'"));
            //echo "$opc2[nome]";
            
            
            echo "<input name='opcionais[]' type='checkbox' value='$opc2[id]' checked onClick='this.checked=true;'> $opc2[nome] ";
            }
        }
        echo "</fieldset><br>";?></td>
 
this code return me something like this but i need to show this in 2 coluns.

Image

if somebody can help me... thanks in advance.

Re: Help with code - show results in 2 coluns.

Posted: Wed Dec 24, 2008 10:14 am
by John Cartwright
I've never understood why this logic needs to be programmed logically. I find using CSS is much less complicated.

Code: Select all

 
echo "<div style='float; left; width: 49%;'><input name='opcionais[]' type='checkbox' value='". $opc2['id'] ."' checked onClick='this.checked=true;'>". $opc2['nome'] ."</div>";
You will also probably need to clear the floats.

Code: Select all

 
echo "<div style='clear: both;'></div>";
echo "</fieldset><br>";?>

Re: Help with code - show results in 2 coluns.

Posted: Wed Dec 24, 2008 10:42 am
by alvaro
Jcart wrote:I've never understood why this logic needs to be programmed logically. I find using CSS is much less complicated.

Code: Select all

 
echo "<div style='float; left; width: 49%;'><input name='opcionais[]' type='checkbox' value='". $opc2['id'] ."' checked onClick='this.checked=true;'>". $opc2['nome'] ."</div>";
Ur code works too - but i still got just 1 columns.

i think i can use something like this

Code: Select all

<?
$columns=2;
if ($total>0) { 
for ($i = 0; $i < $total; $i++) { 
if (($i%$columns)==0) { 
 
$colspan = $coluns+$coluns+$coluns;
?>

Re: Help with code - show results in 2 coluns.

Posted: Wed Dec 24, 2008 11:05 am
by John Cartwright
In my example, you may have to adjust the width depending on padding and margins. Usually 1% offset (each column being 49%) is enough, but not always.

Re: Help with code - show results in 2 coluns.

Posted: Wed Dec 24, 2008 4:29 pm
by alvaro
Jcart wrote:In my example, you may have to adjust the width depending on padding and margins. Usually 1% offset (each column being 49%) is enough, but not always.

I did like this and now works

Code: Select all

<style type="text/css">
.container
{
  width: 400;
}
 
.div_resultado
{
  float: left;
  width: 200px;
}
 
 
</style>

Code: Select all

echo "<div class='div_resultado' style='float; left; width: 49%;'><input name='opcionais[]' type='checkbox' value='". $opc2['id'] ."' checked onClick='this.checked=true;'>". $opc2['nome'] ."</div>";
            }
        }
        echo "<div style='clear: both;'></div>
Image

Thanks mate, apreciate ur help.