wilsoc31 Posted June 20 Share Posted June 20 for the life of me, i'm overlooking something. this line is inside of a loop but at the end, it only has the last record. why??? Thanks in advance $response = array(); while ($row = mysqli_fetch_array($get_tour,MYSQLI_ASSOC)) { $response = array(["name"=>"$tour_name","dates"=>"$print_start","datee"=>"$print_end","location"=>"$city,$state"]); } echo json_encode($response); output: (only last line) [{"name":"TURKEY RUN EVENT-OPEN-$595","dates":"11-16-2024","datee":"11-17-2024","location":"Redding,CA"}] Quote Link to comment https://forums.phpfreaks.com/topic/321872-array-not-appending-right/ Share on other sites More sharing options...
Barand Posted June 20 Share Posted June 20 That code writes a value to $response in each iteration, each value overwriting the previous one. Try $response[] = ... instead of $response = ... to append to the $response array Quote Link to comment https://forums.phpfreaks.com/topic/321872-array-not-appending-right/#findComment-1628429 Share on other sites More sharing options...
wilsoc31 Posted June 21 Author Share Posted June 21 (edited) Barand, Thank you for your reply. so i tried that. the issue is the json will not parse it out correctly i guess. here is the output from print_r . not sure why its changing the way it looks, but it wont parse out right if i cant get it to look right Array ( [0] => Array ( [name] => 2ND ANNUAL KINGS ISLAND SHOWDOWN CINCY METRO USA [dates] => 06-21-2024 [datee] => 06-23-2024 [location] => LEBANON,OH ) [1] => Array ( [name] => Midwest Championship #2 [dates] => 06-21-2024 [datee] => 06-23-2024 [location] => Rantoul,IL ) [2] => Array ( [name] => MIDWEST MELEE CHAMPIONSHIP [dates] => 06-21-2024 [datee] => 06-23-2024 [location] => Columbia City,IN ) [3] i need it to look like this: [{"name":"TURKEY RUN EVENT-OPEN-$595","dates":"11-16-2024","datee":"11-17-2024","city":"Redding","state":"CA"},{"name":"TURKEY RUN EVENT-OPEN-$595","dates":"11-16-2024","datee":"11-17-2024","city":"Redding","state":"CA"},{"name":"TURKEY RUN EVENT-OPEN-$595","dates":"11-16-2024","datee":"11-17-2024","city":"Redding","state":"CA"},{"name":"TURKEY RUN EVENT-OPEN-$595","dates":"11-16-2024","datee":"11-17-2024","city":"Redding","state":"CA"}] i even tried $array_count= count($response) -1; foreach ($response as $x => $y) { if($array_count != $x) { $output .= json_encode($response[$x]).","; } else { $output .= json_encode($response[$x]); } } echo "[".$output."]"; it looks ok, but something must be off: [{"name":"2ND ANNUAL KINGS ISLAND SHOWDOWN CINCY METRO USA","dates":"06-21-2024","datee":"06-23-2024","location":"LEBANON,OH"},{"name":"Midwest Championship #2","dates":"06-21-2024","datee":"06-23-2024","location":"Rantoul,IL"},{"name":"MIDWEST MELEE CHAMPIONSHIP","dates":"06-21-2024","datee":"06-23-2024","location":"Columbia City,IN"},{"name":"Missouri East State","dates":"06-21-2024","datee":"06-23-2024","location":"Eureka,MO"},{"name":"BUCKET BONANZA SHOWCASE","dates":"06-21-2024","datee":"06-23-2024","location":"NEWTON FALLS,OH"},{"name":"BUCKET BONANZA SHOWCASE","dates":"06-21-2024","datee":"06-23-2024","location":"NORTH RIDGEVILLE,OH"},{"name":"42ND SMOKEY BAKER ALL AMERICAN MEMORIAL NIT","dates":"06-21-2024","datee":"06-23-2024","location":"HARRISON,OH"},{"name":"26TH ANNUAL INDEPENDENCE CLASSIC","dates":"06-21-2024","datee":"06-23-2024","location":"MAUMEE OHIO,OH"},{"name":"C-State Championships (10U & 12U)","dates":"06-21-2024","datee":"06-23-2024","location":"Jerrersom City,MO"},{"name":"B-State Championships (10U & 12U)","dates":"06-21-2024","datee":"06-23-2024","location":"Jerrersom City,MO"}] Edited June 21 by wilsoc31 Quote Link to comment https://forums.phpfreaks.com/topic/321872-array-not-appending-right/#findComment-1628450 Share on other sites More sharing options...
Barand Posted June 21 Share Posted June 21 Have you tried.. $response = array(); while ($row = mysqli_fetch_array($get_tour,MYSQLI_ASSOC)) { $response[] = array(["name"=>$row["tour_name"], "dates"=>$row["print_start"], "datee"=>$row["print_end"], "location"=>$row["city"] . ", " . $row["state"] ); } echo json_encode($response); or , using PDO (where $pdo is the pdo db connection)... $get_tour = $pdo->query("SELECT tour_name, print_start, print_end, city, state) FROM wherever"); echo json_encode($get_tour->fetchAll(PDO::FETCH_ASSOC) ) Quote Link to comment https://forums.phpfreaks.com/topic/321872-array-not-appending-right/#findComment-1628454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.