数组中的SQL查询()

时间:2015-12-31 09:01:05

标签: php sql

       $sql = "SELECT * FROM themes ORDER BY id DESC";
        $query = mysqli_query($db_conx, $sql); 
            while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
            $t_id = $row["id"];
            $t_screen = $row["screen_img"];
            $t_name = $row["name"];
            $t_type = $row["type"];
            $t_cat = $row["category"];
            $t_reg_price = $row["regular_price"];
            $t_sale_link = $row["sale_link"];
            if ($t_type == 'html5'){
                $color_code = "ec6334";
            } elseif ($t_type == 'wp'){
                $color_code = "288aad";
            } elseif ($t_type == 'joomla'){
                $color_code = "f2b600";
            } elseif ($t_type == 'jquery'){
                $color_code = "0f1c2e";
            }

            $theme_array = array (
            array ("id" => "{$t_name}",
            "url" => "../demos/{$t_name}",
             "preview" => "../../projects/themes/{$t_screen}",
            "type" => "{$t_type}",
           "type_color" => "{$color_code}",
           "ddn" => "{$t_sale_link}"
             ),

             );
            }

此脚本仅显示循环中的最后结果我正在尝试显示项目 当我在while循环中尝试array()时,它显示错误我是php的新手,因此无法解决此问题

4 个答案:

答案 0 :(得分:0)

您需要构建如下所示的数组:

$theme_array[] = array ("id" => "{$t_name}",
            "url" => "../demos/{$t_name}",
             "preview" => "../../projects/themes/{$t_screen}",
            "type" => "{$t_type}",
           "type_color" => "{$color_code}",
           "ddn" => "{$t_sale_link}"
             ); 

答案 1 :(得分:0)

需要改变

        $theme_array = array (
        array ("id" => "{$t_name}",
        "url" => "../demos/{$t_name}",
         "preview" => "../../projects/themes/{$t_screen}",
        "type" => "{$t_type}",
       "type_color" => "{$color_code}",
       "ddn" => "{$t_sale_link}"
         ),

        $theme_array[] = array (
        array ("id" => "{$t_name}",
        "url" => "../demos/{$t_name}",
         "preview" => "../../projects/themes/{$t_screen}",
        "type" => "{$t_type}",
       "type_color" => "{$color_code}",
       "ddn" => "{$t_sale_link}"
         ),

这应该有效

答案 2 :(得分:0)

$theme_array循环之前将array初始化为while()并使用变量。请尝试以下方法:

<?php 
$sql = "SELECT * FROM themes ORDER BY id DESC";
$query = mysqli_query($db_conx, $sql);
$theme_array = array();
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
    $i=0;
    $t_id = $row["id"];
    $t_screen = $row["screen_img"];
    $t_name = $row["name"];
    $t_type = $row["type"];
    $t_cat = $row["category"];
    $t_reg_price = $row["regular_price"];
    $t_sale_link = $row["sale_link"];
    if ($t_type == 'html5'){
        $color_code = "ec6334";
    } elseif ($t_type == 'wp'){
        $color_code = "288aad";
    } elseif ($t_type == 'joomla'){
        $color_code = "f2b600";
    } elseif ($t_type == 'jquery'){
        $color_code = "0f1c2e";
    }

    $theme_array[$i] = array (
            array ("id" => "{$t_name}",
            "url" => "../demos/{$t_name}",
            "preview" => "../../projects/themes/{$t_screen}",
            "type" => "{$t_type}",
            "type_color" => "{$color_code}",
            "ddn" => "{$t_sale_link}"
            )
            );
    $i++;
}
?>

答案 3 :(得分:0)

您正在循环中初始化和附加数组。

在遍历任何循环时,迭代中的新值会覆盖旧值。

在您的情况下,数组只获得最后一个值。

您需要在while循环之前初始化数组。

并在while循环中追加数组中的值。

更改

while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 

$theme_array = array();
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 

$theme_array = array (
            array ("id" => "{$t_name}",
            "url" => "../demos/{$t_name}",
             "preview" => "../../projects/themes/{$t_screen}",
            "type" => "{$t_type}",
           "type_color" => "{$color_code}",
           "ddn" => "{$t_sale_link}"
             ),
             );

$theme_array[] = array ("id" => "{$t_name}",
            "url" => "../demos/{$t_name}",
             "preview" => "../../projects/themes/{$t_screen}",
            "type" => "{$t_type}",
           "type_color" => "{$color_code}",
           "ddn" => "{$t_sale_link}"
             );