Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

PHP7 while()each() becomes foreach()


douglaswalker

Recommended Posts

Hi there

I am making my Bootstrap Site (Gold) PHP7 compatible. 

I have worked my way through all the changes on GitHub and all is well. Except I have  a non standard piece of code which is a discount module.

The pieces of code below are to places where i need to change the while and each. I have tried a number of ways based on comparing other changes but I can't get it working.

Could someone please help with this last piece in the puzzle.

function total() {
      reset($this->discounts);
      $total = 0;
      while (list($type, ) = each($this->discounts)) {
       $total = $total + $this->discounts[$type]['amount'];
      }
      return $total;
    }

and the second

function get_all() {
      $discounts_array = array();
      reset($this->discounts);
      while (list($type, ) = each($this->discounts)) {
          $discounts_array[] = array('description' => $this->discounts[$type]['description'],
                                     'amount' => $this->discounts[$type]['amount']);
      }
      return $discounts_array;
    }

I thought it would be something like this

function get_all() {
      $discounts_array = array();
     
               foreach($this->discounts as $type) {
          $discounts_array[] = array('description' => $this->discounts[$type]['description'],
                                     'amount' => $this->discounts[$type]['amount']);
      }
      return $discounts_array;
    }

 

but alas it is not

All help appreciated

Doug

Link to comment
Share on other sites

function total() {
  $total = 0;
  foreach(array_keys($this->discounts) as $type) {
    $total = $total + $this->discounts[$type]['amount'];
  }
  return $total;
}

AND

function get_all() {
  $discounts_array = array();
  foreach(array_keys($this->discounts) as $type) {
    $discounts_array[] = array('description' => $this->discounts[$type]['description'], 'amount' => $this->discounts[$type]['amount']);
  }
  return $discounts_array;
}

Should Do It, as per (if I recall correctly);

  • while (list($k, ) = each($v)) {    =>   foreach(array_keys($v) as $k) {
  • while (list(, $k) = each($v)) {    =>   foreach($v as $k) {
  • while (list($k, $v) = each($x)) {    =>   foreach($x as $k => $v) {
     
  • and in all three things, remove the reset($v) line of code.
Link to comment
Share on other sites

Thanks Gary

it is a thing of beauty.

I found my way through all the changes in Github... thank-you

I have a test site running on PHP Version 7.0.30 all seems good

Will this also work on my 5.6.36 server as well  is it not backwards compatible.

 

Link to comment
Share on other sites

11 minutes ago, douglaswalker said:

Will this also work on my 5.6.36 server as well  is it not backwards compatible.

I think it should be OK on 5.6 as well.

I could go overboard on some code changes which are php7 only, but I probably won't as (a) it'll break loads of shops still on php5.x and (b) I don't have time

EG:

echo (isset($_POST['q']) ? $_POST['q'] : '')    =>    echo $_POST['q'] ?? '';

Link to comment
Share on other sites

Hi there

just wanted to report back.

I tested just specials.php  with the altered foreach change on a 5.6  server and it failed.

I just got a no products found... swapped back to the old file and the specials appeared.

So that is interesting... I don't know if that is others experience.

So I guess I will just have to wait until the server upgrades and then upload the new files... unless there is another way to do it.

Doug

 

Link to comment
Share on other sites

Is this helpful at all:

?

By the way, "Gold" hasn't been updated in quite a while, and is more or less obsolete unless you have been keeping up with the "Edge" changes, in which case it might be better to fully migrate to "Edge". That would be less work than trying to patch "Gold" to be PHP 7 compatible.

Link to comment
Share on other sites

Thank-you

I have it all finished now and not throwing any errors.

I have the fixed files as a backup so when my host upgrades I am not stuck.

I will be upgrading fully to edge when it is at final and i have some time.

Just out of interest how would you fix all the errors mentioned above?

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...