반응형
PHP로 사이트에서 6개의 상품을 랜덤하게 3개만 보여주는 방법입니다.
PHP 2차원 배열을 사용하였고 shuffle 함수를 이용하여 배열의 데이터를 조회하기 전에 섞어 주었습니다.
이렇게 되면 결과적으로 랜덤하게 데이터를 가져오게 되며, for 반복문을 배열의 개수가 아닌 임의의 숫자인 3을 넣어서 3개만 가져오 도록 처리를 하였습니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<?php
// 쇼핑몰 추천 상품을 출력한다.
// 총 6개의 상품에서 3개를 랜덤하게 보여준다.
$SHOPPING_ADV_ITEM[0]['img'] = 'https://shop-phinf.pstatic.net/20200727_199/1595831812180uaPOr_JPEG/33194145774411133_528693600.jpg?type=m510';
$SHOPPING_ADV_ITEM[0]['url'] = 'https://smartstore.naver.com/openpass/products/5033644963';
$SHOPPING_ADV_ITEM[1]['img'] = 'https://shop-phinf.pstatic.net/20200810_5/1597037256348T7skA_JPEG/34400644866122943_796718142.jpg?type=m510';
$SHOPPING_ADV_ITEM[1]['url'] = 'https://smartstore.naver.com/openpass/products/5055270928';
$SHOPPING_ADV_ITEM[2]['img'] = 'https://shop-phinf.pstatic.net/20200806_185/1596690958999NcgB6_JPEG/34051697632208950_1835702808.jpg?type=m510';
$SHOPPING_ADV_ITEM[2]['url'] = 'https://smartstore.naver.com/openpass/products/5049802756';
$SHOPPING_ADV_ITEM[3]['img'] = 'https://shop-phinf.pstatic.net/20200624_263/1592975262335qEpzG_JPEG/30338650826369045_1878469151.jpg?type=m510';
$SHOPPING_ADV_ITEM[3]['url'] = 'https://smartstore.naver.com/openpass/products/4985597377';
$SHOPPING_ADV_ITEM[4]['img'] = 'https://shop-phinf.pstatic.net/20200203_240/1580697863405nvtMK_JPEG/18059406019736162_949848309.jpg?type=m510';
$SHOPPING_ADV_ITEM[4]['url'] = 'https://smartstore.naver.com/openpass/products/4808789494';
// 미밴드
$SHOPPING_ADV_ITEM[5]['img'] = 'https://shop-phinf.pstatic.net/20200723_104/1595467504671Vxx9i_JPEG/32829838266370342_456966819.jpg?type=m510';
$SHOPPING_ADV_ITEM[5]['url'] = 'https://smartstore.naver.com/openpass/products/5028409352';
shuffle($SHOPPING_ADV_ITEM);
for ( $i=0; $i<3; $i++ )
{
?>
<div>
<div>
<a href="<?= $SHOPPING_ADV_ITEM[$i]['url'] ?>" target="_blank">
<img src="<?=$SHOPPING_ADV_ITEM[$i]['img']?>" alt="쇼핑몰 추천 상품[<?=$i?>]">
</a>
</div>
</div>
<?php
}
?>
</body>
</html>
w3cw3schools 가이드
PHP shuffle() Function
Example
Randomize the order of the elements in the array:
<?php
$my_array = array("red","green","blue","yellow","purple");
shuffle($my_array);
print_r($my_array);
?>
Definition and Usage
The shuffle() function randomizes the order of the elements in the array.
This function assigns new keys for the elements in the array. Existing keys will be removed (See Example below).
Syntax
shuffle(array)
Parameter Values
Parameter | Description |
array | Required. Specifies the array to use |
Technical Details
Return Value: | Returns TRUE on success or FALSE on failure |
PHP Version: | 4+ |
PHP Changelog: | PHP 4.2: The random number generator is seeded automatically |
More Examples
Randomize the order of the elements in the array:
<?php
$my_array = array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow","e"=>"purple");
shuffle($my_array);
print_r($my_array);
?>
반응형