Jump to content

mallen

Members
  • Content Count

    316
  • Joined

  • Last visited

Community Reputation

1 Neutral

About mallen

  • Rank
    Advanced Member

Profile Information

  • Gender
    Not Telling
  1. mallen

    Set post value of checkbox to empty

    INSERT INTO products (`prod_name`, `prod_model`, `feature`) VALUES('$name', '$model','$feature')
  2. mallen

    Set post value of checkbox to empty

    Thanks. I guess I was assuming I could have the data blank instead of NULL. If its blank in the database, I can check it and it becomes 1. If I save, go back and edit it, resubmit it becomes "0". <input type="checkbox" name="feature" value="1" <?php if($editProduct && $vals['feature'] == 1) echo "checked"; ?> /> I do not have it set for NULL. I have TINYINT, default, none
  3. Is it possible to post an empty value for a check box? Currently the table is empty. After I have checked it and when posted a value of "1". But what I want to do it when I uncheck and re-post have it blank again and not "0" as the value. Is this possible?
  4. mallen

    Getting unique items in array

    I made a couple changed and added array_shift and now it is showing 3 recent products and it changes. function definerecentview() {//get the last 3 products viewed global $wpdb; //Set the current product ID $currentProduct = intval($_GET['singleProduct']);//gets product id from URL that was viewed add_action('init', 'definerecentview'); //Set default of viewed products to empty array $viewed_products = array(); //If there is a saved cookie value - use it if(isset($_COOKIE['recently_viewed'])) { //$viewed_products = unserialize($_COOKIE['recently_viewed']); $viewed_products = (array) explode( ',', $_COOKIE['recently_viewed']); } //If the current product exists in the array remove it if(in_array($currentProduct, $viewed_products)) { unset($viewed_products[array_search($currentProduct, $viewed_products)]); } //Add the current product to the end of the array $viewed_products[] = $currentProduct; //Ensure length is no more than 3 items //$viewed_products = array_slice($viewed_products, 0, 3); if (sizeof( $viewed_products ) > 4 ) {//changed and added here array_shift( $viewed_products ); } // Store cookie //setcookie( 'recently_viewed', serialize($viewed_products), time()+(86400 * 30), '/' ); setcookie( 'recently_viewed',implode( ',', $viewed_products ), time()+(86400 * 30), '/' );//changed here //Run query to get records for recently viewed products $listOfViewed = implode( ',',$viewed_products); $query = "SELECT `prod_id` , `prod_model`, `prod_mainImage`, `prod_name` FROM products WHERE `prod_id` IN ($listOfViewed) AND `prod_id` != '{$currentProduct}' ";//don't show current item $recentProducts = $wpdb->get_results($query, ARRAY_A); //Create output for list of recent products $output = ""; foreach($recentProducts as $prod) { $imgSrc = PRODUCT_IMAGE . "/thumbnails/{$prod['prod_mainImage']}'"; $output .= "<a href='#'><div class='recent grid-area'>"; $output .= "<div class='recentimg grid-area'>"; $output .= "<img src='{$imgSrc}' alt='{$prod['prod_model']}'title='{$prod['prod_id']}' /></div>"; $output .= "<div class='recentmodel grid-area '>{$prod['prod_name']}</div>"; $output .= "<div class='recentnumber grid-area '>{$prod['prod_model']}</div>"; $output .= "</div></a>"; } //Output the content echo "<div class='aroundrecent grid-area'>Recently Viewed"; echo " <div class='columnflex grid-area'>{$output}</div>"; echo "</div>"; }
  5. mallen

    Getting unique items in array

    Ignore the previous reply. It is working but just the list of products is not changing once I view a fourth product.
  6. mallen

    Getting unique items in array

    if(in_array($currentProduct, $viewed_products)) Warning: in_array() expects parameter 2 to be array, boolean I changed it to this and I don't get duplicates but the items don't change after I go to the fourth item. I want to show the last three items. $viewed_products = (array) explode( ',', $_COOKIE['recently_viewed']);
  7. mallen

    Getting unique items in array

    I have printed the array and see I have the correct IDs I need to show. But the output always showed one duplicated product . But in the printed array it is correct. function definerecentview() {//get the last 3 products viewed global $wpdb; $theProduct = $_GET['singleProduct'];//gets product id from URL that was viewed add_action('init', 'definerecentview'); if ( empty($_COOKIE['recently_viewed'])) $viewed_products = array(); else $viewed_products = (array) explode( ',', $_COOKIE['recently_viewed']); if ( ! in_array($theProduct, $viewed_products)) { $viewed_products[] = $theProduct; } if (sizeof( $viewed_products ) > 3) { array_shift( $viewed_products ); } // Store cookie setcookie( 'recently_viewed', implode( ',', $viewed_products ),time() + (86400 * 30), '/' ); $output = ""; $listOfViewed = implode( ',',$viewed_products); $query = "SELECT `prod_id` , `prod_model`, `prod_mainImage`, `prod_name` FROM products WHERE `prod_id` IN ($listOfViewed) AND `prod_id` != '". $this->curProduct ."' ";//don't show current item $group = $wpdb->get_results($query, ARRAY_A); echo "<div class='aroundrecent grid-area'>Recently Viewed"; echo "<div class='columnflex grid-area'>"; foreach($group as $vw){ $output .= "<a href='#'><div class='recent grid-area'>"; $output .= "<div class='recentimg grid-area'><img src='" .PRODUCT_IMAGE .'/thumbnails/'.$vw['prod_mainImage']. "' alt='".$vw['prod_model']."'title='".$vw['prod_id']."' /></div>"; $output .= "<div class='recentmodel grid-area '>".$vw['prod_name']."</div>"; $output .= "<div class='recentnumber grid-area '>".$vw['prod_model']."</div>"; $output .= "</div></a>"; echo $output; } echo "</div>"; echo "</div>"; echo "</div>"; print_r($group);/*Array ( [0] => Array ( [prod_id] => 2164 [prod_model] => XYZ [prod_mainImage] => RGTE.png [prod_name] => Widget 33 ) [1] => Array ( [prod_id] => 2478 [prod_model] => ABC [prod_mainImage] => GHTR.png [prod_name] => Widget 45 ) ) //*/ }
  8. mallen

    Getting unique items in array

    Thanks. I will check out the variable name and try again. I appreciate you looking over to find anything I missed.
  9. mallen

    Getting unique items in array

    And if I try this I get ony one item and no duplicates shown. foreach (array_unique($group) as $vw) {
  10. I am creating a function to set a cookie of the last three viewed items and then display the items except the item I am on. The issue I am having is after the second product I view, it will show duplicate items. I outputted the query and there are no duplicates. I tried array_unique. function definerecentview() { global $wpdb; $myvaluee = $_GET['singleProduct']; add_action('init', 'definerecentview'); if ( empty($_COOKIE['recently_viewed'])) $viewed_products = array(); else $viewed_products = (array) explode( ',', $_COOKIE['recently_viewed']); if ( ! in_array($myvalue, $viewed_products)) { $viewed_products[] = $myvaluee; } if (sizeof( $viewed_products ) > 4 ) { array_shift( $viewed_products ); } // Store cookie setcookie( 'recently_viewed', implode( ',', $viewed_products ),time() + (86400 * 30), '/' ); $output = ""; $viewed = implode( ',',$viewed_products); $unique = implode(',', array_unique(explode(',', $viewed)));//added $query = "SELECT * FROM products WHERE `prod_id` IN ($unique) AND `prod_id` != '". $this->curProduct ."' LIMIT 4";//don't show current item $group = $wpdb->get_results($query, ARRAY_A); echo "<div class='aroundrecent grid-area'>Recently Viewed"; echo "<div class='columnflex grid-area'>"; foreach($group as $vw){ $output .= "<a href='#'><div class='recent grid-area'>"; $output .= "<div class='recentimg grid-area'><img src='/thumbnails/'.$vw['image']. "' alt='".$vw['prod_model']."'title='".$vw['prod_id']."' /></div>"; $output .= "<div class='recentmodel grid-area '>".$vw['prod_name']."</div>"; $output .= "<div class='recentnumber grid-area '>".$vw['prod_model']."</div>"; $output .= "</div></a>"; echo $output; } echo "</div>"; echo "</div>"; echo "</div>"; var_dump($group); //I will get this SELECT * FROM products WHERE `prod_id` IN (2619,2612,2272) AND `prod_id` != '2272' LIMIT 4 And it will show product 2619 twice. }?>
  11. mallen

    Storing array values from form

    I think I finally got it working. Thanks again for helping me through this. I have learned more about arrays than I ever have. <input type="hidden" name="ords[]" <?php if($editProduct) echo "value='{$cat['p_order']}'"; ?>/> --------------------------------------------- $ords = $_REQUEST['ords']; $catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES('$lastID', '". $cats[$i] . "','". $ords[$i] . "')"; --------------------------------------------- I think the quote marks were an issue. Also these two parts. $ords[$i] name="ords[]"
  12. mallen

    Storing array values from form

    Thanks Sepodati. I'm not relating a text box to a check box. I am not changing any order of the products. Each category already has product and its order associated. The pair is not changing. I am just creating a new line with the same pairs. cat_assoc_id | prod_id | cat_id | p_order ------------------------------------------- 16101 195 3 6 16102 195 8 3 ------------------------------------------- So the next one would be ------------------------------------------- 16103 195 3 6 16104 195 8 3 ------------------------------------------- So Item 195 is in category 3 and at position 6 and so on. When the new entry is made all the relation ships in this table remain. 3>>>>>6 8>>>>>3 I worked it it some and now I am getting some results posted in the p_order column. Just not accurate. Sometimes I get "0" sometimes it duplicates the same number. I tried name=ords[] also. $combined = array_replace($cats,$orders); if(count($combined)>0) { foreach($combined as $cat) { ?> <tr> <td><?php echo $cat['cat_name'];/</td> <td><input type='checkbox' name='prodCategories[]' value='<?php echo $cat['cat_id']; ?>' <?php if($editProduct) echo $prod->isCheckSelected($cat['cat_id'], $vals['prod_id']);?>/> <input type="hidden" name="ords" value="<?php echo $cat['p_order'];?>" /> ----------------------------------------------------------------------- $ords = $_REQUEST['ords']; for($i= 0; $i < count($cats); $i++) { $catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES('$lastID', '". $cats[$i] . "','$ords')";
  13. mallen

    Storing array values from form

    Thanks again for all your help. I revised my approach. I was able to combine my two arrays of the category and the order each product appears in each category. Now I am working on the insert. Still losing the value of "p_order" Array data ---------------------------------------------------------- 0 => 'prod_id' => string '184' 'cat_id' => string '3' 'p_order' => string '5' 1 => 'prod_id' => string '174' 'cat_id' => string '9' 'p_order' => string '21' Form ------------------------------------------------------ foreach($combined as $together) { ?> <tr> <td><?php echo $together['cat_name'];?></td> <td><input type='checkbox' name='together[]' value='<?php echo $together['cat_id']; ?>' <?php if($editProduct) echo $prod->isCheckSelected($together['cat_id'], $vals['prod_id']);?>/> </td> </tr> Product class ------------------------------------------------------ $together = $_REQUEST['together']; for($i= 0; $i < count($together); $i++) { $catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES('$lastID', '". $together[$i]."','". $together['p_order'] . "')"; $wpdb->query($catCategory);
  14. mallen

    Storing array values from form

    Correct. But I am not changing the order or the category association. Its large page with lots of files, descriptions, model numbers etc. So if a category has 50 products and a custom order I want to retain that. maybe I eed to merge the two? prodCategories[] orderp[]
  15. mallen

    Storing array values from form

    The order of the products is done elsewhere. I am loosing the relation of the product's order in realtion to its category. When I edit names, descriptions ect. it creates a new assoc_id and enters the data below. That is where I loose the saved data that was there. The categories the product appears in holds ok. Just looses that "p_order". So if product 1 appears in cat 8, 3, and 6 the table would show: 842, 8, 45 843, 3,10 844, 6, 66 but I loose it and it looks like this: 845,8, 0 846,3,0 847,6, 0 $catCategory = "INSERT INTO category_assoc (prod_id, cat_id, p_order) VALUES('$lastID',' ". $cats[$i] . "','". $order[$i] . "')";
×

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.