Encapsulating code, urgent help needed please

I’ve just accidentally overwritten a file that had a part of code encapsulated in a test structure but I’ve got no idea what that means.

Please can somebody help me fix this urgently. I’ve got to get it sorted before my boss notices what I’ve done.

is there a backup anywhere (or even better, versioning)? if not, you’re screwed.

Unfortunately no there’s no back up. I’ve got the code (without the encapsulation) is it possible to put new encapsulation in?

Not using git or svn or anything, no version control at all?

Not that I know of no unfortunately.

In that case is there anything I can do to stop this error:
PHP Warning: array_key_exists() expects parameter 2 to be array, null given in /www/artist.inc on line 309

This is line 309:

if( is_array( $result ) ) { foreach( $result as $key=>$value ) { if( array_key_exists( $value['id'],$album ) ) { if( array_key_exists( $value['artist_id'],$discog_single ) ) {

$album and $discog_single need to be an array.

it might actually be easier to reduce the result set in SQL.

I’m really sorry to be so dumb but how would I do that?

This is the whole code for that block:

  $query="select shop_album_sub.id,shop_album_sub.title,shop_album_sub.description,shop_album_sub.linkword from shop_album_sub,shop_album where shop_album_sub.visible=1 and collection_id = 11 and shop_album.id = 11 and sub_categories = 1 order by shop_album_sub.pos";
  $result=dbselect( $query,"dbShop" );
  if( is_array( $result ) ) { foreach( $result as $key=>$value ) {
    $discog_single[$value['id']]['title']=$value['title'];
    $discog_single[$value['id']]['linkword']=$value['linkword'];
    $discog_single[$value['id']]['description']=$value['description'];
  } }

  # get data from web
  $query="select artist_id,id from products,product_map,shop_album_entry where product_map.product_id=products.id and shop_album_entry.product_id=products.id order by artist_id,shop_album_entry.pos,product_map.pos";
  $result=dbselect( $query,"dbShop" );
  if( is_array( $result ) ) { foreach( $result as $key=>$value ) { if( array_key_exists( $value['id'],$album ) ) { if( array_key_exists( $value['artist_id'],$discog_single ) ) {
    $discog_single[$value['artist_id']]['products'][$value['id']]['name']=$album[$value['id']]['name2'];
    $discog_single[$value['artist_id']]['products'][$value['id']]['title']=$album[$value['id']]['title'];
    $discog_single[$value['artist_id']]['products'][$value['id']]['linkword']=$album[$value['id']]['linkword'];
    $discog_single[$value['artist_id']]['products'][$value['id']]['price1']=$album[$value['id']]['price1'];
  } } } }

  # strip empties
  if( is_array( $discog_single ) ) { foreach( $discog_single as $key=>$value ) {
    if( !is_array( $value['products'] ) ) { unset( $discog_single[$key] ); }
  } }

I would start by not doing a cross join.

then the question would be: what data do I want and where do I find these data in the database.

=> there is with 99+% certainty a relation between shop_album and shop_album_sub as well as between products and product_map and between products and shop_album_entry. and I wouldn’t be surprised if there were a relation between shop_album and shop_album_entry.

then comes following the relations in the SQL statement coupled with finally adding the select condition.

I know I’m being dumb but I don’t understand, I’m pretty new to this and am now really panicking that my boss will discover I’ve over written the file

If you don’t use version control and you don’t have backups… then that’s pretty much it. There’s nothing left to do except tell your boss. Hopefully he has copies on his own computer.

But even if this situation doesn’t go well, here’s what you can learn from this experience:

  1. Always use version control.
  2. Always have backups, not just of your code, but also of your data.
  3. Always develop in dev or test environments. Never develop live.

Even experienced developers will overwrite the wrong file from time to time, but these three rules help ensure that silly mistakes don’t become big problems.

2 Likes

PHPStorm has built in versioning. One of my favorite features.

When you’re not doing things right these are the consequences encountered. Good luck. Hopefully you’re not working directly on prod…

No version control… are you sure? Perhaps you best tell your boss and let him fix it if you just messed up production. If you’re new to this I’m sure he will understand and in some ways expect you to blow things up having back-ups, etc.

1 Like

That’s a good recommendation. At this point you’ve messed up badly enough to need help, and more than you’ll get with random bits of advice from us.

If your boss doesn’t have VC or backups in place, depending on the scope of your position and what you were hired as / purported to be, that may be as much their fault as yours. In any case, not sure what else you can do. We don’t know enough about your application to help you rebuild the lost bits, nor would probably anyone desire to do so.

Thanks guys for trying to help. I guess I just need to bite the bullet and admit what happened!

Wish me luck!

Okay the good news is that I’ve told them and I haven’t lost my job which is a huge relief.

The bad news is I need to fix it now and am not sure who to so would be grateful for any assistance. I was originally told that
$discog_single is not tested to see if it is an array before use which throws an error. The whole block has been encapsulated in a test structure to fix the problem.

This I don’t understand, what does encapsulated in a test structure mean? We have a global to determine if it’s a test version or not which is:

$GLOBALS['testmode']=0; // (0 is live, 1 is test mode)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.