Jump to content

[SOLVED] Format(HTML) ARRAY output


facets

Recommended Posts

Hey Gang,

 

I have an array I need to display creatively and can not work it out.

The data looks something like the following:

 

Barry | Tennis | Captain

Barry | Soccer | Goal Kicker

Barry | Water Polo | Player

 

I know how to display this using a foreach loop the 'satndard' way but I would like to display it the following way..

 

Barry | Tennis | Captain

         | Soccer | Goal Kicker

         | Water Polo | Player

 

Is there any nice way to do this?

 

tks, Will./

Link to comment
Share on other sites

Thanks for the prompt reply.

Here's my array output.. It doesn't match the example data above but this IS the real data.

 

[pre]

array(75) {

  [5106]=>

  array(6) {

    [0]=>

    string(4) "6307"

    [1]=>

    string(10) "2007-12-01"

    [2]=>

    string(10) "2007-12-31"

    [3]=>

    string(3) "yes"

    [4]=>

    string(0) ""

    [5]=>

    string(6) "252.48"

  }

  [5134]=>

  array(6) {

    [0]=>

    string(4) "6269"

    [1]=>

    string(10) "2007-12-01"

    [2]=>

    string(10) "2007-12-31"

    [3]=>

    string(3) "yes"

    [4]=>

    string(0) ""

    [5]=>

    string(6) "283.03"

  }[/pre]

 

Displayed in a table. But first column only has new row when there is a new client. Does that make sense..

tia, Will./

Link to comment
Share on other sites

Okay, as a bit of exercise I wrote a function to output a table from your two-dimensional array, while blanking out the client when it's a repeat.

 

<?php
function array2table($array) {
sort($array);
echo '<table>';
foreach ($array as $nestedArray) {
	if ($nestedArray[0] == $prev) {
		echo '
<tr>
	<td></td>';
		unset($nestedArray[0]);
		foreach ($nestedArray as $value) {
			echo '<td>', $value, '</td>';
		}
		echo '
</tr>';
	} else {
		echo '
<tr>
	';
		foreach ($nestedArray as $value) {
			echo '<td>', $value, '</td>';
		}
		echo '
</tr>';
		$prev = $nestedArray[0];
	}
}
echo '
</table>';
}

// sample usage, inputting the following two-dimensional array
$array = array(
array('client1', 'something', 'more'),
array('client1', 'another something', 'more'),
array('client4', 'Water Polo', 'Player'),
array('client2', 'Tennis', 'Captain'),
array('client2', 'Soccer', 'Goal Kicker'),
array('client63', 'Tennis', 'Captain'));
array2table($array); // run the function
?>

 

Outputs:

<table>
<tr>
	<td>client1</td><td>another something</td><td>more</td>
</tr>
<tr>
	<td></td><td>something</td><td>more</td>
</tr>

<tr>
	<td>client2</td><td>Soccer</td><td>Goal Kicker</td>
</tr>
<tr>
	<td></td><td>Tennis</td><td>Captain</td>
</tr>

<tr>
	<td>client4</td><td>Water Polo</td><td>Player</td>
</tr>
<tr>
	<td>client63</td><td>Tennis</td><td>Captain</td>
</tr>

</table>

 

or forum-formatted:

 

client1 another something more

[/td]somethingmore

client2SoccerGoal Kicker

Captain

client4Water PoloPlayer

client63TennisCaptain

 

My approach maybe isn't the most elegant, but it works :)

Link to comment
Share on other sites

Thanks for your reply. That function did the trick!

I have another question if someone has time..

 

I'm using the following code to create my array and display it. I'd like to tally a total for each client but am unsure on how to seperate the client_id and totals for calculation.

 

foreach ($clientID as $clients) {
    // Get Statement Details
    $query = "SELECT * FROM statements, invoices WHERE statements.id = invoices.statement_id
 AND statements.due_date >= '$date1' AND invoices.paid != 'on' AND statements.client_id = '$clients'";
    $result = mysql_query($query, $linkID) or die("Data not found : Statement Error.");

    while($row = mysql_fetch_array($result)) {
$contents[] = array($row['client_id'], $row['id'], $row['statement_date'], $row['due_date'], $row['overdue'],
    $row['amount_owed'], $row['total']);
    } // End Client Names
}
foreach ($contents as $nestedArray) {

    if ($nestedArray[0] == $prev){
$content .= "<tr><td width=120></td>";
unset($nestedArray[0]);
foreach ($nestedArray as $value) {
    $content .= "<td width=120>" . $value . "</td>";
}
$content .= "</tr>\n";
    } else {
$content .= "<tr>";
foreach ($nestedArray as $value) {
    $content .= "<td width=120>" . $value . "</td>";
}
$content .= "</tr>\n";
$prev = $nestedArray[0];
    }
}

 

Any ideas?

 

tia Will.

Link to comment
Share on other sites

Thanks thebadbad for your help so far..

OK, I have made a small leap forward and have merged all the data I need to display.

2 steps to go.

 

How would I use the php/html code below to only print array position 6 once per client? (In this case 743.24)

And highlight alternate client rows. (Some client rows may have one line some may have more)

 

Here's the new array.

 

$contents[] = array($row['client_id'], $row['id'], $row['statement_date'], $row['amount_owed'],$row['total'],$newTotal);

 

Which outputs like so.

 

array(231) {

  [0]=>

  array(6) {

    [0]=>

    string(4) "5106"

    [1]=>

    string(4) "6104"

    [2]=>

    string(10) "2007-11-01"

    [3]=>

    string(0) ""

    [4]=>

    string(6) "263.16"

    [5]=>

    string(6) "743.24"

  }

  [1]=>

  array(6) {

    [0]=>

    string(4) "5106"

    [1]=>

    string(4) "6255"

    [2]=>

    string(10) "2007-12-01"

    [3]=>

    string(0) ""

    [4]=>

    string(5) "59.84"

    [5]=>

    string(6) "743.24"

  }

 

foreach ($contents as $nestedArray) {

    if ($nestedArray[0] == $prev){
$content .= "<tr><td width=120></td>";
unset($nestedArray[0]);
foreach ($nestedArray as $value) {
    $content .= "<td width=120>" . $value . "</td>";
}
$content .= "</tr>\n";
    } else {
$content .= "<tr>";
foreach ($nestedArray as $value) {
    $content .= "<td width=120>" . $value . "</td>";
}
$content .= "</tr>\n";
$prev = $nestedArray[0];
    }
}

 

Any pointers would be great..

 

Will./

Link to comment
Share on other sites

No problem, I learn best while coding :)

 

Your first problem was easy, but I can't seem to get my head around the coloring of alternating client-rows. Instead of unsetting the first var in the nestedArray, when the client is a repeat, I just blanked out the first and sixth var, like this:

 

$nestedArray[0] = $nestedArray[5] = '';

 

I also put in some double quotes in your HTML. Edited foreach (edited from your last post):

 

foreach ($contents as $nestedArray) {

    if ($nestedArray[0] == $prev){
$content .= "<tr>";
$nestedArray[0] = $nestedArray[5] = '';
foreach ($nestedArray as $value) {
    $content .= "<td width=\"120\">" . $value . "</td>";
}
$content .= "</tr>\n";
    } else {
$content .= "<tr>";
foreach ($nestedArray as $value) {
    $content .= "<td width=\"120\">" . $value . "</td>";
}
$content .= "</tr>\n";
$prev = $nestedArray[0];
    }
}

Link to comment
Share on other sites

I ended up getting the Alternate line highlighting working so I thought I'd post it here.

 

$x = 1;

foreach ($contents as $nestedArray) {

    if ($nestedArray[0] == $prev){
if($x % 2) {
    $content .= "<tr class=row1>";
} else {
    $content .= "<tr class=row2>";
}
$nestedArray[0] = $nestedArray[4] = '';
foreach ($nestedArray as $value) {
    $content .= "<td>" . $value . "</td>\n";
}
$content .= "</tr>\n";
    } else {
$x++;
if($x % 2) {
    $content .= "<tr class=row1>";
} else {
    $content .= "<tr class=row2>";
}
foreach ($nestedArray as $value) {
    $content .= "<td>" . $value . "</td>";
}
$content .= "</tr>\n";
$prev = $nestedArray[0];
    }
}

 

Cheers, Wi||

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.