Brainstorms of a Webdev

Web development, PHP, Security, etc… etc…

  • Home
  • About

1

May

Variable Variables and Template Lite

Posted by tarnus  Published in PHP, Template Lite

There is one programing concept that always hurts my head, and that is the variable variables or dynamic variables. What dynamic let you do is access data from the value of the a variable you are viewing. (See I told you it would hurt your head)

 
$b =20;  // set the value of b
$c="b";  // set c to the name of the variable you want to view
$d=${$c}; // the variable variable  $d is now equal to 20
 

Many of you are probably asking yourself, " OK I understand that , but why do I need to do that?"
The number one place I use this, is in a web grid form. Any place you need to update/or delete data in that grid. Below is an example of what a grid of data may look like:

Data Grid

The issues you face in the above grid is how do I delete multiple items and change multiple elements of data in grid once it is submitted. The solution is you assign each field a row number and add that to the field name.

Example:

 
<input name="del_1" type="checkbox" />
<input name="id_1" type="hidden" value="117" />
<input name="del_2" type="checkbox" />
<input name="id_2" type="hidden" value="95" />

Note each field had its name attached with a _row number so you will see an id_1 as a field name. After this form is submitted you can use the below php code to get the values dynamically.


// Check for item delete
if ((isset($_POST['delete'])) or (isset($_POST['delete_x']))){
for ($i=0; $i<=$totrecs; $i++)
{
$tdel=$_POST['del_'.$i];
$tid=$_POST['id_'.$i];
// Another way to do this is this way
// $tempvar="id_".$i;
// $tid=$_POST[${$tempvar}];
if ($tdel=="on")
{
// Delete Item, the item ID is $tid
}
} // Loop
} // End Post Check

Template Lite And Dynamic Variables
Form Data grid is also the reason I used dynamic variables in templates. Once I have edited a data grid I need to set values in the grid, select drop downs and, check radio or check boxes. There is some extra steps you need to do to use them and it also depends on how you pass your data to template lite. The following is an example showing a more complex use of variable variables.

{foreach key=key2 value=item2 from=$jetarray }
{ assign var="dummyvar1" value="`$item2.jetski_id`_bookid"}
{ assign var="dummyvar2" value="`$item2.jetski_id`_owner"}
{ assign var="cellcolor" value="`$item2.jetski_id`_color"}
{ assign var="dummyvar3" value="`$item2.jetski_id`"}
 
 {if $item[$dummyvar1]!=""}
   {if $session.myid == $item[$dummyvar2]}
<input name="jetid_{$item2.jetski_id}_{$key}" type="hidden"
 value="{$item2.jetski_id}" />
   {/if}
{/if}
 
 {/foreach}

Note we assigned a variable $dummyvar1 with the value $item2.jetski_id and appended _bookid to that variable name. Jetski number 1 turns into 1_bookid. After that we pull the value out of the array $item[$dummyvar1] so what we are actually doing is viewing $item['1_bookid'].

While there are some other uses for dynamic variables these are how I most recently have used them. If it helps someone other than myself, cool. Otherwise this is a place I have my own personal reference library.


Continue reading...

no comment

Search

Get Support

Categories

  • Domain Names (1)
  • Electronics (1)
  • Javascript (2)
  • mysql (1)
  • PHP (4)
  • Template Lite (2)
  • Uncategorized (1)

Archives

  • December 2008
  • May 2008
  • April 2008

Blogroll

  • Alien Assault Traders
  • Diana Botsford
  • Oznet
  • Sonnar Internet
  • Springfield Net

RSS Twitter: tarnus

  • Oh...waiting always sucks when your busy 09:05:17 AM May 23, 2008 from txt
  • Why do people refuse to listen 04:00:34 PM May 19, 2008 from im
  • Back to work... its a Monday 10:51:08 AM May 19, 2008 from im
  • Nothing like kids with nothing better to do than mess with my servers to ruin my sleep 12:52:10 AM May 18, 2008 from im
  • I got to get in better shape...I hate feelin winded 05:23:05 PM May 17, 2008 from txt

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
May 2008
M T W T F S S
« Apr   Dec »
 1234
567891011
12131415161718
19202122232425
262728293031  

Recent Post

  • javascript change input box type to password
  • sql file splitter to the rescue
  • Palm Centro, my new phone
  • PHP day count and date comparison
  • Experiments in Domain Parking
  • Javascript Auto Focus Form Element
  • Variable Variables and Template Lite
  • Template Lite Modifiers

Recent Comments

  • Mikael Benjamin Ulstrup » Pro… in sql file splitter to the rescue
  • Online Reviews » Blog Archive… in Palm Centro, my new phone
© 2007 Brainstorms of a Webdev
Theme by Wired Studios, courtesy of Corvette Garage
Valid XHTML | Valid CSS 3.0
Powered by Wordpress