Jump to content

Recommended Posts

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"}]

Link to comment
https://forums.phpfreaks.com/topic/321872-array-not-appending-right/
Share on other sites

  • wilsoc31 changed the title to array not appending right...
Posted (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 by wilsoc31

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) )

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.