Jump to content

[SOLVED] Php5 array not working, worked in php4


noexcal

Recommended Posts

Alright, I have to use php5 because of new functions.

So far I have had no problems, but came across one.

 

In php4 this worked fine and returned the correct results, but in php5 it knows how many items are in the array but shows the same item for each represented item.

 

(Example)

 

in php4 I would pull "Item1" and "Item2" and then pull those items and on the page I would get "Item1" and "Item2"

 

in php5 I pull "Item1" and "Item2" but it shows on the page "Item1" and "Item1" it does not show "Item2"

 

here is the php code I am using.

 

$some_query = $database->database_query("SELECT * FROM table  WHERE id=3 ORDER BY order_id ASC LIMIT 30");

$some_array = Array();

while($some = $database->database_fetch_assoc($some_query)) {

 

  $some_stuff->id[id] = $some[user_id];

 

  $some_array[] = $some_user;

}

 

ignore any obvious weird errors, as I changed all the info.

but if you see any errors in the main code please tell me what has changed in php5?

Link to comment
Share on other sites

function user_app_list() {

  global $database, $setting;

 

    // SET VARIABLE

  $app_array = Array();

 

 

    // BEGIN FRIEND QUERY

    $app_query = "SELECT * FROM se_apps  WHERE user_id='".$this->user_info[user_id]."' AND app_info2=1 AND profile=1 ORDER BY order_id ASC LIMIT 0,30";  

 

    // LOOP OVER FRIENDS

    $app = $database->database_query($app_query);

    while($app_info = $database->database_fetch_assoc($app)) {

 

      // CREATE AN OBJECT FOR FRIEND

      $app_user->user_info[user_id] = $app_info[user_id];

  $app_user->user_info[app_info1] = $app_info[app_info1];

  //$app_user->user_info[app_info3] = $app_info[app_info3];

  $app_user->user_info[item_id] = $app_info[item_id];

  $app_user->user_info[profile] = $app_info[profile];

  $app_user->user_info[name] = $app_info[name];

  $app_user->user_info[height] = $app_info[height];

  $app_user->user_info[width] = $app_info[width]+10;

 

      // SET FRIEND ARRAY

      $app_array[] = $app_user;

    }

 

 

  // RETURN FRIEND ARRAY

  return $app_array;

 

} // END user_applist() METHOD

 

 

this returns how many ever items are in the array (wether it be 1 or 2) it knows how many are there, but it shows the same item for all items.

Link to comment
Share on other sites

I don't see anything immediately wrong with that, except for maybe you meant to do $this->user_info[..] = $app_info[..]; on each of those lines.  But for the way you have it setup, it should return what you want anyways, so I don't think that's what's causing the error. 

 

So...I would fastfoward to whatever is calling that method and look at what's responsible for looping and displaying the $app_array that's returned from this method.

 

If that looks good, I would look in the database_fetch_assoc method. 

Link to comment
Share on other sites

I take that back, enabling that just screwed up a bunch of stuff that requires php5..

