0

I have this in php

$comment = array;
while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
 $comment[$row['name']] = $row['comment'];
}
echo json_encode($comment);
Having these results
{"John":"Yo","Dan":"Hello","May":"Bye"}

The problem is I actually have two comments(Zup,Yo) for John, but as you can see, it only displays the last comment of John which is "Yo". So I wanted the results of John to be

{"John":["Yo","Sup"]}

^ is this possible?

How can I do that? Im sorry still having a hard time dealing with JSON. Thanks

This is actually my full code

while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
 $comment[$row['name']] = $row['comment'];
 $sql_dup = "SELECT name, COUNT(name) AS dup_count 
 FROM comment
 GROUP BY name
 HAVING (COUNT(name) > 1) 
 ";
 $sqlExec_dup = mysql_query($sql_dup, $connection);
 $row_dup = mysql_fetch_array($sqlExec_dup, MYSQL_ASSOC);
 if($row['name'] = $row_dup['name']){
 $sql_dup2 = "SELECT * FROM comment WHERE name = '{$row['name']}'";
 $sqlExec_dup2 = mysql_query($sql_dup2, $connection);
 while($row_dup2 = mysql_fetch_array($sqlExec_dup2, MYSQL_ASSOC)){
 $x += 1;
 if($x <= $row_dup['dup_count']){
 $comment[$row['name']][] = $row_dup2['comment'];
 }
 }
 }
}

If the name has a duplicate, meaning it has more than one comment, still cant get my desired results.

asked Feb 4, 2011 at 19:37

4 Answers 4

2

You would have to check whether it already exists or not, and if it does, create an array (or do that from the start)

// Create arrays with names
$comment[$row['name']][] = $row['comment'];

or

// Check if there's an array
if (isset($comment[$row['name']])) {
 if (is_array($comment[$row['name']])) {
 $comment[$row['name']][] = $row['comment'];
 } else {
 $comment[$row['name']] = array($comment[$row['name']], $row['comment']);
 }
} else {
 $comment[$row['name']] = $row['comment'];
}

I should point out that the first solution would be very much preferred because it is a lot more consequent.

answered Feb 4, 2011 at 19:40
Sign up to request clarification or add additional context in comments.

Comments

1

Yes, it is possible, but you need to do some preprocessing for that:

$comment = array;
while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
 if(!isset($comment[$row['name']])) {
 $comment[$row['name']] = array();
 }
 $comment[$row['name']][] = $row['comment'];
}
echo json_encode($comment);
answered Feb 4, 2011 at 19:41

Comments

0

Yes, it's possible. Instead of this line:

$comment[$row['name']] = $row['comment'];

Use this:

$comment[$row['name']] = array('Yo', 'Sup');

And replace the contents of the array with the greetings you want from the database.

answered Feb 4, 2011 at 19:41

Comments

0

Simple change:

$comment[$row['name']] = $row['comment'];

to

$comment[$row['name']][] = $row['comment'];

Each name element of the $comment array was being overwritten each time the same name came up.

answered Feb 4, 2011 at 19:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.