Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

rochajl

Definir tamanho fixo array

Recommended Posts

Preciso carregar as informações de um arquivo xml para um array (isto eu consigo fazer). O problema é que o arquivo xml pode conter 50 registros, mas só posso carregar 20 no array para visualizar na tela via tabela. Como poderia fazer algo assim? Segue código onde não consigo fazer esta limitação:

 

<?php
$xml = simplexml_load_file("tabela.xml");
echo "<table border=0 cellspacing=0>";
$numCols = 2; // Número de Colunas
$i=0;
foreach($xml as $tabela){
	print($i%$numCols==0) ? "<tr>\n" : null;
   	print( "<td width='15' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
	print( "<td width='387' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:28px'><strong>" . $tabela->descricao . "</strong></td>\n" );
   	print( "<td width='75' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:35px' align=right><strong>" . $tabela->preco .'</strong><br/>'  . "</td>\n" );
   	print( "<td width='18' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
	print($i%$numCols==$numCols-1) ? "</tr>\n" : null;
	$i++;
	} //fim do foreach
	echo "</table>";
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Quando o contador chegar a 20 use o break;

 

<?php

$i = 0;
foreach( range( 1, 50 ) as $value )
{
  if( $i > 19 )
     break;
  echo $value, '<br/>';
  $i++;
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não vi nenhum array no código que você passou. Você se refere ao loop ou ao objeto XML retornado pelo simplexml?

 

Se você quer que o seu loop rode apenas 20 vezes faça o seguinte:

 

<?php
$xml = simplexml_load_file("tabela.xml");
echo "<table border=0 cellspacing=0>";
$numCols = 2; // Número de Colunas
$i=0;
foreach($xml as $tabela){
print($i%$numCols==0) ? "<tr>\n" : null;
print( "<td width='15' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
print( "<td width='387' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:28px'><strong>" . $tabela->descricao . "</strong></td>\n" );
print( "<td width='75' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:35px' align=right><strong>" . $tabela->preco .'</strong><br/>'  . "</td>\n" );
print( "<td width='18' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
print($i%$numCols==$numCols-1) ? "</tr>\n" : null;
if ($i == 19) {
	break;
}
$i++;
} //fim do foreach
echo "</table>";
?>

 

EDIT: Não vi que o Carlos postou. :P

Compartilhar este post


Link para o post
Compartilhar em outros sites

Não vi nenhum array no código que você passou. Você se refere ao loop ou ao objeto XML retornado pelo simplexml?

 

Se você quer que o seu loop rode apenas 20 vezes faça o seguinte:

 

<?php
$xml = simplexml_load_file("tabela.xml");
echo "<table border=0 cellspacing=0>";
$numCols = 2; // Número de Colunas
$i=0;
foreach($xml as $tabela){
print($i%$numCols==0) ? "<tr>\n" : null;
print( "<td width='15' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
print( "<td width='387' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:28px'><strong>" . $tabela->descricao . "</strong></td>\n" );
print( "<td width='75' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x style='font-size:35px' align=right><strong>" . $tabela->preco .'</strong><br/>'  . "</td>\n" );
print( "<td width='18' height='45' background='imagens/fundo_itens.jpg' background-repeat:repeat-x>" . " " . "</td>\n" );
print($i%$numCols==$numCols-1) ? "</tr>\n" : null;
if ($i == 19) {
	break;
}
$i++;
} //fim do foreach
echo "</table>";
?>

 

EDIT: Não vi que o Carlos postou. :P

 

É isso aí. Funcionou. Muito Obrigado!!!

Compartilhar este post


Link para o post
Compartilhar em outros sites

Se você tem PHP 5, usando a SplFixedArray você pré-determina quantos items o array poderá ter no máximo.

 

O único problema dela é que, que mesmo ela assinando contrato com ArrayAccess ela modifica o tipo do argumento usado nos métodos dessa interface forçando-os a serem inteiros, ao invés de inteiros ou strings.

 

Isso significa que você só poderá usar índices numéricos com essa classe nativa, o que, ao que parece, não será problema para ti.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.