;[

 

And I did look into what was looping it.

I decided just to print_r it to check to see if it was coming from that end or not.

and print_r returns this.

 

Array ( [0] => stdClass Object ( [user_info] => Array ( [user_id] => 1 [app_info1] => 4ad12e98-b9e8-41de-a56c-506db58fc680 [item_id] => 2 [profile] => 1 [name] => Facedouble Widgetbox [height] => 300 [width] => 410 ) ) [1] => stdClass Object ( [user_info] => Array ( [user_id] => 1 [app_info1] => 4ad12e98-b9e8-41de-a56c-506db58fc680 [item_id] => 2 [profile] => 1 [name] => Facedouble Widgetbox [height] => 300 [width] => 410 ) ) )

 

as you can see both arrays contain the same data.. when they should not.

in php4 it returns this

 

Array ( [0] => stdClass Object ( [user_info] => Array ( [user_id] => 1 [app_info1] => 9d5dcc74-02bd-4b43-a32f-89dd7b2ca395 [item_id] => 3 [profile] => 1 [name] => The Simpsomaker 3 [height] => 313 [width] => 410 ) ) [1] => stdClass Object ( [user_info] => Array ( [user_id] => 1 [app_info1] => 4ad12e98-b9e8-41de-a56c-506db58fc680 [item_id] => 2 [profile] => 1 [name] => Facedouble Widgetbox [height] => 300 [width] => 410 ) ) )

 

 

 

Link to comment
Share on other sites

Array ( [item_id] => 3 [user_id] => 1 [app_info1] => 9d5dcc74-02bd-4b43-a32f-89dd7b2ca395 [app_info2] => 1 [app_info3] => 7bf5a704-a7f6-4891-95a4-1ee8059b9b87 [name] => The Simpsomaker 3 [width] => 400 [height] => 313 [profile] => 1 [order_id] => 3 ) Array ( [item_id] => 2 [user_id] => 1 [app_info1] => 4ad12e98-b9e8-41de-a56c-506db58fc680 [app_info2] => 1 [app_info3] => ba85f3b0-99d8-4e18-9a54-135521dbcaa1 [name] => Facedouble Widgetbox [width] => 400 [height] => 300 [profile] => 1 [order_id] => 2 )

 

Yep, that returned the correct results. ;o

Still lost tho.. lol, I know now that its happening somewhere between there and when it returns the final array.

but what has changed between php4 and 5 that would cause this.

Link to comment
Share on other sites

For some reason your php5 doesn't like objects being made on the fly like that.  Dunno why though.  How about just cutting out the middle man though, and just doing this in your while loop intead:

 

while($app_info = $database->database_fetch_assoc($app)) {

  // CREATE AN OBJECT FOR FRIEND
  $app_array[] = $app_info[user_id];
  $app_array[] = $app_info[app_info1];
  //$app_array[] = $app_info[app_info3];
  $app_array[] = $app_info[item_id];
  $app_array[] = $app_info[profile];
  $app_array[] = $app_info[name];
  $app_array[] = $app_info[height];
  $app_array[] = $app_info[width]+10;
}

Link to comment
Share on other sites

As CV suggested - just use an array and cut out the middleman

 

while($app_info = $database->database_fetch_assoc($app)) {
  $app_array[] = $app_info;
}

 

You'd have to change your query a little bit (select exactly what you want)

$app_query = "SELECT `user_id`,`app_info1`,`item_id`,`profile`,`name`,height`,`width`+10 as width FROM se_apps  WHERE user_id='".$this->user_info[user_id]."' AND app_info2=1 AND profile=1 ORDER BY order_id ASC LIMIT 0,30";

 

Link to comment
Share on other sites

or just:

 

      while($app_info = $database->database_fetch_assoc($app)) {

         // CREATE AN OBJECT FOR FRIEND
         $user_info[user_id] = $app_info[user_id];
  $user_info[app_info1] = $app_info[app_info1];
  //$user_info[app_info3] = $app_info[app_info3];
  $user_info[item_id] = $app_info[item_id];
  $user_info[profile] = $app_info[profile];
  $user_info[name] = $app_info[name];
  $user_info[height] = $app_info[height];
  $user_info[width] = $app_info[width]+10;

         // SET FRIEND ARRAY
         $app_array[] = $user_info;
       }

Link to comment
Share on other sites

While this does not directly help with a solution, the difference is php4 assigns a copy of the object to the array, while php5 assigns a reference to the object to the array and you are getting multiple references to the (1) object and you are receiving the last value in it. And in fact here is something from the manual -

When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

 

One or the other of these might work -

 

        $app_array[] = (object)$app_user;

 

        $app_array[] = &$app_user;

 

 

Link to comment
Share on other sites

t

For some reason your php5 doesn't like objects being made on the fly like that.  Dunno why though.  How about just cutting out the middle man though, and just doing this in your while loop intead:

 

while($app_info = $database->database_fetch_assoc($app)) {

  // CREATE AN OBJECT FOR FRIEND
  $app_array[] = $app_info[user_id];
  $app_array[] = $app_info[app_info1];
  //$app_array[] = $app_info[app_info3];
  $app_array[] = $app_info[item_id];
  $app_array[] = $app_info[profile];
  $app_array[] = $app_info[name];
  $app_array[] = $app_info[height];
  $app_array[] = $app_info[width]+10;
}

 

This might actually work if I could figure out how to pass it to the template files (I use smarty) and the class wont pass right, I will play around more with this same with the any of the other ones that change the " $app_user->user_info" part.

 

While this does not directly help with a solution, the difference is php4 assigns a copy of the object to the array, while php5 assigns a reference to the object to the array and you are getting multiple references to the (1) object and you are receiving the last value in it. And in fact here is something from the manual -

When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.

 

One or the other of these might work -

 

         $app_array[] = (object)$app_user;

 

         $app_array[] = &$app_user;

 

 

 

Neither one of these worked, but thank you for telling me the difference.

 

I will continue to work with this, and if I do solve it, I will post that I did.

Link to comment
Share on other sites

Alright, idk what good this will do but here is one that does work.

it is the template I used for making this one.. and it works fine.

 

// SET VARIABLE
  $friend_array = Array();

  // MAKE SURE CONNECTIONS ARE ALLOWED
  if($setting[setting_connection_allow] != 0) {

    // BEGIN FRIEND QUERY
    $friend_query = "SELECT se_friends.friend_id, se_users.user_id, se_users.user_username, se_users.user_photo, se_users.user_lastlogindate, se_users.user_dateupdated";

    // GET FRIEND EXPLAIN, IF NECESSARY
    if($friend_details == 1) { $friend_query .= ", se_friends.friend_type, se_friendexplains.friendexplain_body"; }

    // CONTINUE QUERY
    $friend_query .= " FROM se_friends LEFT JOIN se_users ON";

    // MAKE SURE TO JOIN ON THE CORRECT FIELD (DEPENDENT ON DIRECTION)
    if($direction == 1) { $friend_query .= " se_friends.friend_user_id1=se_users.user_id"; } else { $friend_query .= " se_friends.friend_user_id2=se_users.user_id"; }

    // JOIN ON FRIEND EXPLAIN TABLE, IF NECESSARY
    if($friend_details == 1) { $friend_query .= " LEFT JOIN se_friendexplains ON se_friends.friend_id=se_friendexplains.friendexplain_friend_id"; }

    // CONTINUE QUERY
    $friend_query .= " WHERE friend_status='$friend_status'";

    // EITHER "LIST OF WHO USER IS A FRIEND OF" OR "LIST OF USER'S FRIENDS"
    if($direction == 1) { $friend_query .= " AND friend_user_id2='".$this->user_info[user_id]."'"; } else { $friend_query .= " AND friend_user_id1='".$this->user_info[user_id]."'"; }

    // ADD ADDITIONAL WHERE CLAUSE IF EXISTS
    if($where != "") { $friend_query .= " AND $where"; }

    // SET SORT	AND LIMIT
    $friend_query .= " ORDER BY $sort_by LIMIT $start, $limit";

    // LOOP OVER FRIENDS
    $friends = $database->database_query($friend_query);
    while($friend_info = $database->database_fetch_assoc($friends)) {

      // CREATE AN OBJECT FOR FRIEND
      $friend = new se_user();
      $friend->user_info[user_id] = $friend_info[user_id];
      $friend->user_info[user_username] = $friend_info[user_username];
      $friend->user_info[user_photo] = $friend_info[user_photo];
      $friend->user_info[user_lastlogindate] = $friend_info[user_lastlogindate];
      $friend->user_info[user_dateupdated] = $friend_info[user_dateupdated];

      // SET FRIEND TYPE/EXPLANATION VARS
      if($friend_details == 1) {
        $friend->friend_type = $friend_info[friend_type];
        $friend->friend_explain = $friend_info[friendexplain_body];
      }

      // SET FRIEND ARRAY
      $friend_array[] = $friend;
    }
  }

  // RETURN FRIEND ARRAY
  return $friend_array;

} // END user_friend_list() METHOD

 

Now that one does alot of different stuff in the database, and checks different things.

but for the main part it pulls the array the same way.

Link to comment
Share on other sites

I notice in that one you have $friend = new se_user(); in your loop

 

All though this was my fault, and stupidity for forgetting to change the start of that line.

I tested this first, before everything and it through errors because of the $friend part.

It is really late, and I am tired.. and sometimes taking a second can fix a lot of issues ;[

 

adding

 

$app_user = new se_user();

$app_user->user_info[user_id] = $app_info[user_id];

  $app_user->user_info[app_info1] = $app_info[app_info1];

  //$app_user->user_info[app_info3] = $app_info[app_info3];

  $app_user->user_info[item_id] = $app_info[item_id];

  $app_user->user_info[profile] = $app_info[profile];

  $app_user->user_info[name] = $app_info[name];

  $app_user->user_info[height] = $app_info[height];

  $app_user->user_info[width] = $app_info[width]+10;

 

worked.

but also I did have to add the & in front of the $app_array[] = &$app_user; or it would only show 1 result.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.