Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:14:14+00:00 2026-05-22T22:14:14+00:00

I have a few lines of code that start something like this: $trailheads =

  • 0

I have a few lines of code that start something like this:

$trailheads = array();
// Then a db call with a query.  Then loop through the results.

// This gives a diff value every time, so here we are still ok
$trailhead->trailhead_name = $row['trailhead_name']; 

// Before the look iteration ends, I do something like this:
array_push ( $trailheads , $trailhead );
// But I could have done this with the same result:
$trailheads[] = $trailhead; 

And once I exit the loop, I do print_r and it shows that the second of the two rows returned by the query over-wrote the first.

Here is the full version of the loop:

    while($row = mysql_fetch_assoc($trailhead_result))
    {
        $trailhead->trailhead_name = $row['trailhead_name'];


        $trailhead->park_id = $row['park_id'];
        $trailhead->trailhead_id = $row['trailhead_id'];
        $trailhead->trailhead_description = $row['trailhead_description'];
        $trailhead->parking = $row['parking'];
        $trailhead->lat = $row['lat'];
        $trailhead->lng = $row['lng'];
        $trailhead->is_free = $row['is_free'];
        $trailhead->parking_spots = $row['parking_spots'];
        $trailhead->cost_details = $row['cost_details'];

        $trailheads[] = $trailhead;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:14:14+00:00Added an answer on May 22, 2026 at 10:14 pm

    If that’s your full loop, then one problem is that you’re not initializing $trailhead inside the loop. Do this:

    while($row = mysql_fetch_assoc($trailhead_result))
    {
        $trailhead = new trailhead();
        $trailhead->trailhead_name = $row['trailhead_name'];
        $trailhead->park_id = $row['park_id'];
        $trailhead->trailhead_id = $row['trailhead_id'];
        $trailhead->trailhead_description = $row['trailhead_description'];
        $trailhead->parking = $row['parking'];
        $trailhead->lat = $row['lat'];
        $trailhead->lng = $row['lng'];
        $trailhead->is_free = $row['is_free'];
        $trailhead->parking_spots = $row['parking_spots'];
        $trailhead->cost_details = $row['cost_details'];
    
        $trailheads[] = $trailhead;
    }
    

    I have to assume that the object $trailhead is a class called trailhead. If it’s not, use whatever class is correct in place of new trailhead().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this few lines of code FilterCondition CustomerID = new FilterCondition(Customer_ID,cust.Customer_ID,FilterCondition.FilterOperator.Equals); List<FilterCondition> conditions
I have this line of code: System.Drawing.Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon); A few lines later,
I have this simple code in the first few lines of my script and
I have written a few lines of code in Python to see if I
I have a sample.xhtml file which has few lines of javascript. This javascript will
I have a text file with few first few lines not required and then
I have a text like: Title A some description on a few lines, there
I'll start by saying that this isn't a problem, just curiosity. I have some
I have the first few lines of a text file fx.txt with the following
I have DIV element which only has a few lines in and a background

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.