<?php

//$servername = "localhost";
//$username = "dbcnqcadmin";
//$password = "nimda6202";
//$dbname = "dbcnqc";

//$servername = "localhost";
//$username = "root";
//$password = "1111";
//$dbname = "dbcnqc";

$servername = "localhost";
$username = "dbcnqcadmin";
$password = "wo8wW95@";
$dbname = "dbcnqc";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
    die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn, "utf8");

if($_GET['lg']=="en/"){
    $lang_num=1;
}elseif($_GET['lg']=="zh-hk/"){
    $lang_num=2;
}elseif($_GET['lg']=="zh-cn/"){
    $lang_num=3;
}else{
    $lang_num=2;
}

$search_val ='';
$column='';
if($_GET['cat']!=''){
    $sql = sprintf('SELECT * FROM es_seo where url_rewrite="%s" and tbname="es_business_cat"',str_replace(";",'',$_GET['cat'])); 
    $result = $conn->query($sql);
    if ($result->num_rows > 0)
    {
        while ($row = $result->fetch_assoc())
        {
            $search_val = $row["itemid"];
        }
    }
    $column='node_id';

}elseif($_GET['typeid']!=''){
    $column='project_type';
    $search_val =$_GET['typeid'];
}



$sql = sprintf('SELECT t3.title'.$lang_num.' as pjtype, t1.title'.$lang_num.' as pjtitle, t2.url_rewrite as urlrewrite, t1.ff_thumb as pjimg, t4.url_rewrite as prjtype_url, t5.url_rewrite as cat_url, t6.title'.$lang_num.' as cat_name FROM es_project_list as t1 left join es_seo as t2 on(t1.id=t2.itemid AND t2.tbname="es_project_list") left join es_project_type as t3 on(t1.project_type=t3.id) left join es_seo as t4 on(t1.project_type=t4.itemid AND t4.tbname="es_project_type") LEFT JOIN es_seo as t5 on(t1.node_id=t5.itemid AND t5.tbname="es_business_cat") LEFT JOIN es_business_cat as t6 on(t1.node_id=t6.id) WHERE t1.status=1 AND t1.%s=%d ORDER BY t1.rank desc',$column,$search_val);
$result = $conn->query($sql);

if ($result->num_rows > 0)
{
    $ii=1;

    // output data of each row
    while ($row = $result->fetch_assoc())
    {
        //echo "name: " . $row["pjtype"] . "," . $row["pjtitle"] . "," . $row["urlrewrite"] . "," . $row["pjimg"] . "<br>";

        $title = $row["pjtitle"];
        $proj_type_name = $row["pjtype"];
        $prjtype_url = $row["prjtype_url"];
        $cat_url = $row["cat_url"];
        $cat_name=$row["cat_name"];
        /* img path */
        $thumb_val = explode(',', $row["pjimg"]);
        $thumb_total = ceil($thumb_val[0]);
        $thumb_path = 'project_list/' . $thumb_val[1];

        /* href (Link) */
        $urlpath = $row["urlrewrite"];
        $urlpath = ( $urlpath != '') ? ' href="' . '/' . $_GET['lg']. 'project-list' . '/' . $cat_url . '/' .$prjtype_url.'?item='. $urlpath . '" ' : '';
        $ww = 960;
        $hh = 360;
        $thumb = '';


        if (file_exists( $_SERVER['DOCUMENT_ROOT'].'/userfile/files/' . $thumb_path) && $thumb_total > 0)
        {
                list($width, $height, $type, $attr) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/userfile/files/' . $thumb_path);
                if($height>$width){                        
                    $bg_size='background-size: 350px auto;';
                }else{
                    $bg_size='background-size: auto 262px;';
                }
      
                $code_list.=''
                            . '<div class="item">
                                    <a style="display:block"' . $urlpath . '>
                                    <div style="width:350px; height:262px; background-image:url('.'/userfile/files/'. $thumb_path. '); background-repeat:no-repeat; '.$bg_size.' background-position: center center; margin-left: auto; margin-right: auto;">
                                        <div style="height: 40px; text-align: left; padding: 5px 0 0px 10px; text-shadow: 2px 2px 2px rgba(0,0,0,1),-1px -1px 2px rgba(0,0,0,1);">
                                            '.$cat_name.'
                                        </div>
                                        <div style="height:50px;background-color:rgba(0,0,0,0.65); color:#fff; margin-top:172px; text-align:left; padding: 5px 0 0 15px;">
                                            &gt; ' . $proj_type_name . '<br />' . $title .'
                                        </div>
                                    </div>
                                    </a>
                                </div>';
                




            $ii++;
        }else{
            $totalRecord=$totalRecord-1;
        }

    }
     echo $code_list;
}
else
{
    echo "0 results";
}
$conn->close();
?>