Creation of PHP for adsense random display

Creation of PHP for adsense random display

Created PHP for adsense random display.

There was also a plug-in, but it was troublesome to set up and it was a paid option, so

But after all, I wanted to make it with PHP for the first time in a while. Is big.

With the previous feeling, I feel that the connection of mysql is easier.

Object type, procedural type, I’m not sure, but there are few reference examples of procedural type.

This handling is procedural type, I only understand this, I can not understand

1. DB connection
$link = mysqli_connect(‘localhost’, ‘USER’, ‘PASSWORD’, ‘DBNAME’);

2, character set
mysqli_set_charset($link, “utf8mb4”);

3. SQL inquiry
$sql = “SELECT hyouzimei, mozi FROM wp_astcc_blog_ad_random ORDER BY RAND() LIMIT 1;”;

4, actually throw
$result = mysqli_query($link, $sql);

5, result acquisition
This command didn’t come out easily, half forcibly.
I don’t know what the associative array is

$row = mysqli_fetch_assoc($result);

6. Get results
$hyouzimei = $row[‘hyouzimei’];
$mozi = $row[‘mozi’];

I wonder what happens if there are multiple lines, this time I specify one line so I do not care
You may be in trouble in the future.

I will post the file for the time being.
Creating a database is troublesome, so explanations are omitted this time.

PHP file
ad_random_php

**************************************************
**************************************************
**************************************************
Below PHP file
**************************************************
**************************************************
**************************************************

*/
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

$ad_size_A = 0;
if(strcmp($ad_size, “指定なし”) == 0){
$ad_sizeA = 1;
}
else if(strcmp($ad_size, “縦長小”) == 0){
$ad_size_A = 10;
}
else if(strcmp($ad_size, “縦長中”) == 0){
$ad_size_A = 11;
}
else if(strcmp($ad_size, “縦長大”) == 0){
$ad_size_A = 12;
}
else if(strcmp($ad_size, “横長小”) == 0){
$ad_size_A = 20;
}
else if(strcmp($ad_size, “横長中”) == 0){
$ad_size_A = 21;
}
else if(strcmp($ad_size, “横長大”) == 0){
$ad_size_A = 22;
}
else if(strcmp($ad_size, “スクエア小”) == 0){
$ad_size_A = 30;
}
else if(strcmp($ad_size, “スクエア中”) == 0){
$ad_size_A = 31;
}
else if(strcmp($ad_size, “スクエア大”) == 0){
$ad_size_A = 32;
}
else{
$ad_size_A = 99;
}
//print $ad_size.”=”.$ad_size_A.”
“;

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

$ad_syurui_A = 0;
if(strcmp($ad_syurui, “クリック型”) == 0){
$ad_syurui_A = 1;
}
else if(strcmp($ad_syurui, “報酬型”) == 0){
$ad_syurui_A = 2;
}
else{
$ad_syurui_A = 99;
}
//print $ad_syurui.”=”.$ad_syurui_A.”
“;

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

$ad_tagu_A = 0;
if(strcmp($ad_tagu, “指定なし”) == 0){
$ad_tagu_A = 1;
}
else if(strcmp($ad_tagu, “EA”) == 0){
$ad_tagu_A = 10;
}
else if(strcmp($ad_tagu, “自宅サーバー”) == 0){
$ad_tagu_A = 11;
}
else if(strcmp($ad_tagu, “ショッピングサイト”) == 0){
$ad_tagu_A = 12;
}
else if(strcmp($ad_tagu, “雑記”) == 0){
$ad_tagu_A = 13;
}
else if(strcmp($ad_tagu, “FX”) == 0){
$ad_tagu_A = 14;
}
else if(strcmp($ad_tagu, “English”) == 0){
$ad_tagu_A = 50;
}
else if(strcmp($ad_tagu, “English-EA”) == 0){
$ad_tagu_A = 51;
}
else if(strcmp($ad_tagu, “English-Notes”) == 0){
$ad_tagu_A = 52;
}
else{
$ad_tagu_A = 99;
}
//print $ad_tagu.”=”.$ad_tagu_A.”
“;

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

