Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378299
Share on other sites

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]

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378314
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-378344
Share on other sites

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;
     ....
    ...
}

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379377
Share on other sites

<?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
    
}
?>

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379394
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379397
Share on other sites

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";}
?>

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379421
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379428
Share on other sites

<?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";}
?>

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379429
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379445
Share on other sites

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";
   }
}

Link to comment
https://forums.phpfreaks.com/topic/74805-cd-search-helppp/#findComment-379454
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.