473,770 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1935
je***********@y ahoo.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($use r_array[$name]["accesses"],
array("domain"= >$domain, "date"=>$date)) ;
}

// output array details
foreach ($user_array as $username=>$det ails) {
echo "<tr>";
echo "<td colspan='2' align='left' style='font-weight: bold;
background-color: #fafafa'>$usern ame</td>";
echo "</tr>";
foreach($detail s["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_arr ay);
?>

Apr 7 '07 #2

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

Similar topics

2
2303
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 tough problem for me (though i am getting closer). i have been mucking with a lot of data in a dictionary that looks like:
26
45447
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.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
5
2956
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, I have used individual entry fields for each variable. I would now like to use text area boxes to simplify the data entry (this way, data can be produced by another program--FORTRAN, "C", etc.--but analyzed online, so long as it is first...
5
1995
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 tried setting the unique values property to Yes but that gave me a recordset that was not updateable. Thanks! Marie
0
1200
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 and then display all unique values. I am able to read the file in, but am having trouble getting regex to work. What regex and setting should I use?
4
3023
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 dataset. I have 2 fields that makes a record unique. I have used group by function to find duplicates and got 10 records that are duplicating. Each records duplicating 1 times, thus, 10 unique records and 10
1
1331
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 stored in the unique table. That is exactly what i meant is that consider different category of courses offered and stored in different tables. Now, i need to extract all the unique courses with its id and keep it in a separate table. I know this...
2
1446
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 their values, which are scattered throughout the file, what are some suggestions to put into my script in order to extract them and them only? Is there a way to locate specific keys and ignore the others? Thanks for your help!
6
4320
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 Alvin Leader
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10257
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10037
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8931
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.