for Schleife - letzter Wert kein "," anhängen

Thema wurde von Alex (living-zone.de), 25. April 2014 erstellt.

  1. Alex (living-zone.de)

    Alex (living-zone.de) Erfahrener Benutzer

    Registriert seit:
    24. Mai 2012
    Beiträge:
    60
    Danke erhalten:
    13
    Danke vergeben:
    20
    Hallo,

    ich probiere gerade Google Adwords Remarketing Tag zu implementieren. Dabei habe ich eine For- Schleife und lasse mir die Werte in eine Variable reinschreiben KOMMA getrennt.
    PHP:
    $t_product_id            '[';
    for(
    $i 0$i count($products); $i++){
     
    $product_id            '\''.(int)$products[$i]['id'].'\', ';
     
    $t_product_id     .= $product_id;
    }
    $t_product_id         .= ']';
    ausgegeben wird so:
    Ich möchte (oder besser gesagt google will) das beim letzten Wert kein "," mehr hinzugefügt wird:
    Wie mache ich das?
     
  2. Anonymous

    Anonymous Erfahrener Benutzer
    Mitarbeiter

    Registriert seit:
    22. Juni 2011
    Beiträge:
    4.760
    Danke erhalten:
    1.748
    Danke vergeben:
    137
    Moin,

    ich würde das so machen:

    PHP:
    function add_quotes($product) { return "'${product['id']}'"; }
    $t_quoted_products array_map('add_quotes'$products);
    $t_products_id_list '['.implode(','$t_quoted_products).']';
     
  3. Alex (living-zone.de)

    Alex (living-zone.de) Erfahrener Benutzer

    Registriert seit:
    24. Mai 2012
    Beiträge:
    60
    Danke erhalten:
    13
    Danke vergeben:
    20
    Danke Marco,

    ich bekomme es irgenwie nicht hin. Deswegen poste ich lieber komplette datei.
    Der betroffe Abschnitt ist von Zeilen
    if($t_page == '\'cart\'' || $t_page == '\'purchase\'') {.....}:

    PHP:
    <?php

    class GoogleRemarketingApplicationBottomExtender extends GoogleRemarketingApplicationBottomExtender_parent{


        var 
    $v_page false;

        function 
    init_page(){
            
    // BOF GM_MOD
            
    $t_script_name '';
            if(
    strpos($_SERVER['SCRIPT_NAME'], '.php') !== false && strpos($_SERVER['SCRIPT_NAME'], DIR_WS_CATALOG) !== false){
                
    $t_script_name $_SERVER['SCRIPT_NAME'];
            }elseif(
    strpos($_SERVER["PHP_SELF"], '.php') !== false && strpos($_SERVER['PHP_SELF'], DIR_WS_CATALOG) !== false){
                
    $t_script_name $_SERVER["PHP_SELF"];
            }elseif(
    strpos($_SERVER["SCRIPT_FILENAME"], '.php') !== false && strpos($_SERVER['SCRIPT_FILENAME'], DIR_WS_CATALOG) !== false){
                
    $t_script_name $_SERVER['SCRIPT_FILENAME'];
            }else{
                
    $t_script_name $PHP_SELF;
            }

            
    $t_page '';
            
            if(!empty(
    $this->v_data_array['GET']['cat']) || substr_count($_SERVER["QUERY_STRING"], 'cat=') > || substr_count($_SERVER["REQUEST_URI"], 'cat/') > || isset($this->v_data_array['GET']['filter_fv_id']) || isset($this->v_data_array['GET']['filter_price_min']) || isset($this->v_data_array['GET']['filter_price_max']) || isset($this->v_data_array['GET']['filter_id'])) $t_page '\'category\'';
            elseif(
    substr_count($t_script_name'product_info.php')         > 0$t_page '\'product\'';
            elseif(
    substr_count($t_script_name'shopping_cart.php')     > 0$t_page '\'cart\'';
            
            elseif(
    substr_count($t_script_name'checkout_')     > || substr_count($t_script_name'paypal_checkout')     > 0$t_page '\'purchase\'';
            elseif(
    substr_count($t_script_name'advanced_search_result.php')     > 0$t_page '\'searchresults\'';
            
            elseif(
    substr_count(strtolower($t_script_name), 'index.php') > 0){$t_page '\'home\'';}
            else{
    $t_page '\'other\'';}
            
            
    $this->v_page $t_page;
        }


        function 
    get_page(){
            return 
    $this->v_page;
        }
        

                        
        function 
    init_js(){
            global 
    $xtPrice;
            if(
    gm_get_conf('GM_SHOP_OFFLINE') != 'checked' || $_SESSION['customers_status']['customers_status_id'] == 0) {
                
    $t_get_data_array = array();
                
    $t_page $this->get_page();

                if(
    $t_page == '\'product\'') {
                    
    $product_id            $this->v_data_array['products_id'];
                    
    $product_query    xtc_db_query("select products_id, products_price, products_tax_class_id from " TABLE_PRODUCTS " where products_id='" $product_id "'");
                    
    $product                xtc_db_fetch_array($product_query);
                    
    $products_price    $xtPrice->xtcGetPrice($product['products_id'], ''''$product['products_tax_class_id'], $product['products_price'], '');
                    
                    
    $t_product_id            .= '\''.$product_id.'\', ';
                    
    $t_product_price    .= '\''.$products_price.'\', ';
                }
                
                if(
    $t_page == '\'cart\'' || $t_page == '\'purchase\'') {
                    
                    if (
    $_SESSION['cart']->count_contents() > 1) {
                        
    $t_product_id            '[';
                        for(
    $i 0$i count($products); $i++){
                            
    $product_id            '\''.(int)$products[$i]['id'].'\', ';
                            
    $t_product_id     .= $product_id;
                        }
                        
    $t_product_id         .= ']';
                    }else{
                        for(
    $i 0$i count($products); $i++){
                            
    $product_id            '\''.(int)$products[$i]['id'].'\', ';
                        }
                    }

                    
    $total_content          $xtPrice->xtcFormat($_SESSION['cart']->show_total(),false);
                    
    $t_product_price     '\''.$total_content.'\'';
                }
                
                if(
    $t_page == '\'home\'' || $t_page == '\'category\'' || $t_page == '\'searchresults\'' || $t_page == '\'other\'') {
                    
    $t_product_id     '\'\'';
                    
    $t_product_price     '\'\'';
                }
                
                if(!empty(
    $t_page)){$t_get_data_array[] = $t_page;}
                
    $this->v_output_buffer['GM_JAVASCRIPT_CODE'] = '    
    <script type="text/javascript">
    var google_tag_params = {
    ecomm_prodid: '
    .$t_product_id.',
    ecomm_pagetype: '
    .$t_page.',
    ecomm_totalvalue: '
    .$t_product_price.'
    };
    </script>
    <script type="text/javascript">
    /* <![CDATA[ */
    var google_conversion_id = XXXXXXXXXX;
    var google_custom_params = window.google_tag_params;
    var google_remarketing_only = true;
    /* ]]> */
    </script>
    <script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
    </script>
    <noscript>
    <div style="display:inline;">
    <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/XXXXXXXXXX/?value=0&amp;guid=ON&amp;script=0"/>
    </div>
    </noscript>
    '
    ;
            }
        }
        
        
        function 
    proceed(){
            
    $t_page $this->get_page();
            if(
    $t_page === falsetrigger_error('need call of init_page() method before proceed'E_USER_ERROR);
            
            
    parent::proceed();
        }
    }
    ?>
     
  4. Moritz (Gambio)

    Moritz (Gambio) Administrator

    Registriert seit:
    26. April 2011
    Beiträge:
    5.786
    Danke erhalten:
    2.692
    Danke vergeben:
    903
    Alternativlösung:

    PHP:
    $t_product_id_array = array();
    $t_product_id            "['";
    for(
    $i 0$i count($products); $i++){
        
    $t_product_id_array[] = (int)$products[$i]['id'];
    }
    $t_product_id .= implode("', '"$t_product_id_array);
    $t_product_id .= "']";
     
  5. Alex (living-zone.de)

    Alex (living-zone.de) Erfahrener Benutzer

    Registriert seit:
    24. Mai 2012
    Beiträge:
    60
    Danke erhalten:
    13
    Danke vergeben:
    20
    Danke Moritz.

    Jetzt passt.