1

Good day fellas!

I have this block of code where i create a JSON string and i have a dynamic number of data so what i need is also a dynamic name for a key..

if (mysql_num_rows($result) > 0) {
    $response["members"] = array();
    $x = 0;
    $members = array();
    while ($row = mysql_fetch_array($result)) {
        $members = array();
        $members["member" + (string)$x] = array();
        $member["member_id"] = $row["member_id"];
        $member["firstname"] = $row["firstname"];
        $member["mi"] = $row["mi"];
        $member["lastname"] = $row["lastname"];
        $member["email"] = $row["email"];
        $member["username"] = $row["username"];
        $member["password"] = $row["password"];
        $member["guild_id"] = $row["guild_id"];
        $member["guild_name"] = $row["guild_name"];
        $member["guild_code"] = $row["guild_code"];
        array_push($members["member" + (string)$x], $member);
        $x++;
    }
    array_push($response["members"], $members);

    echo json_encode($response);;
}

In JQuery this type of method works.. my question is, is there any way i can achieve it using php? If you know i will be very happy to know...

5
  • what you need is putting a member on a unique index. Commented Apr 7, 2017 at 7:44
  • yep and i am trying to use a string type of index.. Commented Apr 7, 2017 at 7:45
  • @Siege21x a much better solution given by me. check and try once Commented Apr 7, 2017 at 8:12
  • @AlivetoDie sure man. I'll make sure to try that out! Thanks alot! Commented Apr 7, 2017 at 9:01
  • 1
    Was able to have it man. I voted for your answer. Thanks a lot! Commented Apr 10, 2017 at 2:41

2 Answers 2

1

1.Suggestion:-stop using deprecated+removed(php5 +php7) version of mysql_*.Move towards mysqli_* OR PDO along with prepared statements(Prevent Sql Injection)

2.Why creating unnecessary array and multiple push.No need to do that.Do like below:-

$response["members"] = array();//put outside
if (mysql_num_rows($result) > 0) {
    $x = 0;
    while ($row = mysql_fetch_array($result)) {
        $response["members"][$x]["member_id"] = $row["member_id"]; //assign value directly to the resultant array
        $response["members"][$x]["firstname"] = $row["firstname"];
        $response["members"][$x]["mi"] = $row["mi"];
        $response["members"][$x]["lastname"] = $row["lastname"];
        $response["members"][$x]["email"] = $row["email"];
        $response["members"][$x]["username"] = $row["username"];
        $response["members"][$x]["password"] = $row["password"];
        $response["members"][$x]["guild_id"] = $row["guild_id"];
        $response["members"][$x]["guild_name"] = $row["guild_name"];
        $response["members"][$x]["guild_code"] = $row["guild_code"];
        $x++;
    }
}

if(count($response["members"])>0){ //check finally that array is not empty
 echo json_encode($response); //echo json encoded array data
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is great! Thanks for the idea in this kind of saving the data within arrays. I'll read the difference between mysql and mysqli for me to understand what you are talking about :D Thanks a lot!
1

if you want to achieve that the members array contains individual records with keys as member0, member1, etc., then you could proceed as:

if (mysql_num_rows($result) > 0) {
    $x = 0;
    $members = array();
    while ($row = mysql_fetch_array($result)) {
        $member = array();
        $member["member_id"] = $row["member_id"];
        $member["firstname"] = $row["firstname"];
        $member["mi"] = $row["mi"];
        $member["lastname"] = $row["lastname"];
        $member["email"] = $row["email"];
        $member["username"] = $row["username"];
        $member["password"] = $row["password"];
        $member["guild_id"] = $row["guild_id"];
        $member["guild_name"] = $row["guild_name"];
        $member["guild_code"] = $row["guild_code"];
        $members["member$x"] = $member;
        $x++;
    }
    $response["members"] = $members;

    echo json_encode($response);
}

1 Comment

Thanks for the answer! I'll make sure to try this out and declare this the answer if it will work! Thanks! :D

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.