Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

HELP: PHP!!!


FlyingMonkey

Recommended Posts

Hi,

Thank you very much for your help. I've abstracted the code, to make it easier to read. This is going to be used to update a contribution. The problem is that I am not able to pass $myArray with the information gained from the first while loop, to anywhere outside of it. Thanks again for your help.

$myArray =array();
$result = mysql_query( $sql );
while ( $row = mysql_fetch_object( $result ) ) ?//builds the array
{
$myArray[$row->id]=$row->info;
echo $myArray[row->id]; ?//works
}

echo $myArray[1]; //doesn't work, where 1 is an id in the array

//processMe requires the entire built array and needs to process each id with the entire array.
foreach($myArray as $id=>$info) 
{
echo $myArray[$id]; ?//doesn't work
processMe($id, $myArray);
echo $myArray[$id]; ?//doesn't work
}

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

Can you explain what this element of code is doing - may help us understand what you are attempting to do.

Your online success is Paramount.

Link to comment
Share on other sites

Hi,

 

First off, I'm not an expert on PHP arrays. :D

 

Try this ..........

 

foreach ($myArray as $value) {
  echo "Value: $value<br />\n";
}
?>

 

OR .....

 

foreach ($myArray as $key => $value) {
  echo "Key: $key; Value: $value<br />\n";
}
?>

 

Also, as it seems your array is multidimensional, I _think_ that is why you cannot do this .......

 

echo $myArray[1];

 

you would need to reference the dimensions like this

 

echo $myArray[0][0];
echo $myArray[0][1];
echo $myArray[1][0];
echo $myArray[1][1];
//etc,etc

 

Also try this ....

 

foreach ($myArray as $v1) {
  foreach ($v1 as $v2) {
      echo "$v2\n";
  }
}

 

Hope that helps,

 

Peter

Link to comment
Share on other sites

Can you explain what this element of code is doing - may help us understand what you are attempting to do.

It's attempt to index the category structure from the product to the root. Originally, I wrote code to find the category structure for each individual product... but it took n*m^2 time. Where n is the number of products and m is the number of categories. So a solution, I came up with is to index all of the categories, so that it could be easily called for each product instead of finding the category structure each time for each product. This would in turn bring the time down to n+m^2, then i thought of additional optimizations to bring the code down to n+m time.

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

ahh, thanks peterr. it's sorta given me some ideas. it doesn't work still and i'm not positive about theproblem.

 

when i use the

 foreach ($myArray as $v1) {
 foreach ($v1 as $v2) {
     echo "$v2\n";
 }
}

 

It doesn't print out anything, but when i use the:

foreach ($myArray as $key => $value) {
 echo "Key: $key; Value: $value<br />\n";
}

It prints just the key

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

here's the actual code

 

$catIndex = array();
$catTempDes = array();
$catTempPar = array();
$processCat = mysql_query( $catInfo )or die( $FunctionName . ": SQL error " . mysql_error() . "| catInfo = " . htmlentities($catInfo) );
while ( $catRow = mysql_fetch_object( $processCat ) )
{
$catTempPar[$catRow->prodCatID]=$catRow->parentCatID;
$catTempDes[$catRow->prodCatID]=$catRow->catName;
echo $catTempDes[$catRow->prodCatID];
}

echo "<br>BETWEEN: " . $catTempDes[22];


foreach ($catTempDes as $v1) {
 foreach ($v1 as $v2) {
     echo "$v2\n";
 }
}

foreach($catTempDes as $curID=>$des)
{
echo "<br>Pass to findCat: key:" .  $curID ." value:" . $des . " " . $catTempDes[$curID];
$temp = "";
findCat($curID, $catTempPar, $catTempDes);
}

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

Hi,

 

Good reading on arrays at:

 

http://www.php.net/manual/en/language.types.array.php

 

http://au.php.net/manual/en/ref.array.php

 

From the last link, this is how to calculate how many dimensions are in an array:

 

szymon at mazurek dot info

24-Aug-2003 07:46

How to count dimensions in multi-array?

This is the way I do this:

 

function countdim($array) {

  static $dimcount = 1;

  if (is_array(reset($array))) {

      $dimcount++;

      $return = countdim(reset($array));

  } else {

      $return = $dimcount;

  }

  return $return;

}

 

This function will return int number of array dimensions.

 

I also found this useful for dumping the contents of an array.

 

/*4a quick look*/
foreach($domains as $k=>$v){
echo $k;
print_r($v);
}
?>

 

Peter

Link to comment
Share on other sites

foreach ($myArray as $v1) {
foreach ($v1 as $v2) {
    echo "$v2\n";
}
}

 

Change your PHP settings to show you all errors and warnings this should tell you expecting array $v1.

 

foreach ($myArray as $key => $value) {
echo "Key: $key; Value: $value<br />\n";
}

 

This is a key/value pair, if it was an array of arrays the value would print 'Array'.

 

Use print_r($myArray) and view source to inspect the array structure.

 

Try something like this print_r($cart); in the checkout pages.

Link to comment
Share on other sites

It seems like the array is created sucessfully with the correct keys. such as a[22], a[27]. but the values for these positions are not stored, even when i tried dumping all of the contents of the array out as you guys suggested. however, in:

$catTempDes = array();
$catTempPar = array();
while ( $catRow = mysql_fetch_object( $processCat ) )
{
$catTempPar[$catRow->prodCatID]=$catRow->parentCatID;
$catTempDes[$catRow->prodCatID]=$catRow->catName;
echo $catTempDes[$catRow->prodCatID]; //WORKS!
echo "COUNT DIM1: " . countdim($catTempDes);
}

 

it's able to print out the values... but then it's unable to do it outside of the while loop:

foreach ($catTempDes as $v1) {
?foreach ($v1 as $v2) {
? ? ?echo "$v2\n";
?}
}

echo "<br>###########PRINTR#########: <br>";
foreach($catTempDes as $k=>$v){
echo " K: ". $k;
print_r($v);
}

 

These only print the location in the array, meaning that it prints only k and not v as in the second example.

 

Any insight is appreciated.

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

GOT IT! Turned out not to a PHP problem... but my own stupidity. From the SQL query, for some reason the it gives 25=>Candy, 25=>Candy, 25=>Candy, 25=>Candy, 25=>, 25=>

 

I originally thought all of the 25's would have values, but for some reason, at the end they don't. So when the array was created it was overwritten everytime at the end with "nothing"... hence when I printed out the values in the original array, only the ones with values were printed out. But when it was printed out all at once everything was created, but all of the values were already replaced by "nothing", so it printed out "nothing".

 

i finally figured it out, when i created a starting an array with keys and values and noticed those worked, then i decided that it was still a php scoping problem. so i stored all of the contents in a string as it was being created and printed that out... and said "WHOLLY MOLLY!"

Most likely your question has been answered, please do a search first.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...