473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Looping through PHP arrays

Hi Guys,

I'm from a VB; Access; SQL Server background recently introduced to WAMP.

I have two arrays filled from MySQL recordsets, one for the site Cart and the other for one of the site departments (catalogs) see a manual interpretation below.
Expand|Select|Wrap|Line Numbers
  1. $catalog[] = array( 
  2. array('ProdID'=>2,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  3. array('ProdID'=>6,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  4. array('ProdID'=>7,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  5. array('ProdID'=>8,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  6. array('ProdID'=>9,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  7. );
  8.  
  9.  
  10. $cart[] = array(
  11. array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81','ProdID'=>'2','Quant'=>'1'),
  12. array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81','ProdID'=>'4','Quant'=>'1'),
  13. array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81','ProdID'=>'8','Quant'=>'1'),
  14. array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProdID'=>'21','Quant'=>'1'),
  15. );
  16.  
What I need to do is loop through the catalog array each time a new catalog is chosen, comparing the ProdID of the cart against the catalog, when a match is found I would like to update the 'InBasket' value to '1' this will indicate the item is already in the customers cart which I can then show on screen (The customer is only allowed to purchase one of each item).

I have tried some loops but they don't seem to work.
Expand|Select|Wrap|Line Numbers
  1. for ($x = 0; $x < count($catalog); $x++)
  2.     {
  3.     for ($y = 0; $y < count($cart); $y++)
  4.         {
  5.             If ($catalog[$x]['ProductID'] == $cart[$y]['ProductID'] )
  6.                 {
  7.                   $cart[$x]['InBasket'] = 1;
  8.             }
  9.         }
  10.     }
  11.  
Any ideas would be appreciated.
Jul 23 '12 #1
21 3243
Claus Mygind
571 512MB
Three quick comments about your looping code

1. check the php manual, you will see that the structure you created is not the most efficient. A better option is to calculate the size of the array only once and test against the size variable like this:

for($i = 0, $size = count($catalog); $i < $size; ++$i)

2. In your code no matter how many products are found the answer will always be 1, because you are just setting the value of inBasket to 1

$cart[$x]['InBasket'] = 1;

what I suspect you want to do is increment the value by one.

$cart[$x]['InBasket'] += 1; or $cart[$x]['InBasket'] ++;

3. You may want to use an associative array instead of a numerical array for quicker look-up. That way you do not have to loop through the array.

Look at example #7 in the online php manual
Jul 25 '12 #2
Hi Claus

Thanks for your reply. We both may be a little confused here, my idea (as in VB6) was to loop through each catalog row (record) comparing it with each row in the Cart array, if the 'fields' match on the ProductID then I can update the catalog 'field' InBasket for that row. I only ever want InBasket to be 0 or 1 (true/ false) so that when the web page is constructed I can inform the user about these products to stop them selecting a duplicate product, this action will happen every time a new catalog is chosen.

The probability is there will never be more than 10 items in a catalog and 3 - 5 in the cart.

I could not see how example #7 would update a 'field' perhaps you could explain ?

I thought I already had an associative array, is there a different looping method ?

Kind regards
Jul 25 '12 #3
lyodmichael
75 64KB
hmm, you want to minus the quantity of the item when it is in the cart of other customer am i correct? if that so, you need to have the code in the on load and offload. but i think it's difficult,
Jul 25 '12 #4
Claus Mygind
571 512MB
Not sure where you are keeping track of selected items. On the user's browser? In a session on the web server? Are you doing ajax lookup calls each time the user selects an item to update the cart? or is the user submitting a form for each item?

My thought was to restructure the catalog array. The way you wrote the arrays, both the catalog and cart arrays are numerical arrays. The sub-arrays within each cell is an associative array.

As your arrays sit the way you wrote them, you access each cell with $catalog[1] etc. Check out the revised code below. And in thinking about it you really could also eliminate that other loop. Just pass the product ids selected and stream out the results.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //What if you use this part of example 7
  3.     $array = array(
  4.         "multi" => array(
  5.              "dimensional" => array(
  6.                  "array" => "foo"
  7.              )
  8.         )
  9.     );
  10.  
  11. //and wrote the  catalog array like this
  12.     $catalog = array( 
  13.         'PID2' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  14.         'PID6' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  15.         'PID7' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  16.         'PID8' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  17.         'PID9' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0')
  18.     );
  19.  
  20. //modify the cart array like this
  21.     $cart[] = array(
  22.         array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81' ,'ProdID'=>'PID2','Quant'=>'1'),
  23.         array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81'  ,'ProdID'=>'PID4','Quant'=>'1'),
  24.         array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81' ,'ProdID'=>'PID8','Quant'=>'1'),
  25.         array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProdID'=>'PID21','Quant'=>'1'),
  26.     );                                                                    
  27.  
  28. //update the catalog array like this as shown in example 7 "var_dump($array["multi"]["dimensional"]["array"]);"
  29.         for ($i = 0, $size = count($cart); $i < $size; ++$i)
  30.             {
  31.                 $thisProduct = $cart[$i]['ProductID'];
  32.                 $catalog[$thisProduct]['InBasket'] = 1;
  33.             }
  34.         }
  35. ?>
Jul 25 '12 #5
@lyodmichael
Hi lyodmichael

Not sure you are answering the same question I submitted.

BY THIS
The probability is there will never be more than 10 items in a catalog and 3 - 5 in the cart

I MEAN THIS
The probability is there will never be more than 10 items in a catalog and BETWEEN THREE AND FIVE in the cart

Kind Regards
Jul 26 '12 #6
Hi Claus

This is the code I use to extract data from MySQL via stored Procs. The web site data is either kept in SESSION variables but always in the database and retrieved when required.

I was under the impression I already had associative arrays (correct me if I'm wrong) maybe its just the way I'm accessing the data. I do not wish to rewrite bundles of code, when what I have already my well be good enough. I have the complete web site written in ASP/VBscript//VB6 activex DLL/MS SQLserver, i'ts just that PHP/MySQL does seem to be overly complicated.

So here.............
Expand|Select|Wrap|Line Numbers
  1. //Connect and get data from MySQL......
  2.  
  3. if (mysqli_num_rows($result) > 0)
  4. {
  5. while ($row  =  mysqli_fetch_array($result)) 
  6. $cItems[]      = array(
  7. 'Name'       => $row['Name'], 
  8. 'ProductID'  => $row['ProductID'], 
  9. 'Quantity'   => $row['Quantity'],
  10. 'Price'      => $row['Price'], 
  11. 'LinePrice'  => $row['LinePrice'],
  12. 'Location'   => $row['Location'],
  13. 'Discount'   => $row['Discount'],
  14. 'BasketID'   => $row['BasketID'],
  15. 'countrytax' => $row['countrytax'],
  16. 'LineTax'    => $row['LineTax']);
  17.  
  18. $_SESSION['CartItems'] = $cItems;
  19.  
  20. return true;
  21. }
  22. Else{
  23. return false;
  24. }
  25.  
  26.  
  27. //Connect and get data from MySQL......
  28.  
  29.  
  30. if (mysqli_num_rows($result) > 0)
  31. {
  32. while ($row      =  mysqli_fetch_array($result))
  33. $tempcat[]       = array(
  34. 'ProductID'      => $row['ProductID'], 
  35. 'Name'           => $row['Name'],
  36. 'DepartmentID'   => $row['DepartmentID'], 
  37. 'Price'          => $row['Price'],
  38. 'Location'       => $row['Location'], 
  39. 'DepartmentName' => $row['DepartmentName'], 
  40. 'Discount'       => $row['Discount'],
  41. 'InBasket'       => $row['InBasket']);
  42. Return $tempcat;
  43. }
  44.  
If I can't run a loop within a loop to update a field then I will look at another method, perhaps cursors on MySQL, but, I shall stay with this method for the time being.

Kind regards
Jul 26 '12 #7
Claus Mygind
571 512MB
Ok then here are two approaches (yours and mine)

1) Your original thought of the double loop should work, but in your example you referenced the wrong array. You said:
"What I need to do is loop through the catalog array each time a new catalog is chosen, comparing the ProdID of the cart against the catalog, when a match is found I would like to update the 'InBasket' value to '1' this will indicate the item is already in the customers cart which I can then show on screen (The customer is only allowed to purchase one of each item).
". Which I see was found in your $catalog array. But in your sample code, you are trying to update 'InBasket' in the $cart array, and that does not seem to exist. So if you fix just that one part of the code it should work.
Expand|Select|Wrap|Line Numbers
  1. for ($x = 0, $size = count($catalog); $x < $size; $x++)
  2. {
  3.     for ($y = 0, $size = count($cart); $y < $size; $y++)
  4.     {
  5.         if ($catalog[$x]['ProductID'] == $cart[$y]['ProductID'] )
  6.         {
  7.             //$cart[$x]['InBasket'] = 1;
  8.             $catalog[$x]['InBasket'] = 1;
  9.         }
  10.     }
  11. }
  12.  
2) The other option, which you don't want to use; would be to re-write that small piece of code to make the catalog an associative array. You said " was under the impression I already had associative arrays (correct me if I'm wrong) maybe its just the way I'm accessing the data. ". In your "while" loop your using the "push" method to create the arrays "$cItems[ ] = array(...", the use of the square bracket makes it a numeric/indexed array and not an associative array. But in fact you have a multi-dimensional array. Your first dimension is indexed and your 2nd dimension is associative. Making both dimensions associative would eliminate one loop. I understand your are hesitant to change this part of your code. But this is how I would write it:
Expand|Select|Wrap|Line Numbers
  1. //Connect and get data from MySQL......
  2.  
  3. if (mysqli_num_rows($result) > 0)
  4. {
  5.         // create an empty array
  6.         $cItems = array();
  7.  
  8.     while ( $row  =  mysqli_fetch_array($result) )
  9.     { 
  10. //to make both dimensions of array assoc. I add "PI" to id because I assume product id is a number.
  11.         $cItems[ 'PI'.$row['ProductID'] ]  = array(
  12.                     'Name'       => $row['Name'], 
  13.                     'Quantity'   => $row['Quantity'],
  14.                     'Price'      => $row['Price'], 
  15.                     'LinePrice'  => $row['LinePrice'],
  16.                     'Location'   => $row['Location'],
  17.                     'Discount'   => $row['Discount'],
  18.                     'BasketID'   => $row['BasketID'],
  19.                     'countrytax' => $row['countrytax'],
  20.                     'LineTax'    => $row['LineTax']
  21.         )
  22.     }
  23.  
  24.     $_SESSION['CartItems'] = $cItems;
  25.  
  26.     return true;
  27. }else{
  28.     return false;
  29. }
  30.  
  31.  
  32. //Connect and get data from MySQL......
  33.  
  34.  
  35. if (mysqli_num_rows($result) > 0)
  36. {
  37.     while ($row      =  mysqli_fetch_array($result))
  38.     {
  39. //in creating the tempcat I just add the "PI" to the product id.
  40.         $tempcat[]       = array(
  41.         'ProductID'      => 'PI'.$row['ProductID'], 
  42.         'Name'           => $row['Name'],
  43.         'DepartmentID'   => $row['DepartmentID'], 
  44.         'Price'          => $row['Price'],
  45.         'Location'       => $row['Location'], 
  46.         'DepartmentName' => $row['DepartmentName'], 
  47.         'Discount'       => $row['Discount'],
  48.         'InBasket'       => $row['InBasket'])
  49.     }
  50.     Return $tempcat;
  51. }
  52.  
  53. /*
  54. Now you can use the more efficient method of updating your catalog
  55. */
  56. for ($i = 0, $size = count($tempcat); $i < $size; ++$i)
  57. {
  58.     $thisProduct = $tempcat[$i]['ProductID'];
  59.     $_SESSION['CartItems'][$thisProduct]['InBasket'] = 1;
  60. }
  61.  
Take it for what it is worth. There should be no issue doing the way you originally had it laid out with the double loop.

Post and update on your success or failure.
Jul 26 '12 #8
Claus Mygind
571 512MB
Not sure you are answering the same question I submitted.

You are right I did throw in an additional question. I have not written a shopping cart program. It was just that I had additional ideas on how to update your session cart items. An alternate method would be to do an ajax/JSON call each time the user selected an item, in which case no loop would be required at all. And it would make your site more secure against hacking. But as you said you already have the site working in other languages. So I take that to mean that your are just trying to do a literal conversion and that is fine also. If the code works it works and that is all there is to it.

Sorry I thought you were responding to my answer. In re-reading I see you are not.
Jul 26 '12 #9
Hi Claus

I've been kept busy for the last couple of days, however, I shall try all of this over the weekend and report back.

What does the 'PI'. do?
Can I still loop through each record ?

Kind regards
Jul 27 '12 #10
Claus Mygind
571 512MB
I was not really sure if the PI was needed. I made the assumption that your product id was numeric, if so making the array in javaScript on the browser would be a problem (I don't think it would be a problem in php). Therefore by concatenating the "PI" with the number 9999, I could be sure it would be a string. You could try it either way.

Creating a true Assoc. Array you have to use a different for/next loop, called foreach. Instead of using 1,2,3... as array cell reference you must retrieve each key "pi1", "pi2", "pi3"... I don't use Visual Basic 6, but MS calls associative arrays Dictionaries. If you have used dictionaries in VB6 as shown here, then you should be familiar with how to loop through an associative array in php.

In php you have two options with the foreach loop.

1) you can loop through the array and get the value of each item with
foreach (array_expression as $value)


or

2) you can loop through the array and get the key ie "PI2"
foreach (array_expression as $key => $value). In this version the array key is assigned to $key as in this example

Expand|Select|Wrap|Line Numbers
  1. foreach ($fieldList as $key => $colName ) 
  2. {
  3.     $val = $tData[$key];
  4. }  
  5.  
Here is how I use that to append or update any table by reading the table schema and matching it with the incoming data items stored in an assoc array.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     /*
  3.     ----------------------------------------------------------------------------------------
  4.     uploadData adds/inserts or edits/updates data from the submitted from html page or ajax
  5.     request.
  6.  
  7.     5 parameters are submitted:
  8.      1) table to be updated                = $tTable
  9.      2) the table key/column            = $tKey
  10.      3) the record key                    = $tKeyVal
  11.      4) data set (an assoc array)          = $tData
  12.      5) insert (true) or update (false) = $append 
  13.  
  14.     usage:
  15.      $db->uploadData( "<table>", "<table key>", <key value>, <sanitized data>, true/false );
  16.  
  17.      data is submitted in an assoc array. Each array element must have the same name as the
  18.      field/column name and is case sensitive ie: id and id  or ID and ID, but not id and ID
  19.  
  20.      data should be formatted and sanitized before calling this method to prevent sql injection
  21.     ----------------------------------------------------------------------------------------
  22.     */
  23.     function uploadData( $tTable, $tKey, $tKeyVal, $tData , $append )
  24.     {
  25.         /* 
  26.         ----------------------------------------
  27.         identify the list of fields/columns 
  28.         in the table by calling getFieldList()
  29.         method which is part of this class dbcnx
  30.         ----------------------------------------
  31.         */
  32.         $fieldList = self::getFieldList($tTable);
  33.  
  34.         /* 
  35.         ---------------------------------------
  36.         assemble update/insert string  
  37.         ---------------------------------------
  38.         */
  39.         $setString = "";
  40.  
  41.         /*
  42.         ------------------------------
  43.         loop through table fields and 
  44.         match with submitted data
  45.         ------------------------------
  46.         */
  47.         foreach ($fieldList as $key => $colName ) 
  48.         {
  49.             /*
  50.             ---------------------------------------
  51.             if table field/column name found in 
  52.             data get results and add to $setString
  53.             ---------------------------------------
  54.             */
  55.             if ( array_key_exists ( $key, $tData ) )
  56.             {
  57.                 /*
  58.                 ---------------------------------------
  59.                 get current col name
  60.                 ---------------------------------------
  61.                 */
  62.                 $col = $fieldList[$key];
  63.  
  64.                 /*
  65.                 ---------------------------------------
  66.                 get corrosponding data value, if any
  67.                 ---------------------------------------
  68.                 */
  69.                 $val = $tData[$key];
  70.  
  71.                 if ($col['isAutoInc'] == "auto_increment" or
  72.                     $col['type'] == 'binary' or
  73.                     $col['type'] == "timestamp")
  74.                 {
  75.                     /*
  76.                     ---------------------------------------
  77.                     do nothing!! Autoincrement is automatic
  78.                     and the others not supported in HTML
  79.                     ---------------------------------------
  80.                     */
  81.                 }elseif($col['type'] == "numeric" or 
  82.                         $col['type'] == "decimal" or 
  83.                         $col['type'] == "tinyint" or 
  84.                         $col['type'] == "int" or 
  85.                         $col['type'] == "mediumint" or 
  86.                         $col['type'] == "smallint" or 
  87.                         $col['type'] == "double" or 
  88.                         $col['type'] == "float")
  89.                 {
  90.                     if ( empty($val) )
  91.                     {
  92.                         $val = $col['default'];
  93.                     }elseif ( is_numeric($val) ) 
  94.                     {
  95.                         $val = $val;
  96.                     }else{
  97.                         $val = number_format($val, $col['decimal'], '.', '');
  98.                     }
  99.                     $setString .= ( ( !empty($setString) ) ? ", " : "" ).                        $col['name']."=". $val;
  100.                 }elseif ( $col['type'] == "date" )    
  101.                 { 
  102.                         $setString .= ( ( !empty( $setString ) ) ? ", ": "" ).
  103.                             $col['name']."='".(( empty($val) ) ? $col['default'] : $val)."'";
  104.  
  105.                 }else
  106.                 { 
  107.                     /*
  108.                     ---------------------------------------
  109.                     text/char column
  110.                     ---------------------------------------
  111.                     */
  112.                     $setString .= ( ( !empty($setString) ) ? ", " : "" ).
  113.                             $col['name']."='".(( empty($val) ) ? $col['default'] : str_replace("'", "\'", $val))."'";
  114.  
  115.                 }// end defining column type
  116.             } //end if column found
  117.         } // end looping through table columns  
  118.  
  119.         /*create sql*/
  120.         if ( $append )
  121.         {
  122.             $sql = 'insert into '.$tTable.' set '.$setString;
  123.         }else{
  124.             $sql = 'update '.$tTable.' set '.$setString.' where '.$tKey.' = "'.$tKeyVal.'"';
  125.         }
  126.  
  127.         /* run query*/
  128.         if ( $this->query($sql) )
  129.         {
  130.             $result = $this->affected_rows." record".( ( $append ) ? " was added!": "s were updated!" );
  131.         }else{
  132.             $result = "No data was ".( ( $append) ? "added!": "updated!");
  133.         }
  134.  
  135.         /*
  136.         ---------------------------------------
  137.         return the number of records affected.
  138.         ---------------------------------------
  139.         */
  140.         return $result;
  141.  
  142.     } //end uploadData function
  143.  
  144. ?>
  145.  
Here is another example where I use both the key and the value, to load an array for back filling an HTML page.

Expand|Select|Wrap|Line Numbers
  1.     function loadFormWithData($dataModule, $thisTable, $key, $keyVal){
  2.         /*add my utility data conversion program*/
  3.         include("formatData.php");
  4.  
  5.         /*load table specific dataModule*/
  6.         include($dataModule.".php");
  7.  
  8.         $q = new qSetup();
  9.         $qSelect = $q->getSelect();
  10.         $qFrom = $q->getFrom();
  11.         $sql = "select ".$qSelect." from ".$qFrom ." where ".$key." = '".$keyVal."'";
  12.  
  13.         if ( $result = $this->query($sql) ){
  14.             if ($result->num_rows > 0){
  15.                 while ($row = $result->fetch_assoc()) {
  16.                     foreach ( $row as $key => $value )
  17.                     {
  18.                         $value = (empty($value)) ? " ":$value ;
  19.                         /*
  20.                         --------------------------------------------------------
  21.                         if reference found in $fArray (formatData.php), 
  22.                         then call associated function. pass 2 for display format
  23.                         --------------------------------------------------------
  24.                         */
  25.                         if (array_key_exists($key,$fArray))
  26.                         {
  27.                             $row[$key] = call_user_func($fArray[$key], $value, 2);
  28.                         }
  29.                     }
  30.                     $row = $q->addCalcFields($row);
  31.                     $aResults[]  = $row;    
  32.                 }
  33.             }else{
  34.                 $aResults["noRec"] = true;
  35.             }
  36.         }
  37.         $result->free();
  38.         return $aResults;
  39.     }//end download data to form
  40.  
  41.  
Jul 27 '12 #11
Hi Claus

I have found a few mistakes in my code down to human error, however, all is now well and I now give a full report below.

Expand|Select|Wrap|Line Numbers
  1. $catalog = array( 
  2. array('ProdID'=>2,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  3. array('ProdID'=>6,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  4. array('ProdID'=>7,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  5. array('ProdID'=>8,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  6. array('ProdID'=>9,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  7. );
  8.  
  9. $cart = array(
  10. array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81','ProdID'=>'2','Quant'=>'1'),
  11. array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81','ProdID'=>'6','Quant'=>'1'),
  12. array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81','ProdID'=>'8','Quant'=>'1'),
  13. array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProdID'=>'21','Quant'=>'1'),
  14. );

I am still using a 'for loop' to update the catalog as below.

Expand|Select|Wrap|Line Numbers
  1. for ($y = 0, $size = count($cart); $y < $size; $y++)
  2.     {
  3.     for ($x = 0, $size = count($catalog); $x < $size; $x++)
  4.         {
  5.         if ($catalog[$x]['ProdID'] == ($cart[$y]['ProdID']))
  6.             {                                   
  7.             $catalog[$x]['InBasket'] = 1;
  8.             }
  9.         }
  10.     }
When this is run the result is as shown below and you will notice the 'InBasket' cells have the correct value in them.


Expand|Select|Wrap|Line Numbers
  1. Array ( 
  2. [0] => Array ( [ProdID] => 2 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  3. [1] => Array ( [ProdID] => 6 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  4. [2] => Array ( [ProdID] => 7 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  5. [3] => Array ( [ProdID] => 8 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  6. [4] => Array ( [ProdID] => 9 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
Perhaps you could show me the foreach version for this code.

Kind regards.
Aug 1 '12 #12
Claus Mygind
571 512MB
I was away on vacation for a few days and did not have a chance to check in. Here are the code suggestions.

While there are times that you need to loop through an associative array, the beauty of these arrays is the direct access, thereby eliminating the need for a loop. So below I show you the two ways to update your catalog with and without the "foreach" loop.

In my opinion the most efficient method for your sample code is to not loop through the catalog. But it is good to know how to use both methods.

Expand|Select|Wrap|Line Numbers
  1.         /*
  2.         ----------------------------------------------------
  3.         In the catalog array the product id becomes the key
  4.         instead of the index key.  I showed you in an 
  5.         earlier post how to create this array dynamically.
  6.  
  7.         The key can be created both with or without the 
  8.         quotes. However the same method should be used
  9.         in both the catalog and cart arrays.
  10.         ie:
  11.         $catalog = array( 
  12.             '2' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  13.         );
  14.  
  15.         $cart[] = array(
  16.             array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81' ,'ProdID'=>'2','Quant'=>'1'),
  17.         );
  18.  
  19.         OR:
  20.         $catalog = array( 
  21.             2 => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  22.         );
  23.  
  24.         $cart[] = array(
  25.             array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81' ,'ProdID'=>2,'Quant'=>'1'),
  26.         );
  27.  
  28.  
  29.         ----------------------------------------------------
  30.         */
  31.         $catalog = array( 
  32.             '2' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  33.             '6' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  34.             '7' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  35.             '8' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  36.             '9' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0')
  37.         );
  38.  
  39.         $cart[] = array(
  40.             array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81' ,'ProdID'=>'2','Quant'=>'1'),
  41.             array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81'  ,'ProdID'=>'4','Quant'=>'1'),
  42.             array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81' ,'ProdID'=>'8','Quant'=>'1'),
  43.             array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProdID'=>'21','Quant'=>'1'),
  44.         );                                                                    
  45.  
  46.     /*
  47.     ------------------------------------------
  48.     Example using "foreach" loop
  49.     ------------------------------------------
  50.     */
  51.     for ($y = 0, $size = count($cart); $y < $size; $y++)
  52.     {
  53.         foreach ($catalog as $key => $value ) 
  54.         {
  55.             if ($key == ($cart[$y]['ProdID']))
  56.             {
  57.                 $catalog[$key]['InBasket'] = 1;
  58.             }
  59.         }
  60.     }     
  61.  
  62.     /*
  63.     ------------------------------------------
  64.     Example eliminating the "foreach" loop 
  65.     ------------------------------------------
  66.     */
  67.     for ($i = 0, $size = count($cart); $i < $size; ++$i)
  68.     {
  69.         $key = $cart[$i]['ProductID'];
  70.         $catalog[$key]['InBasket'] = 1;
  71.     }
  72.  
  73.  
Aug 4 '12 #13
Hi Claus

Hope you had a good break and are now back in full harness ?

While you were away I have been digging into the font of knowledge and have come up with the following code in a foreach loop which gives me the correct answer

Expand|Select|Wrap|Line Numbers
  1. $catalog = array( 
  2. array('ProductID'=>2,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  3. array('ProductID'=>6,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  4. array('ProductID'=>7,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  5. array('ProductID'=>8,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  6. array('ProductID'=>9,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  7. );
  8.  
  9. $cartitems = array(
  10. array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81','ProductID'=>'2','Quant'=>'1'),
  11. array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81','ProductID'=>'6','Quant'=>'1'),
  12. array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81','ProductID'=>'8','Quant'=>'1'),
  13. array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProductID'=>'21','Quant'=>'1'),
  14. );
  15.  
  16.  
  17. foreach ($catalog as &$department) { 
  18. foreach($cartitems as $basket) { 
  19.     if ($department['ProductID'] == $basket['ProductID']) {  
  20.        $department['InBasket'] = 1; 
  21.     } 
  22.   } 
  23. }
  24. print_r($catalog);
  25.  
  26. Array ( 
  27. [0] => Array ( [ProductID] => 2 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  28. [1] => Array ( [ProductID] => 6 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  29. [2] => Array ( [ProductID] => 7 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  30. [3] => Array ( [ProductID] => 8 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  31. [4] => Array ( [ProductID] => 9 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
Thanks for your help and input, and I'm sure I will be back with more questions.

Kind regards.
Aug 5 '12 #14
Claus Mygind
571 512MB
John,
Not sure if you are still looking at this thread. If you are -

1) Sorry, I should have tested my code before I posted it to make sure it was correct

2) You are still not using an associative array (look at your own results above the output of the array is indexed 0 - 4)

3) Run my code below. I have tested it and it works (look at the output of the last print statement and notice the array element keys are listed). Just click "select" and ctrl-c to copy code. I have included in the code the expected output at each print statement.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $catalog = array(
  3.     array('ProductID'=>2,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  4.     array('ProductID'=>6,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  5.     array('ProductID'=>7,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  6.     array('ProductID'=>8,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  7.     array('ProductID'=>9,'Name'=>'Alpha','DepID' =>2,'InBasket'=>'0')
  8. );
  9. echo "<br/><b>Your array setup --- Notice catalog array is indexed</b><br/>";
  10. print_r($catalog);
  11. /*Ouput of array shows it is an indexed array
  12. Array ( 
  13. --------------------------------------------------------------------
  14.     [0] => Array ( [ProductID] => 2 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  15.     [1] => Array ( [ProductID] => 6 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  16.     [2] => Array ( [ProductID] => 7 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  17.     [3] => Array ( [ProductID] => 8 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  18.     [4] => Array ( [ProductID] => 9 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) )
  19.  
  20. */
  21. $cartitems = array( 
  22.     array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81','ProductID'=>'2', 'Quant'=>'1'), 
  23.     array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=> '81','ProductID'=>'6', 'Quant'=>'1'), 
  24.     array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81','ProductID'=>'8', 'Quant'=>'1'), 
  25.     array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID' =>'81','ProductID'=>'21','Quant'=>'1')
  26. );
  27.  
  28. foreach ($catalog as &$department) {
  29.     foreach($cartitems as $basket) {
  30.         if ($department['ProductID'] == $basket['ProductID']) {
  31.             $department['InBasket'] = 1;
  32.         }
  33.     }
  34. }
  35. echo "<br/><br/><b>Notice catalog array is indexed</b><br/>";
  36. print_r($catalog);
  37. /*Output shows an indexed array not an associtive array
  38. Notice catalog array is indexed
  39. Array ( 
  40. --------------------------------------------------------------------
  41.     [0] => Array ( [ProductID] => 2 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  42.     [1] => Array ( [ProductID] => 6 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  43.     [2] => Array ( [ProductID] => 7 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  44.     [3] => Array ( [ProductID] => 8 [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  45.     [4] => Array ( [ProductID] => 9 [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) )
  46.  
  47.  
  48. */
  49.  
  50.             /*
  51.             ----------------------------------------------------
  52.             Catalog array built as an associtive array
  53.             ----------------------------------------------------
  54.             */
  55.             $catalog = array( 
  56.                 '2' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  57.                 '6' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  58.                 '7' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  59.                 '8' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  60.                 '9' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0')
  61.             );
  62. echo "<br/><br/><b>Associtive Array --- Catalog array before update</b><br/>";
  63.  print_r($catalog);
  64.  
  65.             $cart = array(
  66.                 array('Name'=>'Alpha2','Loc'=>'e_vol1','BasketID'=>'81' ,'ProdID'=>'2','Quant'=>'1'),
  67.                 array('Name'=>'Beta4','Loc'=>'e_vol2','BasketID'=>'81'  ,'ProdID'=>'4','Quant'=>'1'),
  68.                 array('Name'=>'Alpha8','Loc'=>'e_vol3','BasketID'=>'81' ,'ProdID'=>'8','Quant'=>'1'),
  69.                 array('Name'=>'Gamma21','Loc'=>'e_vol4','BasketID'=>'81','ProdID'=>'21','Quant'=>'1'),
  70.             );
  71. echo "<br/><br/><b>Cart Array</b> <br/>";        
  72.  print_r($cart);    
  73.  
  74.         /*
  75.         ------------------------------------------
  76.         Example using "for next" and  "foreach" loop
  77.         ------------------------------------------
  78.         */
  79.         for ($y = 0, $size = count($cart); $y < $size; $y++)
  80.         {
  81.             foreach ($catalog as $productId => $value ) 
  82.             {
  83.                 //test if product id is found in catalog.  If so update catalog array
  84.                 if ($productId == ($cart[$y]['ProdID']))
  85.                 {
  86.                     $catalog[$productId]['InBasket'] = 1;
  87.                 }
  88.             }
  89.         }     
  90. echo "<br/><br/><b>Associtive Array --- Catalog Array  after foreach loop</b><br/>";
  91.  print_r($catalog);    
  92.  
  93. /* Output
  94. Catalog Array after foreach loop
  95. --------------------------------------------------------------------
  96. Array ( 
  97.     [2] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  98.     [6] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  99.     [7] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  100.     [8] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  101.     [9] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) )
  102. */        
  103.             /*re-initilaze catalog array*/
  104.             $catalog = array( 
  105.                 '2' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),        
  106.                 '6' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  107.                 '7' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  108.                 '8' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0'),
  109.                 '9' => array('Name'=>'Alpha','DepID' =>2,'InBasket'=>'0')
  110.             );
  111.  
  112.  
  113.         /*
  114.         ------------------------------------------
  115.         Example eliminating one of the "foreach" loop 
  116.         ------------------------------------------
  117.         */
  118.         for ($i = 0, $size = count($cart); $i < $size; ++$i)
  119.         {
  120.             $productId = $cart[$i]['ProdID'];
  121.  
  122.             //test if product id is found in catalog.  If so update catalog array
  123.             if (isset($catalog[$productId])){ 
  124.                 $catalog[$productId]['InBasket'] = 1;
  125.             }
  126.         }
  127. echo "<br/><br/><b>Associtive Array --- Catalog Array 2 after no foreach loop</b><br/>";
  128.  print_r($catalog); 
  129.  
  130. /* Output 
  131. Catalog Array 2 after eliminating one foreach loop
  132. --------------------------------------------------------------------
  133. Array ( 
  134.     [2] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  135.     [6] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  136.     [7] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) 
  137.     [8] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 1 ) 
  138.     [9] => Array ( [Name] => Alpha [DepID] => 2 [InBasket] => 0 ) ) 
  139. */
  140. ?>
  141.  
Aug 5 '12 #15
Hi Claus

I'm still confused by these associative arrays, these are the two functions I use to extract the data into the 'Cart' and 'Catalog' arrays

Expand|Select|Wrap|Line Numbers
  1. Function GetBasketItems()
  2. {
  3. //Used to get all the items in the customers basket
  4. require '../includes/cxwdb.inc.php';
  5. session_start();
  6. unset($_SESSION['CartItems']);
  7. $BasID  = $_SESSION['custDetails']['CustomerID'];
  8. $sql    = "call procGetAllItems($BasID)";
  9. $result = mysqli_query($link, $sql);
  10.  
  11. if (!$result)
  12.     {
  13.     $error  = mysqli_errno($link) . ": " . mysqli_error($link) . "\n";    
  14.     $error1 = $prodID;
  15.     $error2 = $BasID;
  16.     include '../err/error.html.php';
  17.     exit();    
  18.     }
  19.     if (mysqli_num_rows($result) > 0)
  20.         {
  21.         while ($row  =  mysqli_fetch_array($result)) 
  22.         $cItems[]      = array(
  23.         'Name'       => $row['Name'], 
  24.         'ProductID'  => $row['ProductID'], 
  25.         'Quantity'   => $row['Quantity'],
  26.         'Price'      => $row['Price'], 
  27.         'LinePrice'  => $row['LinePrice'],
  28.         'Location'   => $row['Location'],
  29.         'Discount'   => $row['Discount'],
  30.         'BasketID'   => $row['BasketID'],
  31.         'countrytax' => $row['countrytax'],
  32.         'LineTax'    => $row['LineTax']);
  33.     $_SESSION['CartItems'] = $cItems;
  34.         return true;
  35.         }
  36.         Else        {
  37.                 return false;
  38.                 }
  39. }
  40.  
  41.  
  42.  
  43. Function GetCatalogue($DeptID = 0)
  44. {
  45. unset($tempcat);
  46. //unset($_SESSION['Catalogue']);
  47. If ($DeptID > 0)
  48. {
  49.     include '../includes/cxwdb.inc.php';
  50.     $result = mysqli_query($link, "call procGetCatalogue($DeptID)");
  51. if (!$result)
  52.     {
  53.     $error =  mysqli_errno($link) . ": " . mysqli_error($link) . "\n";    
  54.     $error1 = $prodID;
  55.     $error2 = $BasID;
  56.     include '../err/error.html.php';
  57.     exit();        
  58.     }
  59.     if (mysqli_num_rows($result) > 0)
  60.         {
  61.         while ($row      =  mysqli_fetch_array($result))
  62.         $tempcat[]       = array(
  63.         'ProductID'      => $row['ProductID'],         
  64.         'Name'           => $row['Name'],
  65.         'DepartmentID'   => $row['DepartmentID'], 
  66.         'Price'          => $row['Price'],
  67.         'Location'       => $row['Location'], 
  68.         'DepartmentName' => $row['DepartmentName'], 
  69.         'Discount'       => $row['Discount'],
  70.         'InBasket'       => $row['InBasket']);
  71.         Return $tempcat;
  72.         }
  73. }
  74. }
  75.  
  76.  
What do I need to do to these to return associative arrays ?

Kind regards
Aug 6 '12 #16
Claus Mygind
571 512MB
It is in the way you create the array. You use the [] push method to add each row to the array. If you assign the Product Id to the row key then it becomes associative.

Take a look at the code below. I commented out your method of creating the array and inserted the associative array creation method. (CAUTION!!! I did not test - but this is the same way I created the array in the test code).

Remember to access the data in the associative array you need to retrieve the key not the value. You will want to compare the "ProductId" from the cart array (which is indexed) to the "key" of the catalog array. So in the "foreach" loop use this syntax "foreach (array_expression as $key => $value)".

But my suggestion the whole time has been to eliminate that 2nd loop. That is the real advantage of the assoc. array.

Look for my code block above with this code:
"for ($i = 0, $size = count($cart); $i < $size; ++$i)
{
$productId = $cart[$i]['ProdID'];

//test if product id is found in catalog. If so update catalog array
if (isset($catalog[$productId])){
$catalog[$productId]['InBasket'] = 1;
}
}
"


Expand|Select|Wrap|Line Numbers
  1.             if (mysqli_num_rows($result) > 0)
  2.             {
  3.                 while ($row      =  mysqli_fetch_array($result))
  4.                 {
  5.                     /*
  6.                     ---------------------------------------
  7.                     HERE you are creating an indexed array
  8.                     since you use [] push method to create
  9.                     each row in the array it is indexed.
  10.  
  11.                     The first dimension is indexed. Then
  12.                     within the row you create an assoc array
  13.                     with the  key => value pair
  14.                     ---------------------------------------
  15.                     $tempcat[]       = array(
  16.                         'ProductID'      => $row['ProductID'],         
  17.                         'Name'           => $row['Name'],
  18.                         'DepartmentID'   => $row['DepartmentID'], 
  19.                         'Price'          => $row['Price'],
  20.                         'Location'       => $row['Location'], 
  21.                         'DepartmentName' => $row['DepartmentName'], 
  22.                         'Discount'       => $row['Discount'],
  23.                         'InBasket'       => $row['InBasket']);
  24.  
  25.  
  26.                     ---------------------------------------
  27.                     Here an Assoc. array is created and
  28.                     the Product Id is the row key.
  29.  
  30.                     Each time you loop around a new row is
  31.                     added and it's key is the Product Id
  32.                     ---------------------------------------
  33.                     */
  34.                     $tempcat[$row['ProductID']] = array(
  35.                         'Name'           => $row['Name'],
  36.                         'DepartmentID'   => $row['DepartmentID'], 
  37.                         'Price'          => $row['Price'],
  38.                         'Location'       => $row['Location'], 
  39.                         'DepartmentName' => $row['DepartmentName'], 
  40.                         'Discount'       => $row['Discount'],
  41.                         'InBasket'       => $row['InBasket']);
  42.                 }
  43.  
  44.                     Return $tempcat;
  45.             }
  46.  
Aug 6 '12 #17
Claus Mygind
571 512MB
Some times I get a little long winded. The only difference between creating the arrays are

$tempCart[]

and

$tempCart[$row['ProductId']]

In the 2nd method you assign a value to the array element's key. And that value can be alphanumeric ie: $tempCart['Claus'].

Where an indexed array just assigns the next number.

Interestingly if you do this

$tempCart['6'] = 'something';
$tempCart[] = 'something else';

and print out they array you would get

[6] = something
[7] = something else
Aug 6 '12 #18
Hi

When I tried the code
Expand|Select|Wrap|Line Numbers
  1. $tempcat  $row['ProductID'] => array(
  2. 'Name'           => $row['Name'],
  3. 'DepartmentID'   => $row['DepartmentID'], 
  4. 'Price'          => $row['Price'],
  5. 'Location'       => $row['Location'], 
  6. 'DepartmentName' => $row['DepartmentName'], 
  7. 'Discount'       => $row['Discount'],
  8. 'InBasket'       => $row['InBasket']);
  9. Return $tempcat;
  10.  
I get the error
Parse error: syntax error, unexpected T_VARIABLE on line xx


In VB I'm only used to one kind of array which has rows and columns i.e array(0 to 5, 0 to 9) and it is filled from a recordset with one command 'GetRows'

Considering my arrays are so small what are the performance issues between associative and indexed arrays are we talking clock cycles here or much more
Aug 6 '12 #19
Claus Mygind
571 512MB
I am sure there is not a big time difference.

But your error is because you do not have the [ ] for the $tempcat and you use the "=>" instead of "="

you wrote
$tempcat $row['ProductID'] => array(

it should be
$tempcat[$row['ProductID']] = array(

I suspect you never worked with dictionaries in VB6, then you would have been looking at something similar to assoc. arrays.

If you decided to stick with indexed arrays then you should go back to the for/next looping method.
Aug 6 '12 #20
Hi Claus

Both the for/next and the foreach loops work at the moment and provide what I need. They will not be called thousands of times so I feel that I am losing a lot of time without gain.

Thank you very much for your help and input it was a shame I was such a poor student, however, when time is on my side I will return and have another go at these arrays.

Kind regards.
Aug 6 '12 #21
Claus Mygind
571 512MB
You were not a poor student. Sorry I did not get the right info to you in a timely manner.

While it is too late to change things now php actually has a number of built in functions that handle arrays. In this case there is a specific function which would have eliminated the need for loops.

John could have used the array_intersect() to compare his two arrays. This would have given him a list of array elements in the catalog array to update with the new value.
Aug 9 '12 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
5
by: chella mani | last post by:
hi all, I am developing a tool for scientific application which must be time and memory efficient,my tool uses 1D,2D and 3D arrays. Is it a good practice to use 3D arrays in applications or I...
27
by: jacob navia | last post by:
Has anyone here any information about how arrays can be formatted with printf? I mean something besides the usual formatting of each element in a loop. I remember that Trio printf had some...
38
by: Peteroid | last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the addresses were contiguous. Is this guaranteed, or just something it does if it can? PS = VS C++.NET 2005 Express...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
1
by: assgar | last post by:
Hello I have changed the process code abit so it receives the data from the form and ensures the data in array format. This has eliminated my previous error. The problem I am experiencing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.