对于每个结果MySQL查询,推阵列(复杂) [英] For each result in MySQL query, push to array (complicated)
问题描述
好了,这就是我要做的。我运行一个MySQL查询最新的帖子。对于每个返回的行,我需要到该行的ID推到一个数组,然后在数组中的ID,我需要从行添加更多的数据。多维数组。
下面是我的code迄今。
$查询=SELECT * FROM帖子ORDER BY ID DESC LIMIT 10;
$结果= mysql_query($查询);而($行= mysql_fetch_array($结果)){ 的$ id = $行[身份证];
$ POST_TITLE = $行[称号];
$ POST_TEXT = $行[文字];
$ post_tags = $行[标签];
$ post_category = $行[类别];
$ POST_DATE = $行[日];}
正如你可以看到我还没有对数组任何事情。以下是我正在寻找一个理想的结构,只是柜面你困惑。
主阵列我想你可以调用它。我们就称这个数组$帖子。
在这阵,我有我的MySQL查询返回的每一行一个阵列。
这些阵列内就有$ POST_TITLE,$ POST_TEXT等。
我如何做到这一点?我很困惑..一个例子是真的AP preciated。
-Dylan
$高手[$ ID] ['POST_TITLE'] = $ POST_TITLE;
$主[$ ID] ['POST_TEXT'] = $ POST_TEXT;
//等
,或较少code。这一个,你可以摆脱在这里你将所有这些变量:
$高手[$行[身份证] ['POST_TITLE'] = $行[称号];
$主[$行[身份证] ['POST_TEXT'] = $行[文字];
//等
编辑在回答评论:
的foreach($主为$行)
{
回声$行['POST_TITLE'];
}//或者如果你知道ID回声$行[$ ID] ['POST_TITLE'];
Okay, here's what I'm trying to do. I am running a MySQL query for the most recent posts. For each of the returned rows, I need to push the ID of the row to an array, then within that ID in the array, I need to add more data from the rows. A multi-dimensional array.
Here's my code thus far.
$query = "SELECT * FROM posts ORDER BY id DESC LIMIT 10";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$id = $row["id"];
$post_title = $row["title"];
$post_text = $row["text"];
$post_tags = $row["tags"];
$post_category = $row["category"];
$post_date = $row["date"];
}
As you can see I haven't done anything with arrays yet. Here's an ideal structure I'm looking for, just incase you're confused.
The master array I guess you could call it. We'll just call this array $posts. Within this array, I have one array for each row returned in my MySQL query. Within those arrays there is the $post_title, $post_text, etc.
How do I do this? I'm so confused.. an example would be really appreciated.
-Dylan
$master[$id]['post_title'] = $post_title;
$master[$id]['post_text'] = $post_text;
// etc
or, less code. With this one, you can get rid of where you set all those variables:
$master[$row["id"]]['post_title'] = $row["title"];
$master[$row["id"]]['post_text'] = $row["text"];
// etc
Edit in answer to comment:
foreach( $master as $row )
{
echo $row['post_title'];
}
// or if you know the id
echo $row[$id]['post_title'];
这篇关于对于每个结果MySQL查询,推阵列(复杂)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!