hi, im trying to search data through an array and then display the macthes found
please help
[PHP]
#!/usr/bin/php
<HTML> <BODY>
<?
#reads a text file outputs it to screen
$filename = "cdlist.txt";
$filepointer = fopen($filename,"r"); // open for read
$cdArray = file ($filename);
if (!(isset($_POST['usersearch']))) {exit;}
$search = $_POST['usersearch'];
print "User typed in $search <br>";
for ($newcount = 0; $newcount < count ($cdArray); $newcount++)
{
//$newstring = getitem ($search,0);
$newaline = $cdArray [$newcount];
$newstring = getitem ($search,0);
//print "<pre>";
//print_r ($cdArray);
//print "</pre>";
print $cdArray[1][7];
// if ($newstring == $cdArray[0][3]
// { print "$newaline \n $newastring <br>";}
}
exit;
fclose ($filepointer);
function getitem ($text, $spaceNmber)
{
return substr ($text, $spaceNumber, $spaceNumber + 2);
}
if (!($filepointer = fopen($filename,"r"))) { exit; }
?>
</body></html>
[/PHP]
text file data....
Artist, Title, Year, Price, StockLevel
Usher, The Masterpiece, 2007, 9.99, 1
snoop, dog pound, 2000, 10.99, 4
Snoop Dogg, Doggystyle, 1993,12.99, 6
Dr. Dre, 2001, 1999, 12.99, 10
2 3975
ok, so your reading all the data into an array from a text file (not a good way to do it, databases are sooo much easier but each to their own)
your best bet is a multidimensional array so read in one line at a time and upon reading it in, pass it through the function preg_split()
there are two ways to sort your data, read in the entire file and split per line and then per item of read in line by line and split the data, personally i would choose the line by line
so we have something like this
[PHP]
$filename = "cdlist.txt";
$filepointer = fopen($filename,"r"); // open for read
while($filepointer){
$cdArray = preg_split("/, /", fgets($filepointer)); //reads in line and splits
}
[/PHP]
so this will take read in a line using fgets, pass it to the preg_split function which will split the data every time it finds a comma and space in the line so we have
[PHP]
$cdArray = array(
[0] => array(
[0] => 'Usher'
[1] => 'The Masterpiece'
[2] => '2007'
[3] => '9.99'
[4] => '1'
)
etc etc for each line
[/PHP]
so to get the artist...
[PHP]echo $cdArray[0][0];[/PHP]
to get the album name and the year...
[PHP]echo $cdArray[0][1]." ".$cdArray[0][2];[/PHP]
now to search through it you can simply match artist to the search input using a for loop like so...
[PHP]
for ($i = 0; $i < count($cdArray); $i++){
if($cdArray[$i][0] == $search){
echo "Match: Artist: ".$cdArray[$i][0]."<br />";
echo " Album: ".$cdArray[$i][1]."<br />";
}
}
[/PHP]
This is a really simple way to do it and it just matches the artist and displays their name beside the albums they made and only if the user inputs the exact artists name, you can add elseif statements to search through other attributes and use functions for making text capitalised or first letter capitalised for slightly more advanced search, to do anything more than that we have to use preg_match functions and regular expressions (love them!) and then dynamic self writing regular expressions! (so much fun!)
hope this helps a bit!
thanks...much apprecited..i think i get some idea now
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Anand Pillai |
last post by:
To search a word in a group of words, say a paragraph or a web page,
would a string search or a regexp search be faster?
The string search would of course be,
if str.find(substr) != -1:...
|
by: Tarique Jawed |
last post by:
Alright I needed some help regarding a removal of a binary search
tree. Yes its for a class, and yes I have tried working on it on my
own, so no patronizing please. I have most of the code working,...
|
by: jdworley |
last post by:
Howdy!
The search is a 1 GB+ file from a crash to be exact - had an
e-mail correspondence (window) hopefully in it so I saved
the file on reboot before windoze kicked in and want to open
and...
|
by: Julie |
last post by:
What is the *fastest* way in .NET to search large on-disk text files (100+ MB)
for a given string.
The files are unindexed and unsorted, and for the purposes of my immediate
requirements, can't...
|
by: Andrew |
last post by:
Hi, im trying to create a small function which can create a binary tree
from the entries in a text file and after that we can perform all the
usual operations like search, insert and delete etc....
|
by: Mark |
last post by:
Hi
I have an application (in vb.NET 2005) which holds data in SQL Server and
some of the SQL records are simply paths to related files.
I would like to be able to do a text search on both the...
|
by: Roopesh |
last post by:
I am using the imaplib to fetch mails. There is an error thrown in the
search function, which I am not able to comprehend.
My program is as follows :
====================
import imaplib
M =...
|
by: 848lu |
last post by:
hi, im trying to do a search on my Array, where a users searches on a HTML screen and then the PHP searches the array for the data under the roice entred by user.....text file data is under the php...
|
by: JamesOo |
last post by:
I have the code below, but I need to make it searchable in query table,
below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
|
by: Debadatta Mishra |
last post by:
Introduction
In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |