Jump to content

Arrays -- MultiDimensional


radar

Recommended Posts

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..

[code]
{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}
[/code]

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..

[code]
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] =>
)
[/code]

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.....

[code]
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] =>
)
)
}[/code]

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

[code]
<?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;
?>
[/code]

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...

[code]
<?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>";
}
?>
[/code]

and here is the output of this..

[code]
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] =>
        )

)
[/code]

any help on this would be appreciated..  thanks...
Link to comment
Share on other sites

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:

[code]
<?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);
}
?>
[/code]

Then here is the syntax for calling this function..

[code]
<?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.
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.