【问题标题】:How To Display random ads in my web site如何在我的网站上显示随机广告
【发布时间】:2016-05-04 10:41:50
【问题描述】:

嗨,我想在我的产品页面中显示随机广告,我的所有产品都在 while 循环中调用以显示在主页中,我想在这些产品之间随机添加来自后端的广告,其设计与 freekaamaal 中的产品显示相同

我如何在产品之间展示这些广告,你能给我一个想法吗??

see this for more info

<div class="products-grids">
<div class="col-md-12">
    <?php

        $id=$_GET['id'];

        $result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");
        while($ro = mysqli_fetch_array($result1))
        {
            $nam1=substr($ro['pne'],0,60);
            $url1=$ro['url'];
            $description1=substr($ro['description'],0,200);
            $price1=$ro['price'];
            $price11=$ro['price1'];
            $bid1=$ro['company'];
            $image=$ro['image_name'];
            $time=$ro['time'];
            $tag=$ro['tag'];
    ?>
    <div class="col-md-3">
        <div class="hentry post1 id="post-225396">

         Display Product in loop

        </div>
    </div>
    <?php } mysqli_close($con);?>
<!--    <div class="col-md-3">
        <div class="hentry post1 id="post-225396"><img src="ads.jpg"></div>
    </div>  -->
</div>
<div class="clearfix"> &nbsp;</div>

【问题讨论】:

  • 你还不了解stack-overflow的使用方法:How do I ask a good question?
  • 你是否只使用 php 或 cms,如 wordpress 之类的
  • 它在核心 PHP @nirpendra 上
  • 那么,任何答案都有效吗?

标签: php html


【解决方案1】:

反正你的问题太宽泛了。

这里是一步一步的解决方案。

前端 HTML + PHP

// 两个硬分区 + 三个随机广告,但只有一个广告触发。

<?php $random_number = rand(0,2); ?>

<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>

<?php $random_number = rand(0,2); ?>

<?php ads($random_number,0); ?>
<div>your image or whatever</div>
<?php ads($random_number,1); ?>
<div>your image or whatever</div>
<?php ads($random_number,2); ?>

比较函数

// 现在创建一个函数来比较生成的随机数是否等于广告的占位符。

function ads($rand_num, $placeholder){
    if($rand_num == $placeholder){
        echo "<div> YOUR ADS CONTENT </div>";

    }
}

注意:这并不意味着完美运行,它只是一个关于如何实现特定目标的大纲。

【讨论】:

  • 我正在使用 while 循环从数据库中获取产品,我想以相同的顺序显示这些产品之间的广告或任何内容,以便更多地了解这一点,您可以查看 freekaamaal.com 这是基于 WordPress 的他们显示随机广告代替产品,就像我在我的核心 PHP 中所做的一样,希望你明白了,顺便说一句,非常感谢你的理解:)
  • @anupdwivedi 在你的问题中添加你的循环和代码,我会尽力帮助你
  • 检查出有问题的亲爱的添加代码@Nirpendra Patel
  • @anupdwivedi 你没有在其中添加主要部分:循环
  • 从这里看到这条线在循环中显示产品 $counter = 1; while($ro = mysqli_fetch_array($result1)) { 他们我把展示产品放在循环中,他们唯一的输出展示,没有别的
【解决方案2】:

做这样的事情(我试图将其编码为易于理解):

<?php

function show_an_ad()
{
    $the_ad = '';

    switch (mt_rand(0,2))
    {
        case 0:
            $the_ad = 'ad0.jpg';
        break;
        case 1:
            $the_ad = 'ad1.jpg';
        break;
        case 2:
            $the_ad = 'ad2.jpg';
        break;
    }
    echo '<div class="col-md-3"><div class="hentry post1"><img src="'.$the_ad.'"></div></div>';
}


$ads_every_how_many_products = 3;


<div class="products-grids">
    <div class="col-md-12">
    <?php

        $id=$_GET['id'];

        $result1 = mysqli_query($con,"select * from products1 where sta='Active' order by id DESC LIMIT 0 , $resultsPerPage");

        $counter = 1;
        while($ro = mysqli_fetch_array($result1))
        {
            $nam1=substr($ro['pne'],0,60);
            $url1=$ro['url'];
            $description1=substr($ro['description'],0,200);
            $price1=$ro['price'];
            $price11=$ro['price1'];
            $bid1=$ro['company'];
            $image=$ro['image_name'];
            $time=$ro['time'];
            $tag=$ro['tag'];
    ?>
        <div class="col-md-3">
            <div class="hentry post1" id="post-225396">

             Display Product in loop

            </div>
        </div>
    <?php 
             if(!($counter%$ads_every_how_many_products))
             {
                 show_an_ad();
             }

             $counter++;
        }
        mysqli_close($con);
    ?>
    </div>
</div>
<div class="clearfix"> &nbsp;</div>

我这里没有 php 系统,所以可能会出现语法错误或其他问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    相关资源
    最近更新 更多