[SOLVED] assign_block_vars() + Arrays

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
odo324
Registered User
Posts: 39
Joined: Tue May 13, 2003 6:38 am

[SOLVED] assign_block_vars() + Arrays

Post by odo324 »

I'm trying to pass a dynamicly created array to the "assign_block_vars()" function. But... the root "SWITCH" repeats with each block ("SWITCH.CHILD").

:arrow: CODE: This code will 'dissects' the array into a form that the assign_block_vars() function can take.
echo 'Count = "'.count($array_vars).'"<br />';
//EACH PARENT SWITCH
while ( list($switch_key, $switch_array) = each($array_vars) )
{
  • //$switch_key == NAME OF SWITCH
    //$switch_array == ARRAY OF VARIABLES
    echo '---- current = "'.$switch_key . '" ---- Count = "'.count($switch_array).'"<br />';
    //EACH VARIABLE of "SWITCH"
    while ( list($switch_var, $switch_val) = each($switch_array) )
    {
    • // $switch_var_key == Variables of the current "switch"
      // $switch_var_val == Value of each key. If this = '.loop_', then it is an array!
      echo '-------- current = "' . $switch_var . '" ---- value = "'.$switch_val.'" ---- Count = "' . count($switch_val) . '"<br />';
      if ( !is_array($switch_val) ) //IF "SWITCH VARIABLE" is -NOT- an array.
      {
      • echo '-------- ...is a Var<br />';
        $store_vars[$switch_var] = $switch_val; //STORE all "SWITCH" VARIABLE
      }
      else
      //IF "SWITCH VARIABLE" -IS- an array.
      {
      • echo '-------- ...is an Array<br />';
        while ( list($block_name, $block) = each($switch_val) ) //EACH MATCH [sql] (in "SWITCH VARIABLE")
        {
        • // $block_name == Name of HTML this block
          // $block == ARRAY containing each match retreved from the database.
          echo '---------------- current = "' . $block_name . '" ---- Count = "'.count($block).'"<br />';
          while ( list($child, $var_array) = each($block) ) //EACH BLOCK (each [child/sql match] to loop through)
          {
          • $template->assign_block_vars( ($switch_key.$switch_var.$block_name), $block_sum );
            //$child == Number of current child.
            //$var_array == Array of variables for this child.
            echo '-------------------------------- block (sql match) #"' . $child . '" ---- Count = "'.count($var_array).'" ---- >>> "'.$switch_key.$switch_var.$block_name.'"<br />';
            //while ( list($child_var, $child_var_val) = each($var_array) ) // -- EACH CHILD VARIABLE
            //{
            • // echo '---------------- ' . $child_var . ' --> ' .$child_var_val . '<br />';
            //}
          }
          //echo '>> ' . $switch_key.$switch_var_key.$block_name;
          //$template->assign_block_vars( $switch
        _key.$switch_var_key.$block_name, $array_block );
      }
      }

    }

    echo '<br />';
    echo 'store_vars = '.count($store_vars) . '<br />';
    echo 'store_vars[V_TYPE] = '.count($store_vars["V_TYPE"]) . '<br />';


    $template->assign_block_vars( $switch_key, $store_vars );
}



:arrow: CLASS: In my class, the variables are defined in an array.

Code: Select all

$sql = 'SELECT	id, list_order, title, description
		FROM	' . $table. '
		WHERE	parent_id=' . $id . '
		ORDER BY list_order';
		$result = $db->sql_query($sql);
if( !($result) )
	message_die(GENERAL_ERROR,
		'<br />DD ERROR: The <u>id</u> (<b>' . $id . '</b>) can not be found in the <b>' . $table . '</b> <u>table</u>!<br /><br />
		** Both peramiters are required **',
		'', __LINE__, __FILE__, $sql
		);

$i = 0;
unset($child_vars, $array_vars);

