링크에 미리 주소 넣기

링크에 미리 주소 넣기

QA

링크에 미리 주소 넣기

본문

안녕하세요.

 

게시판에 글을 쓸때 하단 링크1, 링크2 에 관련링크를 입력 합니다.
그런데 이 부분에 글쓸때마다 랜덤하게 주소를 넣고 싶습니다.

 

예) 랜덤값

링크 :  https://link1.com 부터 https://link10.com 중에 랜덤으로 링크를 넣을수 있는 방법이 있을까요?

 

https://link1.com
https://link2.com
https://link3.com
https://link4.com
https://link5.com
https://link6.com
https://link7.com
https://link8.com
https://link9.com
https://link10.com

 

이렇게 링크를 저장해두고 각 게시글 마다 1~10 까지의 링크를 랜덤으로 넣을수 있는 방법이 있을까요?

 

글쓸때마다 제가 입력하지 않아도 위와 같이 입력되어 있게 하고 싶은데요.
도움을 주실 분이 있을까요?
이런 생각을 안했는지...검색을 해도 안나와서 문의 드립니다.

감사합니다.

이 질문에 댓글 쓰기 :

답변 8

view.skin.php 하단에

 


<script>
link_1 = "https://link1.com";
link_2 = "https://link2.com";
link_3 = "https://link3.com";
link_4 = "https://link4.com";
link_5 = "https://link5.com";
link_6 = "https://link6.com";
link_7 = "https://link7.com";
link_8 = "https://link8.com";
link_9= "https://link9.com";
link_10 = "https://link10.com";
for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
links = [];
for (i = 0; i < linkTotal; i++) links.push(i + 1);
for (j in links) {
    rN = Math.floor(Math.random() * links.length);
    changeN = links[rN];
    links[rN] = links[j];
    links[j] = changeN;  
}
wr_link1.value = this["link_" + links[0]];
wr_link2.value = this["link_" + links[1]];
</script>

 

만일 글 수정시에는 변하지 않고 글작성시에만 입력되게 하려면 아래처럼요.

 


<script>
if (wr_link1.value.trim() === "" && wr_link2.value.trim() === "") {
    link_1 = "https://link1.com";
    link_2 = "https://link2.com";
    link_3 = "https://link3.com";
    link_4 = "https://link4.com";
    link_5 = "https://link5.com";
    link_6 = "https://link6.com";
    link_7 = "https://link7.com";
    link_8 = "https://link8.com";
    link_9= "https://link9.com";
    link_10 = "https://link10.com";
    for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
    links = [];
    for (i = 0; i < linkTotal; i++) links.push(i + 1);
    for (j in links) {
        rN = Math.floor(Math.random() * links.length);
        changeN = links[rN];
        links[rN] = links[j];
        links[j] = changeN;  
    }
    wr_link1.value = this["link_" + links[0]];
    wr_link2.value = this["link_" + links[1]];
}
</script>

 

만일 link_1 부터 link_10 까지만을 따로 문서로 빼고 싶으면 그 부분만 php 로 만들어서

그 부분에 인클루드문을 주면 됩니다.

도메인명에 1부터 10까지요? 아니면 그냥 도메인이 10까지 있는데 이거를 랜덤으로 넣은다고요?

아래 소스코드 맞는진 모르겠네요

 

 



$links = array(
    "https://link1.com",
    "https://link2.com",
    "https://link3.com",
    "https://link4.com",
    "https://link5.com",
    "https://link6.com",
    "https://link7.com",
    "https://link8.com",
    "https://link9.com",
    "https://link10.com"
);

$random_link = $links[array_rand($links)];

echo $random_link;


