Jump to content


Photo

Arrays -- MultiDimensional


  • Please log in to reply
1 reply to this topic

#1 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 17 October 2006 - 09:16 AM

Okay it's been a while since i've posted as I havent needed much help -- but now on my site I absolutely need this array to be multi dimensional.. and I'll go into that in a bit..

First off I am using Smarty as a templateing engine..  which in the actual html I sort through a multi dimensional array like this..

{section name=d loop=$data}
  <tr>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF">&nbsp;{$data[d].hl_date|date_format:"%m-%d-%Y"}</td>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF">&nbsp;{$data[d].hl_page}</td>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF">&nbsp;{$data[d].hl_memname}</td>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF">&nbsp;{$data[d].hl_adminname}</td>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF">&nbsp;{if $data[d].hl_referer eq ''}directly requested{else}{$data[d].hl_referer}{/if} </td>
    <td class="bodytextSmallBlue" bgcolor="#FFFFFF"><a href="?action=logs&act=delete&id={$data[d].hl_id}"><img src="../templates/cesite/images/b_drop.gif" width="16" height="16" border="0"></a></td>
  </tr>
  {/section}

It's all done with the section now only problem is the array passed to it MUST be a multi dimensional array..  When I print_r my array here is the output..

Data:Array
(
    [hl_id] => 1
    [hl_date] => 34234234234
    [hl_page] => hacklogs
    [hl_memname] => Radar
    [hl_adminname] => Radar
    [hl_browser] => IE Exploder
    [hl_ip] => 172.0.0.1
    [hl_referer] => 
)

And here is how I would like it to be...  and this has to be dynamic creation because today there might be two entries and tomorrow there might be 1000.....

Data: Array (
[0] = array (
    [hl_id] => 1
    [hl_date] => 34234234234
    [hl_page] => hacklogs
    [hl_memname] => Radar
    [hl_adminname] => Radar
    [hl_browser] => IE Exploder
    [hl_ip] => 172.0.0.1
    [hl_referer] => 
}
[1] = array (
    [hl_id] => 1
    [hl_date] => 34234234234
    [hl_page] => hacklogs
    [hl_memname] => Radar
    [hl_adminname] => Radar
    [hl_browser] => IE Exploder
    [hl_ip] => 172.0.0.1
    [hl_referer] => 
)
)
}

Here is my code I've got for this case in my index.php file...

<?php
case logs:
	$data1 = mysql_result(mysql_query("SELECT count(*) FROM logs"), 0);
	$turbo->assign('hl_cnt', $data1);
	echo $data1;
	$data = mysql_query("SELECT * FROM logs");
	$data = mysql_fetch_assoc($data);
	$turbo->assign('data', $data);
	$page = "logs";
	break;
?>

I just created a function to kinda help set the way for you guys helping me.. it sorta works but only loops does it over 1 instance...  i have 2 in the system right now both different...

<?php
function assign_md_array($array, $cnt, $result) {
	for ($row = 0; $row < $cnt; $row++) {
		foreach ($array as $key => $value) {
		$data[$row][$key] = $value;
		}
	}
	$this->assign($result, $data);
	echo "<pre> function array:";
	print_r ($data);
	echo "</pre>";
	}
?>

and here is the output of this..

 function array:Array
(
    [0] => Array
        (
            [hl_id] => 1
            [hl_date] => 34234234234
            [hl_page] => hacklogs
            [hl_memname] => Radar
            [hl_adminname] => Radar
            [hl_browser] => IE Exploder
            [hl_ip] => 172.0.0.1
            [hl_referer] => 
        )

    [1] => Array
        (
            [hl_id] => 1
            [hl_date] => 34234234234
            [hl_page] => hacklogs
            [hl_memname] => Radar
            [hl_adminname] => Radar
            [hl_browser] => IE Exploder
            [hl_ip] => 172.0.0.1
            [hl_referer] => 
        )

)

any help on this would be appreciated..  thanks...

#2 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 17 October 2006 - 12:07 PM

Okay so I've done some testing and I finally got this to work correctly..  so for anyone else having issues with arrays being turned into multidimensional arrays...  read this...

Here is the new function:

<?php
function assign_md_array($query, $cnt, $result) {
for ( $row = 0; $row < $cnt && $array = mysql_fetch_assoc($query); $row++ ) {
	foreach ($array as $key => $value) {
  $data[$row][$key] = $value;
  }
	}
// change this to whatever you need such as return $data;
	$this->assign($result, $data);
	}
?>

Then here is the syntax for calling this function..

<?php
// lets get the count of how many are in the database...  
$data1 = mysql_result(mysql_query("SELECT count(*) FROM logs"), 0);
	$turbo->assign('hl_cnt', $data1); // could delete this if you're not using smarty
// now we setup the query and run it that far only..
	$data = mysql_query("SELECT * FROM logs");
// $data = the query string, $data = the count and 'data' could be deleted if you're not using the assign function in smarty...
	$turbo->assign_md_array($data, $data1, 'data'); // i put the function in my class file.
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users