Forum Moderators: coopster
Basically, I've created a script where someone will put in a value for how many input fields they need to have for a form. So if the end user inputs "3" on this form, and submits it, then a new form will populate 3 fields. These fields are written to the database with a key of "field1", "field2" and "field3", and have corresponding input fields of their own that will hold the values for each field.
My issue is pulling the data from the database and doing stuff with it. I need to write a function that will 1) pull the data from the database in the key=>value setup - however I will not know how many key=>value pairs there will be (they auto-increment based on the value in the initial form), and 2) count how many items are in each $value of the pairs so I can manipulate the data.
when I do the following:
if($attcount != '') { // $att count is the "3" entered in the initial field, so 3 fields will be populated
// grab each attribute that's set - if there are any
for($i=1; $i<=$attcount; $i++) {
$names .= 'field' . $i . '_value';
}
// to test
print_r($names);
Now, it *is* giving me the field names correctly - it's listing field1_value, field2_value, field3_value...BUT it's doing it three times. So instead of:
field1_valuefield2_valuefield3_value
it's giving me
field1_valuefield2_valuefield3_valuefield1_valuefield2_valuefield3_valuefield1_valuefield2_valuefield3_value
I can't seem to get the listing to populate just *once* - It seems to loop through the 3, and then do it a totla of three times. I just want it to loop through the three values and *stop*.
Can anyone tell me what I'm doing wrong here?
Yep = it certainly does. Every time I test it, it always echoes out the correct value: 3 or 4 or 5 - whatever I enter in the form, $attcount is definitely reflecting.
Ah - maybe I will try that with the $names thing and see if that helps - thanks! I'll report back in a moment :)
I've looked in the code that appears before it as well, and this is a separate function from everything else - so I don't see why it would be pulling something in like this. This is pretty much the entire function (and yes, I'm using WordPress):
function product_attributes() {
global $post, $dropdowns, $attcount;
$priceonly = pricecheck('priceonly');
$title = get_the_title();
// these two are what I'm replacing - they used to be static values, but now they need to be dynamic.
// These following two values will no longer be used, but I have them here for reference
//$sizes = get_post_meta($post->ID, 'sizes_value', true);
//$colors = get_post_meta($post->ID, 'colors_value', true);
// first, we need to see how many attribute areas there are, and get the values
// start by checking to see if $attcount has a setting
if($attcount != '') {
// grab each attribute that's set - if there are any
$attnames = '';
for($i=1; $i<=$attcount; $i++) {
$attnames .= 'attribute' . $i . '_value';
}
// when I return it, it's looping too many times
echo $attnames;
}
}
If you're not familiar with WordPress, the $post global just pulls in information for the current post your are on. $Dropdowns is a simple "yes,no" setting that doesn't apply here (yet - it will though, after I get this figured out) and $attcount is the number that is applied that will tell *this* function how many times to iterate the fields.
I have the $attcount working in another script that writes the fields to the database, and uses "attribute1_value" as the meta_keys (with "1" going up or down based on the number in $attcount) and provides a blank spot to post data (for the meta_values). My issue now is using this function to *retrieve* that data from the database and manipulate how I want the data used. I can get the data - the problem is, because of this loop, I'm getting it too many times. I just can't see why.
So I'm *not* crazy - it looks like it should do what I *want* it to - yet for some reason it's doing...what it's doing.
I'm going to start taking some stuff out of the file and see if it has any effect. Maybe there's something else that, for some reason, is conflicting with this Loop. I don't see how, though - I'm using unique names (and even if I weren't this is contained within its own function).
I know this is a lot, but I'll put the file here too so if you all see anything that might be causing this, do you think you could nudge me?
<?php
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); }
/*----------------------------------------------------------------------------------------
Create reusable variables
----------------------------------------------------------------------------------------*/
$cakeArray = get_option('CakeShopPluginAdminOptions');
$username = $cakeArray['username'];
$currency = $cakeArray['currency'];
$showButton = $cakeArray['show_button'];
$showPrice = $cakeArray['show_price'];
$malsURL = $cakeArray['url'];
$message = $cakeArray['message'];
$imgorder = $cakeArray['image'];
$titleorder = $cakeArray['title'];
$modelorder = $cakeArray['model'];
$priceorder = $cakeArray['price'];
$attributesorder = $cakeArray['attributes'];
$attimgorder = $cakeArray['attimg_order'];
$attcount = $cakeArray['attribute_count'];
$dropdowns = $cakeArray['attribute_dropdowns'];
$columns = $cakeArray['attribute_columns'];
$stockorder = $cakeArray['stock'];
$descriptionorder = $cakeArray['description'];
$buttonorder = $cakeArray['button_order'];
$css = $cakeArray['buttons'];
/*---------------------------------------------------------------------------------------
Necessary Functions for Shopping cart
---------------------------------------------------------------------------------------*/
// create a function to pull the image
function product_image($type) {
global $post;
$image = get_post_meta($post->ID, 'image_value', true);
if($image != '') {
$bigimage = '<img class="product_image" src="' . $image . '" alt="' . $title . '" />' . "\n";
$smallimage = '<img class="product_image thumbnail" src="' . $image . '" alt="' . $title . '" />' . "\n";
}
if($type == 'regular') return $bigimage;
if($type == 'thumbnail') return $smallimage;
}
//get the post title - this one's pretty much cake.
function product_title($type) {
global $post;
$posttitle = '<h2 class="product_title">' . get_the_title() . '</h2>' . "\n";
$listingtitle = '<h2 class="product_title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h2>' . "\n";
if($type == 'single') return $posttitle;
if($type == 'listing') return $listingtitle;
}
// function for the catalog number
function catalog_number() {
global $post;
$catalog = get_post_meta($post->ID, 'catalog_value', true);
if($catalog != '') {
$catalogstuff = '<small class="catalogID">Catalog ID:' . $catalog . '</small>' . "\n";
}
return $catalogstuff;
}
// create a function to pull the correct price
function pricecheck($type) {
global $post, $currency, $showButton, $showPrice;
$price = get_post_meta($post->ID, 'price_value', true);
$sale = get_post_meta($post->ID, 'sale_value', true);
// first, let's deal with actual prices and sales.
if($price != '') {
if($sale != '') {
if(stristr($sale,'%')) { // get the percentage off
$getnum = str_replace('%','',$sale);
$percent = round($price - ($price * ($getnum/100)),2);
$pricetag = '<h3 class="strike"><s>' . $currency . $price . '</s> Now ' . $_currency . number_format($percent, 2, '.', '') . '</h3>' . "\n" . '<span class="alert">' . $sale . ' off!</span>' . "\n\n";
$hidetag = '<input type="hidden" name="price" value="' . $percent . '">';
$priceonly = number_format($percent, 2, '.', '');
} else { // get the dollar off amount
$getnum = str_replace($currency,'',$sale);
$dollar = round($price - $getnum,2);
$pricetag = '<h3 class="strike"><s>' . $currency . $price . '</s> Now ' . $currency . number_format($dollar, 2, '.', '') . '</h3>' . "\n" . '<span class="alert">' . $sale . ' off!</span>' . "\n\n";
$hidetag = '<input type="hidden" name="price" value="' . $dollar . '">';
$priceonly = number_format($dollar, 2, '.', '');
}
} else { // if it's not on sale, and it's not a volume discount, then just display the price
$pricetag = '<h3 class="product_currency">' . $currency . $price . '</h3>' . "\n";
$hidetag = '<input type="hidden" name="price" value="' . $price . '">';
$priceonly = round($price, 2);
}
if($type == 'priceonly') return $priceonly;
if($type == 'pricetag') return $pricetag;
if($type == 'hidetag') return $hidetag;
if($type == 'discount') return $discountprice;
}
}
// hard stuff - a function to create attributes. These were formerly dropdowns - but they were unpopular as such. I'm now making them into input boxes so multiple choices can be added with one click, and the attributes are prettier. Dropdowns are still available.
function product_attributes() {
global $post, $dropdowns, $attcount;
$priceonly = pricecheck('priceonly');
$title = get_the_title();
$sizes = get_post_meta($post->ID, 'sizes_value', true);
$colors = get_post_meta($post->ID, 'colors_value', true);
//first, we need to see how many attribute areas there are, and get the values
if($attcount != '') {
// grab each attribute that's set - if there are any
$attnames = '';
for($i=1; $i<=$attcount; $i++) {
$attnames .= 'attribute' . $i . '_value';
}
// test
echo $attnames;
}
}
// function for attribute images
function attribute_images() {
// this will be redone, since now they are integrated with the attributes above
}
// create a function to check stock levels
function stocklevel_check() {
global $post, $message;
$stock = get_post_meta($post->ID, 'quantity_value', true);
$inperson = get_post_meta($post->ID, 'cannotbuy_value', true);
if($inperson != '') {
$stockmessage = 'We\'re sorry, but this item cannot be purchased online. ';
} else {
$stockmessage = 'Out of Stock. ';
}
if($stock != '') {
if($stock <= '0' ¦¦ $inperson != '') {
$stocklevel = '<span class="out-of-stock">' . $stockmessage . $message . '</span>' . "\n";
} else if($stock <= '5' && $stock > '0' ) {
$stocklevel = '<span class="low-stock">Only '. $stock . ' of these items are left in stock. Grab one before they\'re gone!</span>';
}
}
return $stocklevel;
}
// function to get the post content - again, pretty much cake since it's a copy of the_content
function product_description() {
global $post;
$content = get_the_content($more_link_text, $stripteaser, $more_file);
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
$thedescription = '<div class="product_description">' . $content . '</div>' . "\n";
return $thedescription;
}
//function to create the buttons
function shopbuttons($type) {
global $post, $username, $malsURL, $showButton, $showPrice, $attributesorder, $dropdowns, $buttonorder, $css;
$stock = get_post_meta($post->ID, 'quantity_value', true);
$inperson = get_post_meta($post->ID, 'cannotbuy_value', true);
$returnURL = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$priceonly = pricecheck('priceonly');
$attributes = product_attributes();
$shopURL = $malsURL . '/cf/add.cfm';
$multiURL = $malsURL . '/cf/addmulti.cfm';
$viewURL = $malsURL . '/cf/review.cfm';
if($stock <= '0' ¦¦ $inperson != '') {
$active = '-disabled';
$buybutton = ' disabled';
} else {
$active = '';
$buybutton = '';
}
// dropdowns and attributes or not?
if($attributes == '') { //no attributes
$url = $shopURL;
$drop = '';
} else if($attributes != '') {
if($dropdowns == 'true') { // both attributes and dropdowns are true
$url = $shopURL; //multiple
$drop = '[]'; // multiple
} else { //attributes, but no dropdowns
$url = $multiURL; //multiple
$drop = ''; // multiple
}
}
// CSS buttons or Images?
if($css == "true") {
$buttontype = 'submit';
$buttonsrc = '';
$buttonview = '';
} else {
$buttontype = 'image';
$buttonsrc = ' src="' . get_bloginfo('template_directory') . '/images/buy-now' . $active . '.gif"';
$buttonview = ' src="' . get_bloginfo('template_directory') . '/images/view-cart.gif"';
}
$postID = get_the_ID();
// view cart button
$viewcart .= '<form id="view_cart" method="post" action="' . $viewURL . '">' . "\n";
$viewcart .= '<input type="hidden" name="userid" value="' . $username . '" />' . "\n";
$viewcart .= '<input type="hidden" name="return" value="' . $returnURL . '" />' . "\n";
$viewcart .= '<input type="' . $buttontype . '" class="button"' . $buttonview . ' value="View Cart" />' . "\n";
$viewcart .= '</form>' . "\n\n";
$buttons .= '<div class="buttons">' . "\n";
if($type == 'full') {
// if there are no attributes, just display the price
if($attributes == '') { // if no attributes...
$buttons .= '<form id="buy_now" method="post" action="' . $url . '">' . "\n";
$buttons .= '<input type="hidden" name="userid" value="' . $username . '" />' . "\n";
$buttons .= '<input type="hidden" name="return" value="' . $returnURL . '" />' . "\n";
$buttons .= '<input type="hidden" name="scode" value="' . $postID . '" />' . "\n";
$buttons .= '<input type="hidden" name="price" value="' . $priceonly . '" />' . "\n";
$buttons .= '<input type="hidden" name="product' . $drop . '" value="' . get_the_title() . '" />' . "\n";
$buttons .= '<input type="' . $buttontype . '" class="button"' . $buttonsrc . ' value="BUY NOW!"' . $buybutton . ' />' . "\n";
$buttons .= '</form>' . "\n\n";
$buttons .= $viewcart;
} else {
// if settings have attributes to appear before the buttons...
if($attributesorder < $buttonorder) {
$buttons .= '<form id="buy_now" method="post" action="' . $url . '">' . "\n";
$buttons .= '<input type="hidden" name="userid" value="' . $username . '" />' . "\n";
$buttons .= '<input type="hidden" name="return" value="' . $returnURL . '" />' . "\n";
$buttons .= '<input type="hidden" name="scode" value="' . $postID . '" />' . "\n";
$buttons .= '<input type="hidden" name="product' . $drop . '" value="' . get_the_title() . '" />' . "\n\n";
$buttons .= $attributes;
$buttons .= '<input type="' . $buttontype . '" class="button"' . $buttonsrc . ' value="BUY NOW!"' . $buybutton . ' />' . "\n";
$buttons .= '</form>' . "\n\n";
$buttons .= $viewcart;
} else { //if the buttons come first...
$buttons .= $viewcart;
$buttons .= '<form id="buy_now" method="post" action="' . $url . '">' . "\n";
$buttons .= '<input type="hidden" name="userid" value="' . $username . '" />' . "\n";
$buttons .= '<input type="hidden" name="return" value="' . $returnURL . '" />' . "\n";
$buttons .= '<input type="hidden" name="scode" value="' . $postID . '" />' . "\n";
$buttons .= '<input type="hidden" name="product' . $drop . '" value="' . get_the_title() . '" />' . "\n\n";
$buttons .= '<input type="' . $buttontype . '" class="button"' . $buttonsrc . ' value="BUY NOW!"' . $buybutton . ' />' . "\n";
$buttons .= $attributes;
$buttons .= '</form>' ."\n\n";
}
}
} else if($type == 'summary') {
if($showButton == 'true' && $attributes == '') {
$buttons .= '<form id="view_cart" method="post" action="' . $url . '">' . "\n";
$buttons .= '<input type="hidden" name="userid" value="' . $username . '" />' . "\n";
$buttons .= '<input type="hidden" name="return" value="' . $returnURL . '" />' . "\n";
$buttons .= '<input type="hidden" name="scode" value="' . $postID . '" />' . "\n";
$buttons .= '<input type="hidden" name="price" value="' . $priceonly . '" />' . "\n";
$buttons .= '<input type="hidden" name="product' . $drop . '" value="' . get_the_title() . '" />' . "\n";
$buttons .= '<input type="' . $buttontype . '" class="button"' . $buttonsrc . ' value="BUY NOW!"' . $buybutton . ' />' . "\n";
$buttons .= '</form>' . "\n";
} else {
$buttons .= '<a class="csb_more" href="' . get_permalink($post->ID) . '">more info »</a>' . "\n";
}
}
$buttons .= '<!--/buttons-->' . "\n" . '</div>' . "\n\n";
return $buttons;
}