SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    merge array question

    PHP Code:
    switch ($this->table)
        {
            case 
    'BS_category_description'//retrieves category ids based on category name ($src_term) taken from form
                
    $this->sql='SELECT `category_id` FROM `' $this->table '` WHERE `name`="' $src_term '"';
                
    $res mysqli_query($this->conn,$this->sql);
                if(
    $res->num_rows == 0)
                    {
                        echo 
    'No results found, please check your search terms';
                        exit();
                    }
                while(
    $cRow mysqli_fetch_array($res)) // retrieves product ids that use the category id retrieved above
                    
    {
                        
    $this->sqlProd='SELECT `product_id` FROM `BS_product_to_category` WHERE `category_id`=' $cRow[0];
                        
    $resProd mysqli_query($this->conn,$this->sqlProd);
                        while(
    $prodRow mysqli_fetch_array($resProd))
                            {
                                
    $this->id_arr array_merge ($prodRow$this->id_arr); 
                            }
                    }
            
    $this->id_arr=array_unique($this->id_arr);
            
    asort ($this->id_arr);
            } 
    What the above code is intended to do is take a category id and get the product ids that use that category id and put that information into an array $this->id_arr.

    Here is the printed out $id_arr:

    Array ( [8] => 204 [7] => 205 [6] => 206 [5] => 207 [4] => 208 [3] => 209 [product_id] => 223 [1] => 224 [0] => 225 )

    I use the following code to retrieve information from the database based on those ids.

    PHP Code:
    $i count ($this->id_arr);
    for (
    $a=0$a $i; ++$a)
        {
            
    $output .= '<tr>';
            
    $this->sql='SELECT model, sku, quantity, image, shipping, price, weight, length, width, height FROM BS_product WHERE product_id='$this->id_arr[$a];
            echo 
    $this->sql '<br />';
            
    $res mysqli_query($this->conn$this->sql);
            while(
    $this->sqlOut mysqli_fetch_array($res))
                {
                    echo 
    $this->sqlOut['model'] . '<br />';
                }
            
    $output .= '</tr>';
        } 
    The problem arises when it reaches the the key that should be a 2, but says product id. Anyone know why product_id is being put in for the key instead of a 2?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See your array is like this:
    Code:
    Array ( 
    	[8] => 204 
    	[7] => 205 
    	[6] => 206 
    	[5] => 207 
    	[4] => 208 
    	[3] => 209 
    	[product_id] => 223 
    	[1] => 224 
    	[0] => 225 
    	)
    So it is there product_id instead of 2.

    So the better solution would be to loop like this:
    Code php:
    foreach($this->id_arr as $id){
    	$this->sql='SELECT model, sku, quantity, image, shipping, price, weight, length, width, height FROM BS_product WHERE product_id='. $id;
    	.
    	.
    	.
    	.....
    }

    Or you can directly query in this way:
    PHP Code:
    $this->sql='SELECT model, sku, quantity, image, shipping, price, weight, length, width, height FROM BS_product WHERE product_id IN('implode(",",$this->id_arr) . ')'
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet, dude... thank you very much...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •