2012-02-25 2 views
0

매순간 이러한 항목이 임의의 순서로 표시되게하려면 어떻게해야합니까 ??이 코드에 PHP rand 함수 추가

  <?php 
      for($i = 1; $i <= 5; $i++) { 
      $nameN = "name{$i}"; 
      $$nameN = get_post_meta(get_the_ID(), "ch_client_name{$i}", TRUE); 
      // or $name[$i], if you can 
      ?> 

      <li data-id="<?php the_ID(); ?>" class="<?php echo $clientterms; ?> portfolio-item" data-type="<?php echo $clientterms; ?>"> 
       <?php echo $$nameN; ?> 
      </li> 

      <?php 
      } ?> 
+0

가변 변수? 왜? 심각하게 PHP에있는 사람들을위한 장소가 거의 없습니다. 왜 정상적인 배열을 사용하지 않는가? 전반적으로 해킹이 적다 ... – xfix

답변

1
<?php 
$numbers = array(1,2,3,4,5); 
for ($i = 1; $i <= 5; $i++) { 
    $r = rand(0, count($numbers) - 1); 
    $nth = $numbers[$r]; 
    unset ($numbers[$r]); 
    array_unshift ($numbers, array_shift ($numbers)); 
    //Other part of the code 

작동합니다. 기본적으로 표시되지 않은 임의의 항목을 선택하여 배열에서 제거합니다. $nth실제 항목을 저장합니다.

0

왜 각 클라이언트를 다른 메타 값에 추가합니까? 모든 클라이언트를 하나의 메타 값 - ch_client_name -에 추가 할 수 있습니다. 이렇게하면 구조가 단순 해집니다.

구조체를 변경하면 먼저 메타 값을 배열로 가져온 다음 shuffle을 사용하고 배열을 반복합니다.

$clientNames = get_post_meta(get_the_ID(), "ch_client_name"); 
shuffle($clientNames); 

foreach ($clientNames as $clientName){....