while( $child = $db->sql_fetchrow($result) ) //for each child with 'parent_id'...
{
	//list of switches/loops.
	$child_vars['child'][] = array(
		// URLS
		'U_CHILD_VIEW'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=0&view=' . $child['id']),
		'U_CHILD_EDIT'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=1&view=' . $child['id']),
		'U_CHILD_DELETE'=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=3&view=' . $child['id']),
		'U_CHILD_UP'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=4&view=' . $id . '&sub=' . $child['id']),
		'U_CHILD_DOWN'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=5&view=' . $id . '&sub=' . $child['id']),
		// VARS
		'V_CHILD_TITLE'	=> $child['title'],
		'V_CHILD_DESC'	=> $child['description'],
		);
}
$array_vars = array(
	'switch_browse'	=> array( //list of main switches/loops.
		'S_TYPE'	=> $http_type, //holds the 'type' currently in view,	...when creating a 'new' whatever.
		'S_VIEW'	=> $id, //holds the 'parent id',					...when creating a 'new' whatever.
		'S_PARENT'	=> $id,
		'.loop_'	=> $child_vars,
		),				//'.loop_' 	=> array//list of loops
	);

return $array_vars;
:arrow: TEMPLATE:

Code: Select all

<!-- BEGIN switch_browse -->
	<tr>
		<td colspan="5" class="row3">
			<span class="gensmall">{V_CURRENT_DESC}</span>
		</td>
	</tr>
	<!-- BEGIN loop_child -->
	<tr>
		<td class="row1">
			{CATEGORY_IMG}
		</td>
		<td class="row1" width="60%">
			<a href="{switch_browse.loop_child.U_CHILD_VIEW}" style="text-decoration:none;font-size:13px;font-weight:bold;" alt="">{switch_browse.loop_child.V_CHILD_TITLE}</a>
			<span class="gensmall"><br />{switch_browse.loop_child.V_CHILD_DESC}</span><div align='right'>{L_DWNS}: <b>0</b> / {L_FILS}: <b>0</b></div>
		</td>
		<td class="row2" width="15%" align="center" nowrap="nowrap">
			<a style="text-decoration:none;" href="{switch_browse.loop_child.U_CHILD_UP}" alt="">{L_UP}</a>
			<br />
			<a style="text-decoration:none;" href="{switch_browse.loop_child.U_CHILD_DOWN}" alt="">{L_DOWN}</a>
		</td>
		<td class="row2" width="25%" align="center" nowrap="nowrap">
			<a style="text-decoration:none;" href="{switch_browse.loop_child.U_CHILD_EDIT}" alt="">{L_EDIT}</a> |
			<a style="text-decoration:none;" href="{switch_browse.loop_child.U_CHILD_DELETE}" alt="">{L_DELETE}</a> |
			<a style="text-decoration:none;" href="{switch_browse.loop_child.U_CHILD_RESYNC}" alt="">{L_RESYNC}</a>
		</td>
	</tr>

	<!-- END loop_child -->
	<tr>
		<td class="cathead" colspan="5" style="padding:2px; height:27;">
				<input type="hidden" name="type" value="{switch_browse.S_TYPE}" />
				<input type="hidden" name="view" value="{switch_browse.S_VIEW}" />
				<input type="hidden" name="mode" value="6" />
				<input type="hidden" name="newtype_parent" value="{switch_browse.S_PARENT}" class="post" />&nbsp;
				<input type="text" name="newtype_title" class="post" />&nbsp;
				<input type="submit" value="Insert New" class="mainoption" />
		</td>
	</tr>
<!-- END switch_browse -->
The problem is "SWITCH_BROWSE" repeates for each "LOOP_CHILD" that exists! I dont get it. All loops are filtered out.

-- A confused and very fustrated indivisual. :oops:
Last edited by odo324 on Fri Feb 27, 2004 5:43 am, edited 4 times in total.

odo324
Registered User
Posts: 39
Joined: Tue May 13, 2003 6:38 am

eval() problem, I think.

Post by odo324 »

Almost forgot! Here's my echo results:
Count = "1"
---- current = "switch_browse" ---- Count = "4"
-------- current = "S_TYPE" ---- value = "" ---- Count = "0"
-------- ...is a Var
-------- current = "S_VIEW" ---- value = "0" ---- Count = "1"
-------- ...is a Var
-------- current = "S_PARENT" ---- value = "0" ---- Count = "1"
-------- ...is a Var
-------- current = ".loop_" ---- value = "Array" ---- Count = "1"
-------- ...is an Array
---------------- current = "child" ---- Count = "4"
-------------------------------- block (sql match) #"0" ---- Count = "7" ---- >>> "switch_browse.loop_child"
-------------------------------- block (sql match) #"1" ---- Count = "7" ---- >>> "switch_browse.loop_child"
-------------------------------- block (sql match) #"2" ---- Count = "7" ---- >>> "switch_browse.loop_child"
-------------------------------- block (sql match) #"3" ---- Count = "7" ---- >>> "switch_browse.loop_child"



