5

这个主题与我在 dba.stackexchange.com 上的其他问题有点相似,但问题不同

我的数据库中有两张表,一张是 user_app 表,其中存储了用户信息,表如下:

+--------------+--------------+------+-----+-------------------+-----------------------------+
| Field        | Type         | Null | Key | Default           | Extra                       |
+--------------+--------------+------+-----+-------------------+-----------------------------+
| id           | int(11)      | NO   | PRI | NULL              | auto_increment              |
| apikey       | varchar(32)  | NO   |     | NULL              |                             |
| app_id       | varchar(16)  | NO   |     | NULL              |                             |
| appidt       | varchar(100) | NO   |     | NULL              |                             |
| imei_num     | varchar(32)  | NO   | MUL | NULL              |                             |
| app_version  | varchar(20)  | NO   |     | NULL              |                             |
| package_name | varchar(100) | NO   | MUL | NULL              |                             |
| stamp        | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| sdk_version  | float        | NO   |     | NULL              |                             |
+--------------+--------------+------+-----+-------------------+-----------------------------+

其他表是创意表,我通过它向用户检索广告:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| id           | int(11)      | NO   | PRI | NULL    | auto_increment |
| campaign_id  | tinyint(4)   | NO   |     | NULL    |                |
| status       | tinyint(4)   | NO   |     | NULL    |                |
| type         | varchar(20)  | NO   |     | NULL    |                |
| title        | varchar(40)  | NO   |     | NULL    |                |
| description  | varchar(100) | NO   |     | NULL    |                |
| iconlink     | text         | NO   |     | NULL    |                |
| marketlink   | text         | NO   |     | NULL    |                |
| app_id       | varchar(16)  | YES  |     | NULL    |                |
| package_name | varchar(200) | YES  | MUL | NULL    |                |
+--------------+--------------+------+-----+---------+----------------+

这是我的php代码:

    $rss = $mysqli->query(" SELECT id as cid,
                        title, description, 
                        iconlink, marketlink, 
                        package_name FROM `creative` 
                        WHERE package_name <> '".$pkg."' AND id = 16 "
                    );

$arr = array();

while($obj = $rss->fetch_object()) {
    $arr = $obj;
    }
        //$uappid = $arr -> uid; //user app id for track
        $cid = $arr -> cid; // creative id
        //$campid = $arr -> campaign_id;
        $title = $arr -> title;
        $desc = $arr -> description;
        $ilink = $arr -> iconlink;
        $mlink = $arr -> marketlink;

$data = array('id'=>"$cid", 'title'=> "$title", 'description'=> "$desc", 'iconlink'=> "$ilink",'marketlink'=>"$mlink");

        $creative = json_encode($data); //return creative json if imei is there

        $response = array('success' => "true", 'result'=>"$creative", 'delay'=>"120000"); 

        echo json_encode($response); //return ad in json

在这种情况下,我必须每 24 小时向用户发送一次广告,我以 json 格式发送。此外,如果用户安装了我们不同的应用程序,我必须在一整天内只向他们发送一个广告。在此,package_name 代表我们的应用程序,我试图通过使用时间标志条目来实现这一点,如下所示:

$flag = date('dmy');

但是如何实现每天一个广告,我只是在研究这个。请看这个问题。

更新:我很抱歉,我之前没有提到它,但我想在很多广告请求中只向用户发送一个广告。例如,用户可以发送请求数量并安装我们的应用程序,然后他不会在每个请求上获得相同的广告。他一整天只收到一个广告。

4

1 回答 1

1

尝试添加结果查询的多维数组:

while($obj = $rss->fetch_object()) {
     $array[$obj->cid]['cid'] = $obj->cid;
     $array[$obj->cid]['title'] = $obj->title;
     $array[$obj->cid]['description'] = $obj->description;
     $array[$obj->cid]['iconlink'] = $obj->iconlink;    
     $array[$obj->cid]['marketlink'] = $obj->marketlink; 
     $array[$obj->cid]['package_name'] = $obj->package_name; 
    }


    echo '<pre>';
    var_dump(json_encode($array)); //return ad in json
    echo '</pre>';
于 2012-08-16T11:46:10.553 回答