|
Posted by Tyno Gendo on 04/07/07 23:23
jerryyang_la1@yahoo.com wrote:
> I'm reading in a CSV and displaying using the code below:
>
> $users = file("text.txt");
> echo "<table border='1' width='75%' align='center'>";
> echo "<tr>";
> echo "<td width='33%' align='center'><b>Username</b></td>";
> echo "<td width='33%' align='center'><b>Domain Name</b></td>";
> echo "<td width='34%' align='center'><b>Date & Time</b></td>";
> echo "</tr>";
> foreach ($users as $user) {
> list($name, $domain, $date) = explode(",", $user);
> echo "<tr>";
> $result = count($users);
> if ($domain == " test.com"){
> echo "<td width='33%' align='center'>$font2$name</td>";
> echo "<td width='33%' align='center'>$font2$domain</td>";
> echo "<td width='34%' align='center'>$font2$date</td>";
> } else {
> echo "<td width='33%' align='center'>$font$name</td>";
> echo "<td width='33%' align='center'>$font$domain</td>";
> echo "<td width='34%' align='center'>$font$date</td>";
> }
> echo "</tr>";
> }
>
> This works fine, except I would like to return the unique value for
> $name, can some one help ?
>
> The CSV is:
>
> username, domain, date
> user1, test.com, 01/02/2007
>
>
> Thanks
>
How big is this CSV going to get? If its going to get big you'd be
better with a database as you're reading the entire file into memory.
Anyhow, not quite sure if I'm on the right lines for what you want, but
I think you want to group the data for a user together even though the
entries may be in a random order in the file?
I'm pretty new to PHP myself so don't have a grasp of all the functions,
so here is a version of what I think you want to do, but there may be a
much better way ;-)
<?php
$users = file("text.txt");
echo "<table style='border: 1px solid #000;' width='75%'
align='center'>";
echo "<tr>";
echo "<td width='50%' align='center'><b>Domain Name</b></td>";
echo "<td width='50%' align='center'><b>Date & Time</b></td>";
echo "</tr>";
// initial CSV parse into usable array
$user_array = array();
foreach ($users as $user) {
list($name, $domain, $date) = explode(",", $user);
if(!isset($user_array[$name]["accesses"])){
$user_array[$name]["accesses"]=array();
}
array_push($user_array[$name]["accesses"],
array("domain"=>$domain, "date"=>$date));
}
// output array details
foreach ($user_array as $username=>$details) {
echo "<tr>";
echo "<td colspan='2' align='left' style='font-weight: bold;
background-color: #fafafa'>$username</td>";
echo "</tr>";
foreach($details["accesses"] as $accesses) {
echo "<tr>";
echo "<td width='50%'
align='center'>{$accesses["domain"]}</td>";
echo "<td width='50%' align='center'>{$accesses["date"]}</td>";
echo "</tr>";
}
}
unset($user_array);
?>
Navigation:
[Reply to this message]
|