gamze Posted October 25, 2007 Share Posted October 25, 2007 hi everybody, Im new on php and i realy need help. i have to do 4 part and i done 1 part. Its about cd search. first part was display cds without comma and second part is, allow the user to enter a search term and web page will display the full details of all the cds containg the users search term included in part of any field. so far i only have what the user types in to search it is then displayed... i need a code that will compare what is entered in to the items in the text file how do i write this?? Thanks php code #!/usr/bin/php <html> <head> <title> CDs Search </title> <body> <? #cdlist.php #read cds text file outputs it to screen $filename = "cds.txt"; $filepointer = fopen($filename,"r"); // open for read $myarray = file ($filename); if(isset($_POST['names'])){ $fname=$_POST['names']; print "$fname <br>"; } for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time $aline =$myarray[$mycount]; $details = $aline; $name = getvalue($details,0); print " $name "; $album =getvalue($details,1); print "$album"; $year = getvalue($details,2); print "$year"; $price= getvalue($details,3); print "$price"; $stock = getvalue($details,4); print "$stock"."<br>"; } if ($fname==$name) { print"$name"; print"$album"; print"$year"; print"$price"; print"$stock"; } function getvalue ($file, $commaToLookFor) { $myarray= explode(",",$file); return $myarray [ $commaToLookFor]; } fclose ($filepointer); //if (!($filepointer = fopen($filename,"r"))) { exit; } ?> </body> </html> HTML code <html> <head> <title> CD list </title> </head> <body> Please enter Artist Name <br/> <FORM ACTION="cdsearch.php" METHOD="POST"> Artist Name<input type="text" name="names" size="30"/> </br> <input type="submit" name="submit" value="Search"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/ Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 Homework assignment? Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378282 Share on other sites More sharing options...
gamze Posted October 25, 2007 Author Share Posted October 25, 2007 yess cw:( Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378287 Share on other sites More sharing options...
Barand Posted October 25, 2007 Share Posted October 25, 2007 We don't do those. Wouldn't do you any favours in the long term. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378292 Share on other sites More sharing options...
gamze Posted October 26, 2007 Author Share Posted October 26, 2007 i know you cant help cw or assigmnets. I only need 1 part of the code i done some code (i sended) and it seems right. i just need to know what i am doing wrong because i cant get right result. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378297 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 I don't see any code that tries to search for a value. You might get a clue from this http://www.phpfreaks.com/forums/index.php/topic,165025.msg725781.html#msg725781 And to see if the $search string exists in the artist name or the album name use http://us3.php.net/strpos Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378299 Share on other sites More sharing options...
gamze Posted October 26, 2007 Author Share Posted October 26, 2007 how do u find a word from a grounp text file? Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378302 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 Assuming you file is something like [pre] artist, album, year, price, stock Amadeus String Quartet,Selection of Chamber Music,1975,6.50,10 London Symphony Orchestra,Serenade for Strings,1995,8.99,2 BBC Symphony Orchestra,Mozart Symphony 40,2002,6.99,8 [/pre] search each line as you read it and output if the seach is successful <?php $filename = 'cd.txt'; $search = 'String' ; $fp = fopen ($filename, 'r'); echo '<table border="1">'; while (list($artist, $album, $year, $price, $stock) = fgetcsv($fp, 1024)) { if (strpos($artist, $search)!== false || strpos($album, $search)!== false) { echo "<tr><td>$artist</td><td>$album</td><td>$year</td><td>$price</td><td>$stock</td></tr>"; } } echo '</table>' ?> --> [pre] Amadeus String Quartet | Selection of Chamber Music | 1975 | 6.50 | 10 London Symphony Orchestra | Serenade for Strings | 1995 | 8.99 | 2 [/pre] Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378314 Share on other sites More sharing options...
gamze Posted October 26, 2007 Author Share Posted October 26, 2007 I added your code and changed but i didnt use table and echo. And i still got same result nothing change it ? #cdlist.php #read cds text file outputs it to screen $filename = "cds.txt"; $filepointer = fopen($filename,"r"); // open for read $myarray = file ($filename); $search='String'; if(isset($_POST['names'])){ $fname=$_POST['names']; print "$fname <br>"; } for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time $aline =$myarray[$mycount]; $details = $aline; $name = getvalue($details,0); print " $name "; $album =getvalue($details,1); print "$album"; $year = getvalue($details,2); print "$year"; $price= getvalue($details,3); print "$price"; $stock = getvalue($details,4); print "$stock"."<br>"; } while(list($artist,$album,$year,$price,$stock)=fgetcsv($filepointer,1024)) { if(strpos($artist,$search)!== false||strpos($album,$search)!== false) { print"$artist"."$album"."$year"."$price"."$stock"; } } function getvalue ($file, $commaToLookFor) { $myarray= explode(",",$file); return $myarray [ $commaToLookFor]; } function getitem($look,$enter) { substr($look,$enter,""); } fclose ($filepointer); //if (!($filepointer = fopen($filename,"r"))) { exit; } ?> </body> </html> i entered in search box "Tweet". ----> it printed that Tweet Aaliyah One in a million 1996 12.99 44 Tweet Southern Hummingbird 2000 10.99 20 Mariah Carey Emancipation of Mimi 20058.99 12 Missy Elliott Da real world 19999.99 10 Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378344 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 Which only goes to show there are are some you can help and some you can't. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378345 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 Hi again, I said, i am new on php at begin. If i know something or if i understand codes. I dont need help anyways i done some part. Now i can search only artist name and when i click search i am getting the detail, now i am need 1 if statemnt which its need to look all. I try "and", "or" function but still i cant get result can you help me for IF statement thanks. $fname=$_POST['names']; } for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time $aline =$myarray[$mycount]; $details = $aline; $name = &getvalue($details,0); $album = &getvalue($details,1); $year = &getvalue($details,2); $price = &getvalue($details,3); $stock = &getvalue($details,4); if ($name==$fname) { $name = getvalue($details,0); $display=$name; print $display; .... ... } Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379377 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 im not sure what your saying? can u be more clear? ??? Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379387 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 ok (sorry for my english) now, I am getting full details for only when i entered artist name, I need another IF statement when I enter album, price or stock so the web page will display the full details of all the cds containg. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379391 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 <?php $name = &getvalue($details,0); $album = &getvalue($details,1); $year = &getvalue($details,2); $price = &getvalue($details,3); $stock = &getvalue($details,4); $array= array($name,$album,$year,$price,$stock); if (in_array($name,$album,$year,$price,$stock)| $name=$fname|$album=$falbum| $year=$fyear|$price=$fprice|$stock=$fstock) { $display=$array; print $display; // prints the entire array } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379394 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 thanks I changed it but i got this error : <b>Warning</b>: Wrong parameter count for in_array() in <b> on line <b>40</b><br /> Array<br><br /> my full code is : #!/usr/bin/php <html> <head> <title> CDs Search </title> <body> <? #cdlist.php #read cds text file outputs it to screen $filename = "cds.txt"; $filepointer = fopen($filename,"r"); // open for read $myarray = file ($filename); if(isset($_POST['names'])){ $fname=$_POST['names']; } for ($mycount = 0; $mycount < count($myarray); $mycount++ ) { // one input line at a time $aline =$myarray[$mycount]; $details = $aline; $name = &getvalue($details,0); $album = &getvalue($details,1); $year = &getvalue($details,2); $price = &getvalue($details,3); $stock = &getvalue($details,4); $array= array($name,$album,$year,$price,$stock); if (in_array($name,$album,$year,$price,£stock)| $name=$fname |$album=fname | $year=$fname | $price=$fprice | $stock=$fname) { $display=$array; print $display."<br>"; //$name = getvalue($details,0); //$display=$name; //print $display; //$album =getvalue($details,1); //$display=$album; //print $display; //$year = getvalue($details,2); //$display=$year; //print $display; //$price= getvalue($details,3); //$display=$price; //print $display; //$stock = getvalue($details,4); //$display=$stock; //print $display."<br>"; } } function getvalue ($file, $commaToLookFor) { $myarray= explode(",",$file); return $myarray [ $commaToLookFor]; } fclose ($filepointer); if (!($filepointer = fopen($filename,"r"))) { exit; } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379397 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 slight error sorry Try this: <?php if (in_array(array($name,$album,$year,$price,$stock),$array)| $name=$fname |$album=$fname | $year=$fname | $price=$fprice | $stock=$fname) { $display=$array; print $display;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379401 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 It doesnt display anything even artist name ??? Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379413 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 <?php if (in_array(array($name,$album,$year,$price,$stock),$array,true)| $name=$fname |$album=$fname | $year=$fname | $price=$fprice | $stock=$fname) { print $array; }else{ echo "Error: could not return the array";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379415 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 now its printing me that : ArrayStereophonics Pull The Pin 2007 £9.97 25 ArraySugababes change 2005 £15.99 18 ArrayJennifer Lopez Brave 2001 £4.99 52 ArrayBruce Springsteen Magic 2001 £10.99 29 ArrayEric Clapton Complete Clapton 2000 £6.88 24 Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379418 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 what did u want then? <?php if (in_array(($name,$album,$year,$price,$stock),$array,true)| $name=$fname |$album=$fname | $year=$fname | $price=$fprice | $stock=$fname) { print $array; }else{ echo "Error: could not return the array";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379421 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 Sorry i think u dont understand what i mean. If you read begging of the this topic there is my question and there is my html form. This html form include a box and button u need to enter any cd detail and it will give all detail. like if you enter: Jennifer lopez (artist name) it will give you: Jennifer Lopez Brave 2001 £4.99 52 but when you enter: Brave (album) or (price),(stock),(year) it doesnt give me any thing. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379428 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 <?php if (in_array(($name,$album,$year,$price,$stock),$array,true)| $name=$fname |$album=$fname | $year=$fname | $price=$fprice | $stock=$fname) { unset ($array); print "Name:$array[$name]"; print "Album:$array[$album]"; print "Year:$array[$year]"; print "Price:$array[$price]"; print "In Stock:$array[$stock]"; }else{ echo "Error: could not return the array";} ?> Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379429 Share on other sites More sharing options...
gamze Posted October 27, 2007 Author Share Posted October 27, 2007 No, i doesnt work anyway thanks for your help i will try myself. thank you very much again Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379435 Share on other sites More sharing options...
darkfreaks Posted October 27, 2007 Share Posted October 27, 2007 i was thinking if that one if statement searching for everything then you can do the same thing only less and statements like <?php if (in_array(array($name,$album,$year,$price,$stock),$array) |$album=$fname | $year=$fname | $price=$fprice | $stock=$fname) { $display=$array; print $display;} ?> and keep messing with the and's |'s until you have several if statements like this that search for specific things. Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379445 Share on other sites More sharing options...
marcus Posted October 27, 2007 Share Posted October 27, 2007 darkfreaks you've got a mess there. | should be || to specify you're wanting to use the PHP Operator "OR" = should be == to specify you're wanting to use the PHP Operator "is equal to" $yourArray = array($name, $album, $year, $price, $stock); if(in_array($yourArray,$array) || $album == $fname || $year == $fname || $price == $fprice || $stock == $fname){ $display[] = $array; foreach($display AS $displays){ echo $displays . "<br>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379454 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.