Jump to content

Echo product size if it exists in array


willboudle

Recommended Posts

I'm a total noob trying to figure out how to get this function to work right.  I want to echo  for each

'<OPTION value="'.$SKUCAPS.$skunum.$size.'">'.$size.'</OPTION>'

 

for each size in the $skus[$type][$skunum]['sizes'] array. and if no size exists echo

'<input value="'.$SKUCAPS.$skunum.'" type=hidden" name="item">'

 

 

<?php

$BANDNAME="Band Name"; 
$BANDCAPS="BANDNAME";
$BANDLOWER="bandname";
$SKUCAPS="BND";
$SKULOWER="bnd";

$skus = array(


'shirts' => array( 

			'165' => array(
				  'name'  => 'World Khaos Tour <br> Limited Edition Tour Track Jacket.',
				  'price' => '70.00',
				  'sizes' => array('M', 'L', '1X' )
			  ),

			'139' => array(
				  'name'  => 'KHAOS LEGIONS ALBUM COVER',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', '1X' )
			  ),
),  //end product type


	'misc' => array( 	

			'603' => array(
				  'name'  => 'Ring Button',
				  'price' => '3.00',
				  'sizes' => array('none')
			  ),

			'602' => array(
				  'name'  => 'Logo Button',
				  'price' => '3.00',
				  'sizes' => array('none')
			  )  

		  
  	)  //end product type
  
  );  //end skus


function makeProduct($type){

global $skus;


foreach ( $skus[$type] as $skunum => $value) {
$price = $skus[$type][$skunum]['price'];
$name = $skus[$type][$skunum]['name'];



echo '
<!-- PRODUCT BEGIN -->
					<li class="product" >
						<a href="images/'.$SKULOWER.$skunum.'lg.jpg" rel="lightbox"><img src="product_files/'.$SKULOWER.$skunum.'.png"></a><br>
                            <strong>'.$name.'</strong><br>'.$SKUCAPS.$skunum.'<br>$'.$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().
                            
                            '</SELECT>
                            <br><br>
                           <input class="css3button" value="Add To Cart" type="image">
                            </form>
                   	    </li>
               		<!-- Product End -->';
                    
}
}

                    ?>

 

any help would be greatly appreciated :)

Link to comment
Share on other sites

Do not use global in your function. Instead, pass $skus as an additional parameter to the function.

 

I don't see in your code where you are using the ['sizes'] array currently. I see a function call to makesizes(), but you aren't passing anything to that function so I don't know how it would even be working. You show what you want to have happen when there are no sizes, but not what you want to happen when there are sizes.

 

Plus, there are other problems with your code since you didn't follow the advice I gave you previously. You don't need to define all the versions of the band name since you can programmatically create the uppercase and lowercase versions. Besides, you don't pass the band name(s) to your function, so they will not have values in the function. So, you need to pass the band name to the function as a parameter as well. Same goes for the SKU.

 

Based on your prior thread I believe you wanted to make the sizes a drop down list? If so, you can create the makeSizes function accordingly. But, your current code has the beginning/ending SELECT tag hard-coded. That needs to be part of the makeSizes function also.

$BANDNAME = "Band Name"; 

function makeSizes($sizesArray)
{
    if(count($sizesArray)==0)
    {
        echo "<input value='{$SKUCAPS}{$skunum}' type='hidden' name='item'>";
    }
    else
    {
        echo "<select name='item'>\n";
        foreach($sizesArray as $size) { echo "<option value='{$size}'></option\n"; }
        echo "</select>\n";
    }
    return;
}