--- EDIT ---

Ok, I decided to echo the hell out of the assign_block_vars() function to see where the problem starts.

I found it, but this doesn't make any sence! (...Read on...)

Code: Select all

function assign_block_vars($blockname, $vararray)
{
	echo '<br />';
	if (strstr($blockname, '.'))
	{
		// Nested block.
		$blocks = explode('.', $blockname);		$blockcount = sizeof($blocks) - 1;		$str = '$this->_tpldata';
		for ($i = 0; $i < $blockcount; $i++)
		{
			echo '>1.> blocks['.$i.'] = '. $blocks[$i] .'<br />';
			$str .= '[\'' . $blocks[$i] . '.\']';
			echo '>2a> str = '. $str .'<br />';
			echo '>2b> sizeof($str) = '. sizeof($str) .'<br />';
			eval('$lastiteration = sizeof(' . $str . ') - 1;'); // <--- PROBLEM!
			echo '>3.> lastiteration = '.$lastiteration .'<br />';
			$str .= '[' . $lastiteration . ']';
		}
		// Now we add the block that we're actually assigning to.
		// We're adding a new iteration to this block with the given
		// variable assignments.
		$str .= '[\'' . $blocks[$blockcount] . '.\'][] = $vararray;';

		// Now we evaluate this assignment we've built up.
		eval($str);
	}
	else
	{
		// Top-level block.
		// Add a new iteration to this block with the variable assignments
		// we were given.
		$this->_tpldata[$blockname . '.'][] = $vararray;
	}

	return true;
}
NOTE: I numbered each ECHO for easier reference.

Here's my echo:

Code: Select all

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = -1

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 0

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 1

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 2

//NOTE: In this version of the script, the sql statement I used had 4 matches.
As you can see, "lastiteration" is diffrent each time the loop is run!

Using the SAME template, I ran a 'traditional' script... (using no pre-defined arrays)
...and got this result:

Code: Select all


>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 0

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 0

>1.> blocks[0] = switch_browse
>2a> str = $this->_tpldata['switch_browse.']
>2b> sizeof($str) = 1
>3.> lastiteration = 0

//NOTE: In this version of the script, the sql statement I used had only 3 matches.
See, "lastiteration" remains at '0'!

How, using an array, does Lastiteration change!
The $str variable is the exact same thing in both tests! Plus, I read that variables defined in an EVAL statement should retain their value!
So, why is it changing!

Is this the fabled 'black magic" of php?
:cry:

odo324
Registered User
Posts: 39
Joined: Tue May 13, 2003 6:38 am

Post by odo324 »

I think i has the solution! :lol:

When I started my day, I looked and echo'ed more of the assign_block_vars() function, when I realized... "It only orginizes the info! It doesn't really 'do' anything usefull with it! It just passes all variables to the "_tpldata()" member function!"

So, I reorginized my array a little...
:arrow: CLASS:

Code: Select all

while( $child = $db->sql_fetchrow($result) ) //for each child with 'parent_id'...
{
	//list of switches/loops.
	$child_array[] = array(
		// URLS
		'U_CHILD_VIEW'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=0&view=' . $child['id']),
		'U_CHILD_EDIT'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=1&view=' . $child['id']),
		'U_CHILD_DELETE'=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=3&view=' . $child['id']),
		'U_CHILD_UP'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=4&view=' . $current_id . '&sub=' . $child['id']),
		'U_CHILD_DOWN'	=> append_sid('admin_dwndb_hierarchy.' . $phpEx . '?action=5&view=' . $current_id . '&sub=' . $child['id']),
		// VARS
		'V_CHILD_TITLE'	=> $child['title'],
		'V_CHILD_DESC'	=> $child['description'],
		);
}

$array_vars['switch_browse.'][0] = array(
	'loop_child.'	=> $child_array,
	);
...and using that array like this...
:arrow: CODE:

Code: Select all

$class = new my_class(); //MY CLASS
$array_vars = $class->my_func(); //GET ALL VARIABLES
$template->_tpldata = $array_vars; //THE SOLUTION
I can hardly beleve it! It just all fits togeather!
In the template, all variables function as thay normaly would!
I'm so happy! :roll:


:?: However, the 'ERROR' remains unsolved!
:!: Not that I care... anymore, :wink: .

Post Reply

Return to “[2.0.x] MOD Writers Discussion”

cron