View previous topic :: View next topic |
Author |
Message |
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Wed Dec 19, 2007 8:39 am Post subject: how can i display this with smarty? |
|
|
Hi all
i have this php code and i want to display it with smarty such as i display it with php
Code: | $sql_invoices="SELECT*FROM invoices ORDER BY invoice_id DESC LIMIT 0,5";
$query_on_invoices=$DB->Query($sql_invoices);
while($fetching_invoices=$DB->Fetching($query_on_invoices))
{
$invoices_array[]=$fetching_invoices;
extract($fetching_invoices);
//this to show customer name in invoices table
$sql_customers="SELECT customer_name FROM customers WHERE customer_id='$customer_id'";
$query_on_customers=$DB->Query($sql_customers);
while($fetching_customers=$DB->Fetching($query_on_customers))
{
extract($fetching_customers);
$customers_array[]=$fetching_customers;
//This waht i want to display with smarty
echo $customer_name."-";
}
} |
i tried foreach and sections and i failed to display this variable $customer_name
plz help and tell me if there are any edits in programing
thanks for your efforts |
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Thu Dec 20, 2007 5:52 am Post subject: |
|
|
is it possible or not ?
i see 40 views and no replays is it so hard ? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Dec 20, 2007 5:56 am Post subject: |
|
|
Please show what you have tried. No one is going to do this for you. |
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Thu Dec 20, 2007 6:01 am Post subject: |
|
|
so sorry this is it
but i'm sure it is wrong so i didn't post it
and please tell my if i have any errors in programing
because i display it correctly with php
Code: | <p style="display: none;">{counter start=0 skip=1}</p>
<table>
<tr class="tableshead">
<td>#</td>
<td>Invoice ID</td>
<td>Customer name</td>
<td>Salesman name</td>
<td>Invoice date</td>
<td>Total</td>
</tr>
{foreach item=invoice from=$invoices_show}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}"class="loop_tr">
<td>{counter}</td>
<td>{$invoice.invoice_unique_number}</td>
<td>
{foreach item=customer from=$customers_show}
{$customer.customer_name}
{/foreach}
</td>
<td>Salesman name</td>
<td>{$invoice.invoice_date}</td>
<td>{$invoice.invoice_total}</td>
</tr>
{/foreach}
</table> |
|
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Fri Dec 21, 2007 3:23 am Post subject: |
|
|
plz help my in this code in this code i need it very soon |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Dec 21, 2007 2:43 pm Post subject: |
|
|
So, exactly what isn't working? It looks like the syntax is ok.
One thing I notice, you have a nested {foreach} loop, and the inner loop is going to display the same customer info over and over. You probably want to loop over something from the outter foreach, such as $invoice.customers_show (that is a guess, I don't know the data structure.) |
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Sat Dec 22, 2007 3:04 am Post subject: |
|
|
thank you mohrt
this the table in admin page
all data come from table in database called invoices exception "customer_name" and "sales_man_name"
customer_name comes from another table called customers
this is all
with php i got it but when i tried to get it with smarty i failed
this is the php code and this working ok
Code: | $sql_invoices="SELECT*FROM invoices ORDER BY invoice_id DESC LIMIT 0,5";
$query_on_invoices=$DB->Query($sql_invoices);
while($fetching_invoices=$DB->Fetching($query_on_invoices))
{
$invoices_array[]=$fetching_invoices;
extract($fetching_invoices);
//this to show customer name in invoices table
$sql_customers="SELECT customer_name FROM customers WHERE customer_id='$customer_id'";
$query_on_customers=$DB->Query($sql_customers);
while($fetching_customers=$DB->Fetching($query_on_customers))
{
extract($fetching_customers);
$customers_array[]=$fetching_customers;
//This waht i want to display with smarty
echo $customer_name."-";
}
} |
thanks to all and hope to get help soon[img][/img] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Dec 22, 2007 3:40 am Post subject: |
|
|
Do that nested SQL in PHP and assign your data in nested arrays to Smarty. Then in the template, loop over the data in a nested loop.
In other words, create an array of data in PHP with everything inside of it (invoices, and customer data per invoice), then assign it to Smarty. |
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Sat Dec 22, 2007 3:46 am Post subject: |
|
|
mohrt
i tried this but i failed as i said and i posted my codes so please correct my codes that i posted and many thanks to you |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Dec 22, 2007 3:58 am Post subject: |
|
|
something like:
Code: |
$sql_invoices="SELECT*FROM invoices ORDER BY invoice_id DESC LIMIT 0,5";
$query_on_invoices=$DB->Query($sql_invoices);
while($fetching_invoices=$DB->Fetching($query_on_invoices))
{
$sql_customers="SELECT customer_name FROM customers WHERE customer_id='{$fetching_invoices['customer_id']}'";
$query_on_customers=$DB->Query($sql_customers);
while($fetching_customers=$DB->Fetching($query_on_customers))
$customers_array[]=$fetching_customers;
$fetching_invoices['customer_info'] = $customers_array;
$invoices_array[]=$fetching_invoices;
$customers_array = null;
} |
Now you have a nested array $invoices_array, assign that to smarty and loop over it with nested loops.
And, stay way from extract(), it makes horrible code to work with. |
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Sat Dec 22, 2007 4:35 am Post subject: |
|
|
thanks about your replay
i edited php code as you told me like this
Code: | $sql_invoices="SELECT*FROM invoices ORDER BY invoice_id DESC LIMIT 0,5";
$query_on_invoices=$DB->Query($sql_invoices);
while($fetching_invoices=$DB->Fetching($query_on_invoices))
{
$sql_customers="SELECT customer_name FROM customers WHERE customer_id='{$fetching_invoices['customer_id']}'";
$query_on_customers=$DB->Query($sql_customers);
while($fetching_customers=$DB->Fetching($query_on_customers))
{
$customers_array[]=$fetching_customers;
}
$fetching_invoices['customer_id'] = $customers_array;
$invoices_array[]=$fetching_invoices;
$customers_array = null;
}
$Smarty->assign("invoices_show",$invoices_array);
$Smarty->assign("customers_show",$customers_array);
$Smarty->display("invoices_show_to_admin.html"); |
and i put this smarty code
Code: |
{foreach item=invoice from=$invoices_show}
<tr bgcolor="{cycle values="#eeeeee,#d0d0d0"}"class="loop_tr">
<td>{counter}</td>
<td>{$invoice.invoice_unique_number}</td>
<td>
{foreach item=customer from=$customers_show}
{$customer.customer_name}
{/foreach}
</td>
<td>Salesman name</td>
<td>{$invoice.invoice_date}</td>
<td>{$invoice.invoice_total}</td>
</tr>
{/foreach} |
but not working
every thing working ok exception customer_name
so please correct my smarty code i know it is wrong but i cant correct it |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Dec 22, 2007 6:19 am Post subject: |
|
|
You probably need:
Code: | {foreach item=customer from=$invoice.customer_id} |
And you should only need to assign one array. See how I nested the data in the PHP array. Get rid of:
Code: | $Smarty->assign("customers_show",$customers_array); |
|
|
Back to top |
|
Egystr Smarty Rookie
Joined: 16 Dec 2007 Posts: 10
|
Posted: Sat Dec 22, 2007 6:30 am Post subject: |
|
|
It works
many thanks for you
i appreciate your efforts |
|
Back to top |
|
|