$skus = array(
    'shirts' => array( 
        '165' => array(
            'name'  => 'World Khaos Tour <br> Limited Edition Tour Track Jacket.',
            'price' => '70.00',
            'sizes' => array('M', 'L', '1X' )
        ),

        '139' => array(
            'name'  => 'KHAOS LEGIONS ALBUM COVER',
            'price' => '15.95',
            'sizes' => array('S', 'M', 'L', '1X' )
        ),
    ),  //end product type

    'misc' => array(
        '603' => array(
            'name'  => 'Ring Button',
            'price' => '3.00',
            'sizes' => array('none')
        ),
                
        '602' => array(
            'name'  => 'Logo Button',
            'price' => '3.00',
            'sizes' => array('none')
        )  
    )  //end product type
);  //end skus


function makeProduct($skus, $type, $BANDNAME, $SKU)
{
    $BANDCAPS  = strtoupper($BANDNAME);
    $BANDLOWER = strtolower($BANDNAME);
    $SKUCAPS   = strtoupper($SKU);
    $SKULOWER  = strtolower($SKU);

    foreach ( $skus[$type] as $skunum => $value)
    {
        $price = $skus[$type][$skunum]['price'];
        $name  = $skus[$type][$skunum]['name'];
        
        echo "<!-- PRODUCT BEGIN -->
              <li class='product' >
                 <a href='images/{$SKULOWER}{$skunum}lg.jpg' rel='lightbox'><img src='product_files/{$SKULOWER}{$skunum}.png'></a><br>
                 <strong>{$name}</strong><br>{$SKUCAPS}{$skunum}<br>\${$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>  
                 ".makeSizes()."                           
                 <br><br>
                 <input class='css3button' value='Add To Cart' type='image'>
                 </form>
             </li>
              <!-- Product End -->";
    }
}

Link to comment
Share on other sites

Thank you so much!! I'm sorry it seems like I wasn't listening to you, I was just having a hard time wrapping my head around it.

 

I have adjusted a few of the things to make it actually output what I was looking for but I'm still having one small issue.

 

The makeSizes function outputs prior to the makeProduct function resulting in this

 

 <strong>Size:</strong> 
<select name='item'>

<option value='AEN165M'>M</option>
<option value='AEN165L'>L</option>
<option value='AEN165X'>X</option>
<option value='AEN165XX'>XX</option>
<option value='AEN165XXX'>XXX</option>
<option value='AEN165XXXX'>XXXX</option>
</select>
<!-- PRODUCT BEGIN -->
              <li class='product' >
                 <a href='images/aen165lg.jpg' rel='lightbox'><img src='product_files/aen165.png'></a><br>

                 <strong>World Khaos Tour <br> Limited Edition Tour Track Jacket.</strong><br>AEN165<br>$70.00<br>
                 <form name='AEN165' method='GET' target='_blank' action='http://www.jsrdirect.com/cgi-bin/Make-a-Store.cgi'>
                 <input type='hidden' name='band' value='ARCHENEMY'> <input type='hidden' name='back' value='http://www.jsrdirect.com/bands/archenemy/index.html'>
                 <strong>Qty:</strong>
                 <input type='text' name='quantity' size='1' value='1' >

                 
                                            
                 <br><br>
                 <input class='css3button' value='Add To Cart' type='image'>
                 </form>
             </li>
              <!-- Product End -->

I used a return on the first part of the function and it fixed it, but what is the best way to fix it for the else portion of the function?

 

function makeSizes($sizesArray, $SKUCAPS, $skunum )
{
    if(count($sizesArray)==0)
    {
        return "<input value='{$SKUCAPS}{$skunum}' type='hidden' name='item'>\n";
    }
    else
    {
        echo "<strong>Size:</strong> \n <select name='item'>\n";
        foreach($sizesArray as $size) { echo "<option value='{$SKUCAPS}{$skunum}{$size}'>{$size}</option>\n"; }
        echo "</select>\n";
    }
    return;
};

 

<?php $BANDMETA="Arch Enemy"; 
$BANDNAME="ARCHENEMY";
$SKU="AEN";

function makeSizes($sizesArray, $SKUCAPS, $skunum )
{
    if(count($sizesArray)==0)
    {
        return "<input value='{$SKUCAPS}{$skunum}' type='hidden' name='item'>\n";
    }
    else
    {
        echo "<strong>Size:</strong> \n <select name='item'>\n";
        foreach($sizesArray as $size) { echo "<option value='{$SKUCAPS}{$skunum}{$size}'>{$size}</option>\n"; }
        echo "</select>\n";
    }
    return;
};


$skus = array(


'shirts' => array( 

			'165' => array(
				  'name'  => 'World Khaos Tour <br> Limited Edition Tour Track Jacket.',
				  'price' => '70.00',
				  'sizes' => array('M', 'L', 'X', 'XX', 'XXX', 'XXXX' )
			  ),

			'139' => array(
				  'name'  => 'KHAOS LEGIONS ALBUM COVER',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', 'X', 'XX', 'XXX', 'XXXX' )
			  ),
			  
			'137' => array(
				  'name'  => 'KHAOS LEGIONS',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', 'X', 'XX', 'XXX', 'XXXX')
			  ),
			  
			'135' => array(
				  'name'  => 'FUCK THE RULES',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', 'X', 'XX', 'XXX', 'XXXX')
			  )
							  
),  //end product type


'girls' => array( 

			'138' => array(
				  'name'  => 'REBEL GIRL',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', 'X', 'XX', 'XXX', 'XXXX')
			  ),
  
			'105' => array(
				  'name'  => 'RISE OF THE TYRANT WOMENS',
				  'price' => '15.95',
				  'sizes' => array('S', 'M', 'L', 'X', 'XX', 'XXX', 'XXXX')
			  )
										  
  	),  //end product type


	'misc' => array( 	

			'603' => array(
				  'name'  => 'Ring Button',
				  'price' => '3.00',
				  'sizes' => array()
			  ),

			'602' => array(
				  'name'  => 'Logo Button',
				  'price' => '3.00',
				  'sizes' => array()
			  )
		  
  	)  //end product type
  
  );  //end skus






function makeProduct($skus, $type, $BANDNAME, $SKU)
{
    $BANDCAPS  = strtoupper($BANDNAME);
    $BANDLOWER = strtolower($BANDNAME);
    $SKUCAPS   = strtoupper($SKU);
    $SKULOWER  = strtolower($SKU);

    foreach ( $skus[$type] as $skunum => $value)
    {
        $price = $skus[$type][$skunum]['price'];
        $name  = $skus[$type][$skunum]['name'];
	$sizesArray  = $skus[$type][$skunum]['sizes'];


        
        echo "<!-- PRODUCT BEGIN -->
              <li class='product' >
                 <a href='images/{$SKULOWER}{$skunum}lg.jpg' rel='lightbox'><img src='product_files/{$SKULOWER}{$skunum}.png'></a><br>
                 <strong>{$name}</strong><br>{$SKUCAPS}{$skunum}<br>\${$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' >
                 
                 ".makeSizes($sizesArray, $SKUCAPS, $skunum)."                           
                 <br><br>
                 <input class='css3button' value='Add To Cart' type='image'>
                 </form>
             </li>
              <!-- Product End -->";
    }
};
?>

Link to comment
Share on other sites

The makeSizes function outputs prior to the makeProduct function resulting in this

 

Ok, yeah, I see that now. Just create the output as a variable in the function and return the variable

function makeSizes($sizesArray, $SKUCAPS, $skunum )
{
    if(count($sizesArray)==0)
    {
        $returnVal = "<input value='{$SKUCAPS}{$skunum}' type='hidden' name='item'>\n";
    }
    else
    {
        $returnVal = "<strong>Size:</strong> \n <select name='item'>\n";
        foreach($sizesArray as $size)
        {
            $returnVal .= "<option value='{$SKUCAPS}{$skunum}{$size}'>{$size}</option>\n";
        }
        $returnVal .= "</select>\n";
    }

    return $returnVal;
}

Link to comment
Share on other sites

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.