473,394 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Extracting unique values ??

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

Apr 7 '07 #1
1 1907
je***********@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);
?>

Apr 7 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kevin parks | last post by:
hi. I've been banging my head against this one a while and have asked around, and i am throwing this one out there in the hopes that some one can shed some light on what has turned out to be a...
26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
5
by: Marie | last post by:
Access97 I have a table containing addresses with a separate field for State. Is there a way to create a query that returns an unique list of the states in that table and still be updateable? I...
0
by: Bubbles | last post by:
Hello. New to ASP.NET and struggling on this one. I have a text file with a bunch of text in it. Throughout the file words followed by a ":" will appear. I need to pull every such string out...
4
by: apatel85 | last post by:
Hey Guys, Total Number of Records (Based on 5 fields): 1000 Total Unique Records (Based on 5 Fields): 990 Total number of fields: 5 I have question regarding extracting duplicates from the...
1
by: yasmine | last post by:
Hi friends, Can anyone tell me how can i write a php script which extract all the unique values of a field from multiple tables? and also if anybody enters some new values it also should be...
2
by: lunabelle22a | last post by:
Hi, I have 125 unique variables and corresponding values in a data file with 6 fixed-width columns (columns alternate between "variables" and "values"). If I only need 6 of these variables and...
6
by: Alvin SIU | last post by:
Hi all, I have a table in Db2 v8 like this: Team Name Role ------ -------- --------------------- A Superman Leader A Batman Member A WonderWoman Member B ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.