$ad_debaisu_A = 0;
if(strcmp($ad_debaisu, “指定なし”) == 0){
$ad_debaisu_A = 1;
}
else if(strcmp($ad_debaisu, “パソコン”) == 0){
$ad_debaisu_A = 2;
}
else if(strcmp($ad_debaisu, “タブレット”) == 0){
$ad_debaisu_A = 3;
}
else if(strcmp($ad_debaisu, “スマホ”) == 0){
$ad_debaisu_A = 4;
}
else{
$ad_debaisu_A = 99;
}
//print $ad_debaisu.”=”.$ad_debaisu_A.”
“;

////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

$link = mysqli_connect(‘localhost’, ‘USER’, ‘PASSWORD’, ‘DBNAME’);

mysqli_set_charset($link, “utf8mb4”);

/*
if(!$link){
die(“データベースに接続できません:” . mysqli_connect_error() . “\n”);
}
else{
echo “データベースの接続に成功しました。\n”;
}
*/

if($ad_size_A == 99 AND $ad_syurui_A == 99 AND $ad_tagu_A == 99 AND $ad_debaisu_A == 99){
//指定なしで出力する
$sql = “SELECT hyouzimei, mozi FROM wp_astcc_blog_ad_random ORDER BY RAND() LIMIT 1;”;
}
else{
$sql = “SELECT hyouzimei, mozi FROM wp_astcc_blog_ad_random WHERE “;

$sql_A=””;
if($ad_size_A == 99){
$sql_A = “”;
}
else{
$sql_A = “ookisa = $ad_size_A”;
}

if($ad_syurui_A == 99){
//$sql_A = “”;
}
else{
if($sql_A == “”){
$sql_A = “ad_syurui = $ad_syurui_A”;
}
else{
$sql_A = $sql_A.” AND “.”ad_syurui = $ad_syurui_A”;
}
}

if($ad_tagu_A == 99){
//$sql_A = “”;
}
else{
if($sql_A == “”){
$sql_A = “tagu = $ad_tagu_A”;
}
else{
$sql_A = $sql_A.” AND “.”tagu = $ad_tagu_A”;
}
}

if($ad_debaisu__A == 99){
$ad_debaisu__B = “”;
}
else{
if($sql_A == “”){
$sql_A = “debaisu = $ad_debaisu_A”;
}
else{
$sql_A = $sql_A.” AND “.”debaisu = $ad_debaisu_A”;
}
}

$sql = $sql.$sql_A.” ORDER BY RAND() LIMIT 1;”;
}

//$sql = “SELECT hyouzimei, mozi FROM wp_astcc_blog_ad_random ORDER BY RAND() LIMIT 1;”;
//SELECT hyouzimei, mozi FROM wp_astcc_blog_ad_random AND ad_syurui = $ad_syurui_A AND tagu = $ad_tagu_A AND debaisu = $ad_debaisu_A ORDER BY RAND() LIMIT 1;”;

//print $sql.”

“;

$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);
if($result->num_rows == 1){
//echo “ok

“;
}
else{
//echo “no

“;
$sql = “SELECT * FROM wp_astcc_blog_ad_random ORDER BY RAND() LIMIT 1;”;
$result = mysqli_query($link, $sql);
$row = mysqli_fetch_assoc($result);
}

$hyouzimei = “alt=”.”\””.$row[‘hyouzimei’].”\””;
$mozi = $row[‘mozi’];
$mozi = str_replace(“☆”, $hyouzimei, $mozi);
$mozi = “

“.$mozi.”

“;

//print ‘hyouzimei’.”=”.$row[‘hyouzimei’].”

“.’mozi’.”=”.$mozi.”

“;

print $mozi;

mysqli_close($link);

?>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Proudly powered by WordPress | Theme: HoneyBee by SpiceThemes