在Flex项目中,我有一个包含对象的数组。我想将这个数组保存在mysql表格的单元格中,以及其他一些基本信息,如title和id。
编辑:只是澄清,因为我似乎得到了解释如何回显所有行的响应...我试图回应序列化并放置在单个单元格中的数组的内容。这个数组中有对象。所以,我这里有这个代码序列化数组,并将其与其他信息一起插入我的数据库中:
function submitLogDbObj($array,$id,$title)
{
$title=mysql_real_escape_string($title);
return mysql_query("INSERT INTO logs (text,id,title) VALUES ('".serialize($array)."','$id','$title')");
}
然后进行测试我试图创建一个循环,以一种看起来像对话的方式显示日志......
我的数组中的对象看起来像:
[1]
icon = ""
msg = "this is a test"
name = "Them: "
systemMsg = 0
[2]
icon = ""
msg = "yep it sure is"
name = "You: "
systemMsg = 0
所以这就是我到目前为止所做的,但它不起作用!如何创建一个循环,从数据库中获取该数组,对其进行反序列化,然后以看起来像聊天日志的方式回显该数据?
谢谢!
<?php
include_once("dbinfo.php");
$id= $_GET['id'];
$result = mysql_query("SELECT text,title FROM logs WHERE id='$id'")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($result)
{
$log = unserialize($row['text']);
echo 'starting loop!';
echo "<ul>";
/* im not sure how to represent the length of an array in php thats why i just have $log.length */
for ($i = 1; $i <=$log.length; $i++)
{
echo "<div id='logbox'>";
echo "<li>";
$name=$log[$i]['name'];
$msg=$log[$i]['msg'];
echo "$name - $msg";
echo "</li>";
echo "</div>";
echo "<br />";
}
echo "</ul>";
echo 'finished loop!';
}
else
{
echo "Looks like this chat log has been deleted. Sorry!";
}
答案 0 :(得分:1)
嗯,这里有一些可能更好的事情:
count( $array )
或sizeof( $array )
length
在此上下文中未定义,因此$ log.length表示“$ log.length”。foreach( $set as $val)
或foreach( $set as $key => $val )
while($row = mysql_fetch_array($result)){
或do... while
(见下文)。除非你明确地且有意识地只想要一个,否则最好使用它。如果你只想要一个,那么在查询中添加一个限制。mysql_real_escape_string
。br
内包围某些内容,则真的不应该需要div
。改进的代码:
$row = mysql_fetch_array($result);
// row could be empty if there were no results
if($row)
{
// we've already grabbed the first value, so we need
// to invert while into do... while.
do
{
$log = unserialize($row['text']);
echo "<ul>";
foreach( $log as $line )
{
// Are you sure this should be outside of the li?
echo "<div id='logbox'>";
echo "<li>";
$name=$line['name'];
$msg=$line['msg'];
echo "$name - $msg";
echo "</li>";
echo "</div>";
}
echo "</ul>";
}
while( $row = mysql_fetch_array($result) );
echo 'finished loop!';
}
else
{
echo "Looks like this chat log has been deleted. Sorry!";
}
答案 1 :(得分:0)
首先,养成indenting your code properly的习惯,在寻找错误时会为你节省很多挫折。
你不需要知道数组的长度,你可以只使用while循环:(从臀部开始编码,如果你收到错误,请告诉我)
$result = mysql_query("......") or die("Query failed");
//Keep going while $row isn't FALSE
//mysql_fetch_array returns false when there are no more rows
while($row = mysql_fetch_array($result)){
//You can close PHP tags here and insert the
//variables in the HTML, it often looks neater
//and your editor can colour code HTML, helping
//you to find problems
?>
<div>
<li><?php echo $row['name'] ?></li>
<li><?php echo $row['msg'] ?></li>
</div>
<?php
}
有关更多示例,请参阅this tutorial的“fetch array while循环”。
答案 2 :(得分:0)
$result = mysql_query("SELECT text,title FROM logs WHERE id='$id'")
echo "<ul>";
while ($row = mysql_fetch_assoc($results)) {
$name = $row['name'];
$msg = $row['msg'];
//display data how ever you want
}
echo "</ul>";