2

In a Flex project, I have an array with objects in it. I want to save this array in a cell on a mysql table along with some other basic info like title, and an id.

EDIT: just clarifying since i seem to be getting responses explaining how to echo all the rows... I'm trying to echo the contents of an array that was serialized and placed in a single cell. This array has objects in it.

So, I have this code here to serialize the array, and insert it along with the other info into my DB:

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')");

}

Then for a test i'm trying to make a loop that will display the log in a way that looks like a conversation...

an object in my array would look something like:

[1] 
  icon = ""
  msg = "this is a test"
  name = "Them: "
  systemMsg = 0
[2]
  icon = ""
  msg = "yep it sure is"
  name = "You: "
  systemMsg = 0

So here's what i've got so far, but its not working! How can I make a loop that will take that array from the DB, unserialize it and then echo the convo in a way that looks like a chat log?

Thanks!

<?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!";
}

3 Answers 3

2

Well, there're a few things which could be better here:

  1. The length of an array is found through count( $array ) or sizeof( $array )
  2. $value . $otherValue means concatenate those two values. Since length is undefined in this context, $log.length means "$log.length".
  3. The best way to loop through an array is foreach( $set as $val) or foreach( $set as $key => $val )
  4. The preferred method of iterating through a SQL result is the while loop: while($row = mysql_fetch_array($result)){ or do... while (see below). Unless you specifically and consciously only want one, then it would be best to use that. And if you do only want one, then put a Limit in the query.
  5. Serialized arrays in databases has a redundant flavor. Are you sure that this is what you want?
  6. Your serialized array, before it is inserted, should also be run through mysql_real_escape_string.
  7. br really shouldn't be needed if you're surrounding something in its own div.
  8. Indent properly or the kitten of death will come for you.

The improved code:

$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!";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Makes sense! Thanks! and sorry about the indenting, stackoverflow messed it up for some reason, and I didnt feel like doing all the indenting using space since I cant tab on a stackoverflow post.
0

Firstly, get into the habit of indenting your code properly, it will save you a lot of frustration when looking for errors.

You don't need to know the length of the array, you can just use a while loop: (coding from the hip here so let me know if you get errors)

$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
}

See the "fetch array while loop" of this tutorial for more examples.

2 Comments

Thanks for the tip, but I do indent properly, stackoverflow just really messed up the indenting pretty bad and I didnt feel like indening it all properly over again. and 2 I'm familiar with what you're suggesting for displaying all the rows in the mysql table. BUT if you look at my post you'll see its a serialized array that was in a cell im trying to display.
Ah sorry I misread. use count($myVar) to get array length, and start $i at 0. To indent your code properly on SO just paste it, highlight it and click the {} icon (maybe you did that already, but usually works for me)
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>"; 

1 Comment

I'm familiar with this method for displaying all the rows from my query, but if you look at my post its a serialized array in a single cell that contains multiple objects that im trying to echo to the page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.