이걸 게시판이 직접 넣지 않고 setup.php파일에 링크를 올려두고 해당 링크들을 랜덤으로 불러오려면 어떻게 해야 하나요? seeup.php파일안에
$doc1 = "https://link1.com";
$doc2 = "https://link2.com";
$doc3 = "https://link3.com";
$doc4 = "https://link4.com";
$doc5 = "https://link5.com";
$doc6 = "https://link6.com";
$doc7 = "https://link7.com";
$doc8 = "https://link8.com";
$doc9 = "https://link9.com";
$doc10 = "https://link10.com";
이런식으로 링크를 저장해두고 그때그때 수정을 하고 싶은데 이럴땐 어떻게 해야 하죠?
$links = array( 요 아래부분을 <?php echo $doc1;?>~<?php echo $doc10;?> 이런식으로 교체를 하면 될까요?

<?php
$doc1 = "https://link1.com";
$doc2 = "https://link2.com";
$doc3 = "https://link3.com";
$doc4 = "https://link4.com";
$doc5 = "https://link5.com";
$doc6 = "https://link6.com";
$doc7 = "https://link7.com";
$doc8 = "https://link8.com";
$doc9 = "https://link9.com";
$doc10 = "https://link10.com";
?>

이렇게 php파이로 빼놓고 

 

<script>
if (wr_link1.value.trim() === "" && wr_link2.value.trim() === "") {
    link_1 = "<?php echo $doc1;?>";
    link_2 = "<?php echo $doc2;?>";
    link_3 = "<?php echo $doc3;?>";
    link_4 = "<?php echo $doc4;?>";
    link_5 = "<?php echo $doc5;?>";
    link_6 = "<?php echo $doc6;?>";
    link_7 = "<?php echo $doc7;?>";
    link_8 = "<?php echo $doc8;?>";
    link_9= "<?php echo $doc19?>";
    link_10 = "<?php echo $doc10;?>";
    for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
    links = [];
    for (i = 0; i < linkTotal; i++) links.push(i + 1);
    for (j in links) {
        rN = Math.floor(Math.random() * links.length);
        changeN = links[rN];
        links[rN] = links[j];
        links[j] = changeN;  
    }
    wr_link1.value = this["link_" + links[0]];
    wr_link2.value = this["link_" + links[1]];
}
</script>

 

이런식으로 하면 된다는말씀이신가요? 초보자에 입장이라 번거렵게 해드려 죄송합니다.

setup.php 는 아래처럼

link_1 = "https://link1.com";
link_2 = "https://link2.com";
link_3 = "https://link3.com";
link_4 = "https://link4.com";
link_5 = "https://link5.com";
link_6 = "https://link6.com";
link_7 = "https://link7.com";
link_8 = "https://link8.com";
link_9= "https://link9.com";
link_10 = "https://link10.com";

-----

<script>
<?php include_once('setup.php'); ?>
for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
links = [];
for (i = 0; i < linkTotal; i++) links.push(i + 1);
for (j in links) {
    rN = Math.floor(Math.random() * links.length);
    changeN = links[rN];
    links[rN] = links[j];
    links[j] = changeN; 
}
wr_link1.value = this["link_" + links[0]];
wr_link2.value = this["link_" + links[1]];
</script>

setup.php 는 아래처럼

link_1 = "https://link1.com";
link_2 = "https://link2.com";
link_3 = "https://link3.com";
link_4 = "https://link4.com";
link_5 = "https://link5.com";
link_6 = "https://link6.com";
link_7 = "https://link7.com";
link_8 = "https://link8.com";
link_9= "https://link9.com";
link_10 = "https://link10.com";

-----

<script>
<?php include_once('setup.php'); ?>
for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
links = [];
for (i = 0; i < linkTotal; i++) links.push(i + 1);
for (j in links) {
    rN = Math.floor(Math.random() * links.length);
    changeN = links[rN];
    links[rN] = links[j];
    links[j] = changeN; 
}
wr_link1.value = this["link_" + links[0]];
wr_link2.value = this["link_" + links[1]];
</script>

 

이렇게 해봤는데 지금 이 페이지가 작동하지 않습니다. 라고하네요.ㅠ.ㅠ

관련링크 1은빼고 관련링크2 에만 주소를 넣고 싶습니다.

    <!-- 관련링크 시작 { -->
    <br>

    <section id="bo_v_link">
        <ul>
        <?php
         //링크
        $cnt = 0;
        for ($i=2; $i<=count($view['link']); $i++) {
            if ($view['link'][$i]) {
                $cnt++;
                $link = cut_str($view['link'][$i], 70);
            ?>
            <li>
                <i class="fa fa-link" aria-hidden="true"></i> <a href="<?php echo $view['link_href'][$i] ?>" target="_blank">
                    
                    <strong><?php echo $link ?></strong>
                </a>
                <span class="bo_v_link_cnt"><?php echo $view['link_hit'][$i] ?>회 연결</span>
            </li>
            <?php
            }
        }
        ?>
        </ul>
    </section>
    <!-- } 관련링크 끝 -->

 

    <script>
<?php include_once('setup.php'); ?>
for (linkTotal = 0; this["link_" + (linkTotal + 1)]; linkTotal++);
links = [];
for (i = 0; i < linkTotal; i++) links.push(i + 1);
for (j in links) {
    rN = Math.floor(Math.random() * links.length);
    changeN = links[rN];
    links[rN] = links[j];
    links[j] = changeN; 
}
wr_link1.value = this["link_" + links[0]];
wr_link2.value = this["link_" + links[1]];
</script>

 

셋업php파일에는

$soc1 = "https://link1.com";
$soc1 = "https://link2.com";
$soc1 = "https://link3.com";
$soc1 = "https://link4.com";
$soc1 = "https://link5.com";
$soc1 = "https://link6.com";
$soc1 = "https://link7.com";
$soc1 = "https://link8.com";
$soc1 = "https://link9.com";
$soc1 = "https://link10.com";

 

먹히질 않네요.ㅠ.ㅠ

그누 최상위라면 ../setup.php 로 해 보세요.
bbs 에 넣는다면 ./setup.php 입니다.

그리고 link_1 부터 link_10 까지 변수명 바꾸지 마세요.
왜 자꾸 달러를 붙여서 $doc1 등으로 비꾸시나요?

값을 미리 array이 넣어놓고 출력은 랜덤으로 0~count(array)임의의 숫자를 뽑아서 출력하면 되지 않을까요??


$url = array();
for($i = 1;  $i<=10; $i++){
    $url[] = "https://link".$i.".com";
}
echo $url[rand(0, count($url)-1)];

하나 더 필요하다면 unet($url[rand된값]) 하고 다시 rand로 뽑으면 됩니다.

링크주소 여러개를 php파일에 저장해둔다고 치면 전체적인 소스내용이 어떻게 될까요? 글 쓸때 불러오는게 아닌 view.skin.php 에서 직접 입력됐으면 하는데요.

답변을 작성하시기 전에 로그인 해주세요.
전체 123,674 | RSS
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT