Welcome Guest, Not a member yet? Register   Sign In
help with templates
#1

[eluser]zyklon[/eluser]
Hi
Can someone help me with sending the array to templates?I am having trobles with this problem,as i have multiple mysql queries which depend one on another and there are like 3 queries.If someone can please contact me on private message to help me as i don't think that if i put the code here the peoples will understand,and is better to be able to explain.I would need this help asap if possible.
I put the code here for one of the problems here,maybe someone can figure it out.
Template:

{specification_categories}
<div class="specs">
<div class="blue_11_bold"><span class="specs_title">{specification_category_name}</span></div>
{subspecifications}
<div class="specs_gray">
<div class="specs_left">{subspecification_name}</div>
<div class="specs_right">{subspecification_value}</div>
</div>
{/subspecifications}
</div>
{/specification_categories}

And the script:
$data["specification_categories"] = array();

foreach($result as $key => $value)
{

$product_specification_value = $value["product_specification_value"];




$result_category_spec = $this->lugnuts->get_products_category_specification_id($tableName,$id_product);
foreach($result_category_spec as $key_2 => $value_2)
{
$product_specification_category_id = $value_2["product_specification_category_id"];
$result_spec_id = $this->lugnuts->get_products_specification_id($tableName,$id_product,$product_specification_category_id);
foreach($result_spec_id as $key_4 => $value_4)
{
$result_spec_id_value = $value_4["product_specification_id"];

$specification_category_name_res = $this->lugnuts->get_category_specification_name($tableNameproducts_specifications_categories,$product_specification_category_id);
$specification_category_name = $specification_category_name_res->product_specification_category_name;
$data["specification_category_name"] = $specification_category_name;
$subspecification_name_result = $this->lugnuts->get_category_subspecification_name($tableNameproducts_specifications_subspecifications,$result_spec_id_value);
$subspecification_name = $subspecification_name_result->product_specification;

$data["specification_categories"][] = array_merge((array)$data["specification_category_name"],
array('subspecifications' => array(
array('subspecification_name' => $subspecification_name),
array('subspecification_value' => $product_specification_value)
)
)
);
}

}


}
#2

[eluser]Crafter[/eluser]
I tjhink you need to review how templates work. In prinicple, you need to pass all view local variables in an array, like this:

Code:
$this->load->view('myview', $my_array_containing_view_values);

In this case your variable $my_array_containing_view_values will be like this
Code:
$my_array_containing_view_values['subspecification_name'] = 'The sub spec name';
$my_array_containing_view_values['result'][] = array( "product_specification_value" => 'xxx1', "product_specification_name" -> 'yyy1');
$my_array_containing_view_values['result'][] = array( "product_specification_value" => 'xxx2', "product_specification_name" -> 'yyy2');

Got it?
#3

[eluser]zyklon[/eluser]
Yes i know this but i don't know how to compose the array in this case.Because for example i have
$data["specification_category_name"] = $specification_category_name;

In 2 foreach.They execute both.Basically i have in 2nd foreach,the both arrays,
Code:
$data["specification_category_name"] = $specification_category_name;
and
Code:
$data[subspecifications] => array(
array(’subspecification_name’ => $subspecification_name),
array(’subspecification_value’ => $product_specification_value
);
They are both in the 2nd foreach as i said.

Or look at this another try below.Shouldn't work?

Code:
$data["specification_categories"] = array();
          
         foreach($result as $key => $value)
         {
            
             $product_specification_value    =  $value["product_specification_value"];
            
            
        
          
             $result_category_spec = $this->lugnuts->get_products_category_specification_id($tableName,$id_product);
             foreach($result_category_spec as $key_2 => $value_2)
             {
                 $product_specification_category_id = $value_2["product_specification_category_id"];
                 $result_spec_id = $this->lugnuts->get_products_specification_id($tableName,$id_product,$product_specification_category_id);
                 foreach($result_spec_id as $key_4 => $value_4)
                 {
                     $result_spec_id_value = $value_4["product_specification_id"];
                    
                     $specification_category_name_res =  $this->lugnuts->get_category_specification_name($tableNameproducts_specifications_categories,$product_specification_category_id);
                     $specification_category_name = $specification_category_name_res->product_specification_category_name;
                     $data["specification_category_name"] = $specification_category_name;
                     $subspecification_name_result = $this->lugnuts->get_category_subspecification_name($tableNameproducts_specifications_subspecifications,$result_spec_id_value);
                     $subspecification_name = $subspecification_name_result->product_specification;
                                                                                
        $data["subspecifications"] =   array(
                                             'subspecification_name' => $subspecification_name,
                                             'subspecification_value' => $product_specification_value
                                             );
                                                                                    
                 $data["specification_categories"][] = array_merge((array)$data["specification_category_name"],$data["subspecifications"]);
                                                                                    
                 }
                                              
             }

or another try below:
Code:
foreach($result as $key => $value)
         {
                 $tmp = array();
             $product_specification_value    =  $value["product_specification_value"];
                
           $tmp['subspecifications'] = array();
             $result_category_spec = $this->lugnuts->get_products_category_specification_id($tableName,$id_product);
             foreach($result_category_spec as $key_2 => $value_2)
             {
                 $product_specification_category_id = $value_2["product_specification_category_id"];
                 $result_spec_id = $this->lugnuts->get_products_specification_id($tableName,$id_product,$product_specification_category_id);
                 foreach($result_spec_id as $key_4 => $value_4)
                 {
                     $result_spec_id_value = $value_4["product_specification_id"];
                    
                     $specification_category_name_res =  $this->lugnuts->get_category_specification_name($tableNameproducts_specifications_categories,$product_specification_category_id);
                     $specification_category_name = $specification_category_name_res->product_specification_category_name;
                     $tmp['specification_category_name'] = $specification_category_name;
                     $subspecification_name_result = $this->lugnuts->get_category_subspecification_name($tableNameproducts_specifications_subspecifications,$result_spec_id_value);
                     $subspecification_name = $subspecification_name_result->product_specification;
                
                     $tmp["subspecifications"][] = array(
                                                         'subspecification_name' => $subspecification_name,
                                                         'subspecification_value' => $product_specification_value
                                                         );
                                                        
                 }
                
             }
                     $data["specification_categories"] = $tmp;
        
         }
#4

[eluser]rvent[/eluser]
for that much code, please use the "code" tags in order make it simple for us...
#5

[eluser]Crafter[/eluser]
Look at my example again. I showed how to use an array.

Suppose you have an array of test results. You need to pass that to the view. OK?

So you pass the array like any other variable.

Code:
// Assume $results_from_db is an array of values
   // $results_from_db[] = array('productid' => 1, 'product_name' => 'toys');
   // $results_from_db[] = array('productid' => 2, 'product_name' => 'shirts');
   $data['results'] = $results_from_db;


Then in your view, you will have a LOCAL variable $results, which is an array.
Code:
foreach($result as $value)
{
   echo "ID : " . value['productid'] . '<br>';
   echo "ID : " . value['product_name'] . '<br>';
}
#6

[eluser]zyklon[/eluser]
I know this,i know how to pass it,i passed it,but look at the results:
http://208.109.241.32/~lugnuts/index.php...roduct_6/6
I am not constructing the array right.And as i said at the beginning i am using templates,meaning i can only put html in them not php.And i wrote in the first post how the template look like.
#7

[eluser]Crafter[/eluser]
Firstly, no one's going to bite you if you ass PHP to the template. It's your application, do what's best for it.

Secondly, all I see from you link is a template. I'm not sure exactly what you are asking.

I suspect your real problem is that you don't undretand the data that is being passed down to the view.

My suggestion is that you understand what is going on in your view.. Look at my earlier posts. Do you undetstand it? If you do, debug your template by printing the value of the variables you need. especially your arrys, like this:

Code:
foreach($result as $key => $value)
{
    // Your secret lies here!!!
   print_r($value);
}
#8

[eluser]zyklon[/eluser]
Check the print:
http://208.109.241.32/~lugnuts/index.php...duct_14/14

The template is:
{specification_categories}
<div class="specs">
<div class="blue_11_bold"><span class="specs_title">{specification_category_name}</span></div>
{subspecifications}
<div class="specs_gray">
<div class="specs_left">{subspecification_name}</div>
<div class="specs_right">{subspecification_value}</div>
</div>
{/subspecifications}
</div>
{/specification_categories}


and the array sent:
Code:
$result = $this->lugnuts->get_product($tableName,$id_product);      
        
          $data["specification_categories"] = array();
          
         foreach($result as $key => $value)
         {
            
             $product_specification_value    =  $value["product_specification_value"];
            
            
        
          
             $result_category_spec = $this->lugnuts->get_products_category_specification_id($tableName,$id_product);
             foreach($result_category_spec as $key_2 => $value_2)
             {
                 $product_specification_category_id = $value_2["product_specification_category_id"];
                 $result_spec_id = $this->lugnuts->get_products_specification_id($tableName,$id_product,$product_specification_category_id);
                 foreach($result_spec_id as $key_4 => $value_4)
                 {
                     $result_spec_id_value = $value_4["product_specification_id"];
                    
                     $specification_category_name_res =  $this->lugnuts->get_category_specification_name($tableNameproducts_specifications_categories,$product_specification_category_id);
                     $specification_category_name = $specification_category_name_res->product_specification_category_name;
                     $data["specification_category_name"] = $specification_category_name;
                     $subspecification_name_result = $this->lugnuts->get_category_subspecification_name($tableNameproducts_specifications_subspecifications,$result_spec_id_value);
                     $subspecification_name = $subspecification_name_result->product_specification;
                                                                                
        $data["specification_categories"][] =  array_merge((array)$data["specification_category_name"],
                                                   array('subspecifications' => array(
                                                                                     array('subspecification_name' => $subspecification_name),
                                                                                     array('subspecification_value' => $product_specification_value)
                                                                                     )
                                                                                     )
                                                                                     );
                 }
                
             }
                
        
         }
            
        
          print_r ($data["specification_categories"]);
                      
                      
        $products_show = $this->parser->parse('site/components/product.html', $data,TRUE);

The template is parsed with the array that is printed,and the result is starting from
Specification 2
As you see is
spec 1 {subspecification_value}
{subspecification_name} spec 1 value

and so on.
The real data that should be shown is:
Specification 2
spec 2-1 spec 2-1 value
spec 2-2 spec 2-2 value
Specification 1
spec 1 spec 1 value
spec 2 spec 2 value
These are the values from database.I highlited them to look clear.
That's all that should be there,not what it is now

The template is parsed but it shows wrong results.It should look like:
http://ip-208-109-241-32.ip.secureserver...etails.php
Specifications Category 1 part.
#9

[eluser]zyklon[/eluser]
All right let's leave this ,i made just one query instead of those 3.My question is now how should the array passed to a template that looke like:
Code:
{specification_categories}
        <div class="specs">
          <div class="blue_11_bold"><span  class="specs_title">{specification_category_name}</span></div>
          {subspecifications}
          <div class="specs_gray">
            <div class="specs_left">{subspecification_name}</div>
            <div class="specs_right">{subspecification_value}</div>
          </div>
          {/subspecifications}
       </div>
      {/specification_categories}
Should look like.I mean there is not just one loop,as in the codeigniter documentation,there are 2 loops.
Here it is explained for 1 loop:
http://ellislab.com/codeigniter/user-gui...arser.html
But what if there are 2?How the array should look like?For the the template i put above i made the array like this:
Code:
$data["specification_categories"] = array();
          
         foreach($result as $key => $value)
         {
                        
            $data["specification_categories"][] =   array(
                                                          'specification_category_name' => $value["product_specification_category_name"],
                                                           'subspecifications' => array(
                                                                                      array(            
                                                                                       'subspecification_name' => $value["product_specification"],
                                                                                        'subspecification_value' => $value["product_specification_value"]
                                                                                          )
                                                                                        )
                                                   );          
                
        
         }
You can see the results here:
http://208.109.241.32/~lugnuts/index.php...duct_11/11
The "specification category" section.My problem is that it must appear like:
Specification category 1
spec 1 spec 1
spec 2 spec 2
Not like it is now:
Specification category 1
spec 1 spec 1
Specification category 1
spec 2 spec 2




Theme © iAndrew 2016 - Forum software by © MyBB