Jump to content

Recommended Posts

Hi all, I'm trying to set up a situation where a function loops through a multidimensional array and echos an html block for each item in the $skus['skunum'] array and also increments $i each time . ( Each is a product on the site) 

The catch is that I'm trying to have a function within that one that goes through the $skus['sizes'] and echoes an option if the substring (shirt size) exists.

 

The idea being that I can manage products by adding 4 elements, Skunum, name, price and size.

 

And well, I'm failing miserably.  :-[

 

At this point it won't even work because of the variable scope, I'm getting Notice: Undefined variable: skus

from the functions, but am not sure of the right way to get that information.

 

Am I going about doing this entirely wrong or am I on the right track?

 

<?php $BANDNAME="Apocalypse"; 
$BANDCAPS="APOCALYPSE";
$BANDLOWER="apocalypse";
$SKUCAPS="FGD";
$skus = array
  (
  "skunum"=>array
  (
  "101",
  "102",
  "103", 
  "104",
  "105", 
  "106", 
  "107", 
  "108", 
  "109", 
  "110", 
  "111", 
  "112" 
  ),
  "name"=>array
  (
  "RIDDICK /TSHIRT",
  "MAFIA / TSHIRT",
  "ORACLES / TSHIRT", 
  "AGONY / TSHIRT",
  "BLOODY VIOLINIST / TSHIRT",
  "THE VIOLATION / TSHIRT",
  "AGONY-TOUR DATES / TSHIRT",
  "BAND PHOTO / TSHIRT",
  "SILVER AGONY LOGO / TSHIRT",
  "PHOENIX-TOUR DATES / TSHIRT",
  "PHOENIX / TSHIRT",
  "BLOODY VIOLINIST / ZIP HOOD"
  ),
  "price"=>array
  (
  "15.95",
  "15.95",
  "15.95", 
  "15.95",
  "15.95",
  "15.95",
  "15.95",
  "15.95",
  "15.95",
  "15.95",
  "15.95",
  "42.95"
  ),  
  "sizes"=>array
  (
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X",
  "S, M, L, 1X, 2X, 3X, 4X"
  )
  
  );

function makeProducts() {
	 //Products Sizes
$sizes = $skus['sizes'][$i];
$S="S";$M="M";$L="L";$XL="1";$XXL="2";$XXXL="3";$XXXXL="4";

$small = strpos($sizes,$S);
$medium = strpos($sizes,$M);
$large = strpos($sizes,$L);
$xlarge = strpos($sizes,$XL);
$xxlarge = strpos($sizes,$XXL);
$xxxlarge = strpos($sizes,$XXXL);
$xxxxlarge = strpos($sizes,$XXXXL);

function sizeOptions () {
	if($small === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'S">S</OPTION>';
	}
	if($medium === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'M">M</OPTION>';
	}
	if($large === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'L">L</OPTION>';
	}
	if($xlarge === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'X">XL</OPTION>';
	}
	if($xxlarge === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XX">XXL</OPTION>';
	}
	if($xxxlarge === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXX">XXXL</OPTION>';
	}
	if($xxxxxlarge === true) {
	 echo '<OPTION value="'.$SKUCAPS.$skus['skunum'][$i].'XXXX">XXXXL</OPTION>';
	}
}			

foreach ( $skus['skunum'] as $value){ 
  
   
  

  		echo '<!-- PRODUCT BEGIN -->
  <li class="product" >
						<a href="product_files/large/'.$SKUCAPS.$skus['skunum'][$i].'jpg" rel="lightbox"><img src="product_files/'.$SKUCAPS.$skus['skunum'][$i].'.png"></a><br>
                            <strong>'.$skus['name'][$i].'</strong><br>('.$SKUCAPS.$skus['skunum'][$i].')<br>$'.$skus['price'][$i].'<br>
                            <form name="'.$SKUCAPS.$skus['skunum'][$i].'" method="GET" target="_blank" action="http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi">
                            <input type="hidden" name="band" value="'.$BANDCAPS.'"> <input type="hidden" name="back" value="http://www.jsrdirect.com/bands/'.$BANDLOWER.'/index.html">
                            <strong>Qty:</strong>
                            <input type="text" name="quantity" size="1" value="1" >
                            <strong>Size:</strong>  
                            <SELECT name="item">'
						.sizeOptions();
						'</SELECT>
                            <br><br>
                            <input type="image" src="images/addtocart.png" value="Add To Cart">
                            </form>
                   	    </li>
               		<!-- Product End -->';
						 }
  									
						} // End Function

?>

If you have a variable declared outside a function that you need to use inside a function, then pass that variable as one of the parameters for the function!

 

Although, I would suggest changing the format of your array. Creating different subsets for sku's, name, sizes, etc. as you have them make it difficult to work with. Especially if you need to manually edit them. I would suggest an array where the sku number id the index and the value is an array of the parameters for that sku item.

    '101' => array(
          'name'  => 'RIDDICK /TSHIRT',
          'price' => '15.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      ),

    '102' => array(
          'name'  => 'MAFIA / TSHIRT',
          'price' => '15.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      ),

Thank you for the advice! It really cleans it up and makes editing easier.

 

I'm still confused as to how to properly create a function that will check the sizes array  & print a string  if the size exists.

 

I guess I'm not totally getting the whole "pass a variable into a function by adding it as a parameter " because no matter how I try to do that I get errors.

 

$BANDNAME="Apocalypse"; 
$BANDCAPS="APOCALYPSE";
$BANDLOWER="apocalypse";
$SKUCAPS="FGD";

function makeProducts() {
 $BANDNAME="Apocalypse"; 
$BANDCAPS="APOCALYPSE";
$BANDLOWER="apocalypse";
$SKUCAPS="FGD";

$skus = array
  (
   '101' => array(
          'name'  => 'RIDDICK /TSHIRT',
          'price' => '15.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      ),

    '102' => array(
          'name'  => 'MAFIA / TSHIRT',
          'price' => '15.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      )
  
   

  
  );

   //Products Sizes I'M HAVING AN ISSUE CREATING THIS FUNCTION

function makeSizes($skus) {

 $key = $skus['sizes'];

if ( in_array("S", $key )) {
	echo '<OPTION value="'.$SKUCAPS.$skunum.'S">S</OPTION>';
	}
  }   //end function

  
  foreach ( $skus as $skunum => $value){ 

   		echo '<!-- PRODUCT BEGIN -->
  <li class="product" >
						<a href="product_files/large/'.$SKUCAPS.$skunum.'.jpg" rel="lightbox"><img src="product_files/'.$SKUCAPS.$skunum.'.png" alt="'.$BANDNAME.' Merch T-shirts"></a><br>
                            <strong>'.$value['name'].'</strong><br>('.$SKUCAPS.$skunum.')<br>$'.$value['price'].'<br>
                            <form name="'.$SKUCAPS.$skunum.'" method="GET" target="_blank" action="http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi">
                            <input type="hidden" name="band" value="'.$BANDCAPS.'"> <input type="hidden" name="back" value="http://www.jsrdirect.com/bands/'.$BANDLOWER.'/index.html">
                            <strong>Qty:</strong>
                            <input type="text" name="quantity" size="1" value="1" >
                            <strong>Size:</strong>  
                            <SELECT name="item">'
						. makeSizes($value). // TRYING TO CALL THE FUNCTION  HERE
						'</SELECT>
                            <br><br>
                            <input class="css3button" value="Add To Cart" type="image">
                            </form>
                   	    </li>
               		<!-- Product End -->';
						 }

					  }


?>

Functions have an argument/parameter list, which is denoted by parentheses.  When you define your function, you can tell it to accept parameters to use inside the function in order to do work on them:

 

function example($arg1, $arg2)
{
   // do something with $arg1 and $arg2
}

 

In the code above, the function example will take two parameters and act on them.  When you want to actually invoke your function, you'll need to place the variables you want to pass into the function within its parentheses:

 

$x = 3;
$y = "Yo momma";

example($x, $y);

 

If you need your function to create a value to use in the rest of your code, you need to return it, like so:

 

function example($arg1, $arg2)
{
   // do something with $arg1 and $arg2

   return $someResult;
}

// ... code ...

$x = 3;
$y = "Yo momma";

$result = example($x, $y);

 

For more detail, look at functions

I'm still confused as to how to properly create a function that will check the sizes array  & print a string  if the size exists.

 

I guess I'm not totally getting the whole "pass a variable into a function by adding it as a parameter " because no matter how I try to do that I get errors.

 

Well, your code is a bit confusing as to what you are really trying to accomplish. Plus, you are putting functions within functions. There is typically no reason to do that. It definitely makes no sense in your scenario. In fact, you are defining the products within a function and not doing anything with them IN that function. I'll give you an example script which I think is close to what you want and you can modify it as needed

 

<?php

//Put the array is a separate file and include it here.
//Makes management much easier
$products = array
  (
   '101' => array(
          'name'  => 'RIDDICK /TSHIRT',
          'price' => '25.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      ),

    '102' => array(
          'name'  => 'MAFIA / TSHIRT',
          'price' => '15.95',
          'sizes' => array('S', 'M', 'L', '1X', '2X', '3X', '4X')
      )
);

//Only need to specify band name. You can programatically create 
//all uppercase or all lower case as needed
$band_name = "Apocalypse";
$sku = "FGD";

function createProductForm($bandname, $sku, $productArray)
{
    $BANDCAPS  = strtoupper($bandname);
    $BANDLOWER = strtolower($bandname);

    foreach($productArray as $skunum => $prodInfo)
    {
        $prodID = $sku.$skunum;
        $sizeOptions = '';
        foreach($prodInfo['sizes'] as $size)
        {
            $sizeOptions .= "<option value='{$prodID}{$size}'>{$size}</option>\n";
        }
        echo "<!-- PRODUCT BEGIN -->\n";
        echo "<li class='product'>\n";
        echo "<a href='product_files/large/{$prodID}.jpg' rel='lightbox'><img src='product_files/{$prodID}.png' alt='{$bandname} Merch T-shirts'></a>\n";
        echo "<br>\n";
        echo "<strong>{$prodInfo['name']}</strong><br>({$prodID})<br>\${$prodInfo['price']}<br>\n";
        echo "<form name='{$prodID}' method='GET' target='_blank' action='http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi'>\n";
        echo "<input type='hidden' name='band' value='{$BANDCAPS}'>\n";
        echo "<input type='hidden' name='back' value='http://www.jsrdirect.com/bands/{$BANDLOWER}/index.html'>\n";
        echo "<strong>Qty:</strong>\n";
        echo "<input type='text' name='quantity' size='1' value='1'>\n";
        echo "<strong>Size:</strong> \n";
        echo "<select name='item'>\n";
        echo $sizeOptions;
        echo "</select>\n";
        echo "<br><br>\n";
        echo "<input class='css3button' value='Add To Cart' type='image'>\n";
        echo "</form>\n";
        echo "</li>\n";
        echo "<!-- Product End -->\n";
    }
}

?>
<html>
<head></head>
<body>
<h1>Order Form</h1>
<?php createProductForm($band_name, $sku, $products); ?>
</body>
</html>

Can functions even be defined in other functions?

 

Yes. Absolutely. Of course, they would only have scope within the function that defines them - just like variables.

 

EDIT: I was wrong. You can define a function within a function and it will have global scope. BUT - you must call the function that defines the "sub" function before it becomes available.

Would it then be a closure?

I don't believe so. But, it can be used to define functions differently based upon certain parameters. With OOP it would be similar to dynamically loading subclasses to extend the main class. Here is a ROUGH example of how you could use it (not that this is a good example)

 

function createSayThankYou($language)
{
    switch($language)
    {
        case 'sp':
            function sayThankYou() { echo "gracias."; }
            break;
        case 'ge':
            function sayThankYou() { echo "danke."; }
            break;
        case 'en':
        default:
            function sayThankYou() { echo "thank you."; }
            break;
    }
}

//Dynamically create the function to say thank you
createSayThankYou($users_language);

sayThankYou(); //Will output thank you based on function created above

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.