$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的新手,因此无法解决此问题
答案 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}"
);