hey guys, Kinda wondering on how I should go about doing this. I have a txt file which I want to read in the txt file it has 501# A potion made from ground Red Herbs that restores ^000088about 45 HP^000000. ^ffffff_^000000 Weight :^777777 7^000000 # how would I go about pulling the info via the id 501 there is a database with these ID's to match but no description field. if you could help me that would be great I have tried $file = file_get_contents('idnum2itemdesctable.txt'); //$match = $row->id; preg_match('~506#[^#]*#~s',$file,$match); Code (markup): With no luck. This does what I want it to but its at random and I would like it to post by the ID //$file = file_get_contents('idnum2itemdesctable.txt'); //$array = explode("". $row->id ."", $file); //$random = rand(1 + 6 - 5, count($array)); //$desc = $array[$random]; Code (markup): This is all of my code <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="ro_itemdb"; // Database name // Connect's to MySQL with MySQL code mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="text/css"> #outer { height:10px;width:200px;background:#000;padding:0; } #inner { height:10px;background:#090;margin:0;padding:0; } </style> <?php if($_REQUEST['ln']) $ln=$_REQUEST['ln']; ?> <title>ShmaRO - We're just hotter.</title> <link type="text/css" rel="stylesheet" href="pyeus.css" /> <?php include('checkonline.php'); ?> </head> <body> <h1><img src="logo.png" alt="ShmaRO" /></h1> <div id="underscore"><a href="join.php"> <?php if($ln=='es') echo "Conectados:"; else echo "We're online:"; ?></a></div> <?php if($ln=='es') include 'esmenu.php'; else include 'menu.php'; ?> <?php if($ln=='es') include 'esvote.php'; else include 'vote.php'; ?> <div id="body"><br /><br /><br /><br /><Br /><br /><br /> <center> <?php echo "Overall Progress of the Search Engine"; $finish = '50'; $width = '100'; $overall = $finish / $width; echo '<div id="outer"> <div id="inner" style="width:'.$finish.'%;"> </div> </div>'; ?> <a href="search.php?p=Item">Item Search</a> | <a href="search.php?p=Monster">Monster Search</a> <br /> <fieldset ><legend>ShamRo Item Search Engine</legend> <?php $p = $_GET['p']; if($p == 'Item'){ echo " <FORM NAME=Search ACTION=search.php?p=Item METHOD=POST> <INPUT TYPE=TEXT NAME=searchString> <INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=search> </FORM> "; $Limit = 2; $searchString = $_POST["searchString"]; $searchString = stripslashes($searchString); If($searchString == "") $searchString=$_GET["searchString"]; If($searchString == "") { exit(); } $page = $_GET["page"]; //Get the page number to show If($page == "") $page=1; //If no page number is set, the default page is 1 $searchResult=mysql_query("SELECT * FROM item_db WHERE name_japanese LIKE '%$searchString%' ORDER BY id") or die(mysql_error()); $NumberOfResults=mysql_num_rows($searchResult); $NumberOfPages=ceil($NumberOfResults/$Limit); $searchResult=mysql_query("SELECT * FROM item_db WHERE name_japanese LIKE '%$searchString%' ORDER BY id LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error()); While($row = mysql_fetch_object($searchResult)) { //$file = file_get_contents('idnum2itemdesctable.txt'); //$array = explode("". $row->id ."", $file); //$random = rand(1 + 6 - 5, count($array)); //$desc = $array[$random]; $file = file_get_contents('idnum2itemdesctable.txt'); //$match = $row->id; preg_match('~506#[^#]*#~s',$file,$match); //$filename="idnum2itemdesctable.txt"; //$fh=fopen($filename,'r'); //r means we are opening file only for reading //$textinfile = fread($fh, filesize($filename)); //fread which takes file handle and file size, reading the file data //preg_match('506#',$filename, $textinfile, $match); //fclose($fh); //script closes file //$textbody = "This book is *very* difficult to find."; //$word = "*very*"; //$textbody = preg_replace ("/" . preg_quote($word) . "/", // "<i>" . $word . "</i>", // $textbody); echo "<table width=200 border=0 cellpadding=0 cellspacing=0 bgcolor=#acc6bd> <!--DWLayoutTable--> <tr> <td width=390 height=185><table border=1 cellspacing=0 cellpadding=0 width = '200px'> <tr class = 'lmd'> <td class = 'bborder' align = 'left' colspan=10> <b> ". $row->name_japanese ." </b> Item ID# ". $row->id ." (". $row->name_japanese .")</td> </tr><tr> <th class='lmd' align='left' width=105>Type</th> <td class='bb' align='right' width=100>coming soon</td> <th class='lmd' align='left' width=70>Class</th> <td class = 'bb' align='right' width=105>coming soon</td> <th class='lmd' align='left' width=70>Buy</th> <td class = 'bb' align='right' width=50>". $row->price_buy ."</td> <th class='lmd' align='left' width=70>Sell</th> <td class = 'bb' align='right' width=50>". $row->price_sell ."</td> <th class='lmd' align='left' width=70>Weight</th> <td class = 'bb' align='right' width=30>". $row->weight ."</td> </tr><tr><th class='lmd' align='left' valign='top'>Description</th> </td><td colspan=9 class='bb' valign='top'>". $match ."</td></tr> <tr><th class='lmd' align='left'>Item Script</th><td colspan=9 class='bb'>". $row->script ."</td></tr> <tr><th class='lmd' align='left'>Dropped By</th><td colspan=9 class='bb'>coming soon</td></tr> <tr><th class='lmd' align='left'>Buyable At</th><td colspan=9 class='bb'>coming soon</td></tr><tr><th class='lmd' align='left'>Obtained from</th><td colspan=9 class='bb'>coming soon</td></tr> </table></td> </tr> </table> <BR>"; } $Nav=""; If($page > 1) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page-1) . "&searchString=" .urlencode($searchString) . "\"> << Prev </A>"; } For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $page) { $Nav .= "<B>[$i]</B>"; }Else{ $Nav .= " <A HREF=\"search.php?p=Item&page=" . $i . "&searchString=" .urlencode($searchString) . "\">[$i] </A>"; } } If($page < $NumberOfPages) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page+1) . "&searchString=" .urlencode($searchString) . "\"> Next >></A>"; } Echo "<BR><BR>" . $Nav; echo "</legend></fieldset>"; } ?> <?php if($p == 'Monster'){ echo "Monster Database search Engine <FORM NAME=Articlessearch ACTION=search.php?p=Monster METHOD=POST> <INPUT TYPE=TEXT NAME=searchString> <INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=search> </FORM> "; $Limit = 20; $searchString = $_POST["searchString"]; $searchString = stripslashes($searchString); If($searchString == "") $searchString=$_GET["searchString"]; If($searchString == "") { exit(); } $page=$_GET["page"]; //Get the page number to show If($page == "") $page=1; //If no page number is set, the default page is 1 $searchResult=mysql_query("SELECT * FROM mob_db WHERE kName LIKE '%$searchString%' ORDER BY id") or die(mysql_error()); $NumberOfResults=mysql_num_rows($searchResult); $NumberOfPages=ceil($NumberOfResults/$Limit); $searchResult=mysql_query("SELECT * FROM mob_db WHERE kName LIKE '%$searchString%' ORDER BY id LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error()); While($row = mysql_fetch_object($searchResult)) { echo "<strong>Name:</strong> ". $row->kName . " <strong>Level:</strong> " . $row->LV . " <strong>HP:</strong> ". $row->HP ."<BR>"; } $Nav=""; If($page > 1) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page-1) . "&searchString=" .urlencode($searchString) . "\"> << Prev </A>"; } For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $page) { $Nav .= "<B>[$i]</B>"; }Else{ $Nav .= " <A HREF=\"search.php?p=Item&page=" . $i . "&searchString=" .urlencode($searchString) . "\">[$i] </A>"; } } If($page < $NumberOfPages) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page+1) . "&searchString=" .urlencode($searchString) . "\"> Next >></A>"; } Echo "<BR><BR>" . $Nav; } ?> <?php // get contents of a file into a string $filename = "idnum2itemdesctable.txt"; $handle = fopen($filename, "rb"); $contents = fread($handle, filesize($filename)); fclose($handle); ?> </center> <?php include('end.php'); ?> </div> </body> </html> Code (markup):
Well i think you should do something similar if i got your question right of course <?php $filename = "idnum2itemdesctable.txt"; $file = file($filename); $lines = count($file); for ($line=0; $line<=$lines; $line++){ $final_line = explode(",", $file[$line]); } ?> Code (markup): Now the cell id will equal '$final_line[0]' and you can compare between them easily, i hope i helped i just waked up and your post in DH and facebook was the first thing to see so in case i got your question wrong or you needed any further help just leme know Regards, Canadian Spike
Well what I need it to do is print everything between 501# to # it is all like this and in the database the items etc... have the same ID as this being the ID for 501 is Red Potion and I need it to get the description from the text file so it will have to find the ID in the text file then print the description from that text file. I can get it to print Array, but that is not what I want. umm this site is what I need it to do they are doing the same thing. ratemyserver[dot]net/index.php?page=item_db&item_id=501&iname=&itype=-1&islot_sign=-1&islot=-1&ij[+]=-1&idesc=&iscript=&i_ele=-1&i_status=-1&i_race=-1&i_bonus=-1&icfix=&sort_r=0&sort_o=0&isearch=Search they are using the text file to get the description of the item by the ID. 501# A potion made from ground Red Herbs that restores ^000088about 45 HP^000000. ^ffffff_^000000 Weight :^777777 7^000000 # 502# A potion made from ground Red and Yellow Herbs that restores ^000088about 105 HP^000000. ^ffffff_^000000 Weight :^777777 10^000000 # 503# A potion made from ground Yellow Herbs that restores ^000088about 175 HP^000000. ^ffffff_^000000 Weight :^777777 13^000000 # 504# A potion made from ground White Herbs that restores ^000088about 325 HP^000000. ^ffffff_^000000 Weight :^777777 15^000000 # 505# A potion made from ground Blue Herbs that restores ^000088about 60 SP^000000. ^ffffff_^000000 Weight :^777777 15^000000 # is a example of the layout as you can see the id's as 501# threw 505# well I need all the info between 501# to the following # any clue on what to do
//$file = file_get_contents('idnum2itemdesctable.txt'); //$array = explode("". $row->id ."", $file); //$random = rand(0, count($array)); //$desc = $array[$random]; Code (markup): this work's but it displays at random. how would I make it so it does not display at random?
There are a few ways to do this. One way is to read through the file using fread function. Read in some text and do a strpos() search with it. You can do that or you can use the file() function which will read each line of a file into an array and then from there you can loop through the array looking for the id number or simply use array_search() to look for the value. <?php $arrayOfLines = file("idnum2itemdesctable.txt"); if (array_search("501#",$arrayOfLines)) { // Item was found in array, the rest of your code goes below } ?> Code (markup): The idea is that once you get it into an array you can do all sorts of searching on it. You could array_walk() it with your own function, you could conduct search algorithms on it..etc Regards, Canadian Spike
Try the following... Keep in mind...all custom code by me. So if it works, glad to be of service. Included example usage. <?PHP $Data=file_get_contents('idnum2itemdesctable.txt'); $DataArray=explode("\n#",str_replace('\r\n','\n',$Data)); array_pop($DataArray); // Go back one. $List=ProcessList($DataArray); /* Example of usage */ echo $List[501]; /* End of example */ /* Process the list! */ function ProcessList($Data) { $List=array(); if(!is_array($Data)) return array(); foreach($Data as $DataItem) { $FirstPound=strpos($DataItem,'#'); $Num=intval(substr($DataItem,0,$FirstPound)); if(is_numeric($Num)) $List[$Num]=substr($DataItem,$FirstPound+1); } return $List; } ?> PHP:
another work around is to store the text in the .txt file with $var1 ="501 = blah blah blah"; $var2 ="502 = yadda yadda"; etc fopen and read the file, turn it into variables into PHP . and then use them by $var name.
well none of it worked. opensvn.csie[dot]org/EnglishTranslation/English/idnum2itemdesctable.txt is the txt file, that I have dled and I cant seem to get anything to work. This is the whole code for the section you will see I have diff ones commented out. Ones that I have tried. how do I go about echoing it? I see you have echo $List[501]; but that dosnt return anything. <?php $p = $_GET['p']; if($p == 'Item'){ echo " <FORM NAME=Search ACTION=search.php?p=Item METHOD=POST> <INPUT TYPE=TEXT NAME=searchString> <INPUT TYPE=SUBMIT NAME=SUBMIT VALUE=search> </FORM> "; $Limit = 2; $searchString = $_POST["searchString"]; $searchString = stripslashes($searchString); If($searchString == "") $searchString=$_GET["searchString"]; If($searchString == "") { exit(); } $page = $_GET["page"]; //Get the page number to show If($page == "") $page=1; //If no page number is set, the default page is 1 $searchResult=mysql_query("SELECT * FROM item_db WHERE name_japanese LIKE '%$searchString%' ORDER BY id") or die(mysql_error()); $NumberOfResults=mysql_num_rows($searchResult); $NumberOfPages=ceil($NumberOfResults/$Limit); $searchResult=mysql_query("SELECT * FROM item_db WHERE name_japanese LIKE '%$searchString%' ORDER BY id LIMIT " . ($page-1)*$Limit . ",$Limit") or die(mysql_error()); While($row = mysql_fetch_object($searchResult)) { //$file = file_get_contents('idnum2itemdesctable.txt'); //$array = explode("". $row->id ."", $file); //$random = rand(0, count($array)); //$desc = $array[$random]; $Data=file_get_contents('idnum2itemdesctable.txt'); $DataArray=explode("\n#",str_replace('\r\n','\n',$Data)); array_pop($DataArray); // Go back one. $List=ProcessList($DataArray); /* Example of usage */ /* End of example */ /* Process the list! */ function ProcessList($Data) { $List=array(); if(!is_array($Data)) return array(); foreach($Data as $DataItem) { $FirstPound=strpos($DataItem,'#'); $Num=intval(substr($DataItem,0,$FirstPound)); if(is_numeric($Num)) $List[$Num]=substr($DataItem,$FirstPound+1); } return $List; } //$filename="idnum2itemdesctable.txt"; //$fh=fopen($filename,'r'); //r means we are opening file only for reading //$textinfile = fread($fh, filesize($filename)); //fread which takes file handle and file size, reading the file data //preg_match('506#',$filename, $textinfile, $match); //fclose($fh); //script closes file //$textbody = "This book is *very* difficult to find."; //$word = "*very*"; //$textbody = preg_replace ("/" . preg_quote($word) . "/", // "<i>" . $word . "</i>", // $textbody); echo "<table width=200 border=0 cellpadding=0 cellspacing=0 bgcolor=#acc6bd> <!--DWLayoutTable--> <tr> <td width=390 height=185><table border=1 cellspacing=0 cellpadding=0 width = '200px'> <tr class = 'lmd'> <td class = 'bborder' align = 'left' colspan=10> <b> ". $row->name_japanese ." </b> Item ID# ". $row->id ." (". $row->name_japanese .")</td> </tr><tr> <th class='lmd' align='left' width=105>Type</th> <td class='bb' align='right' width=100>coming soon</td> <th class='lmd' align='left' width=70>Class</th> <td class = 'bb' align='right' width=105>coming soon</td> <th class='lmd' align='left' width=70>Buy</th> <td class = 'bb' align='right' width=50>". $row->price_buy ."</td> <th class='lmd' align='left' width=70>Sell</th> <td class = 'bb' align='right' width=50>". $row->price_sell ."</td> <th class='lmd' align='left' width=70>Weight</th> <td class = 'bb' align='right' width=30>". $row->weight ."</td> </tr><tr><th class='lmd' align='left' valign='top'>Description</th> </td><td colspan=9 class='bb' valign='top'>". $List ."</td></tr> <tr><th class='lmd' align='left'>Item Script</th><td colspan=9 class='bb'>". $row->script ."</td></tr> <tr><th class='lmd' align='left'>Dropped By</th><td colspan=9 class='bb'>coming soon</td></tr> <tr><th class='lmd' align='left'>Buyable At</th><td colspan=9 class='bb'>coming soon</td></tr><tr><th class='lmd' align='left'>Obtained from</th><td colspan=9 class='bb'>coming soon</td></tr> </table></td> </tr> </table> <BR>"; } $Nav=""; If($page > 1) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page-1) . "&searchString=" .urlencode($searchString) . "\"> << Prev </A>"; } For($i = 1 ; $i <= $NumberOfPages ; $i++) { If($i == $page) { $Nav .= "<B>[$i]</B>"; }Else{ $Nav .= " <A HREF=\"search.php?p=Item&page=" . $i . "&searchString=" .urlencode($searchString) . "\">[$i] </A>"; } } If($page < $NumberOfPages) { $Nav .= " <A HREF=\"search.php?p=Item&page=" . ($page+1) . "&searchString=" .urlencode($searchString) . "\"> Next >></A>"; } Echo "<BR><BR>" . $Nav; echo "</legend></fieldset>"; } ?> Code (markup):
That'd involve some parsing...but maybe using preg_match. If I get some time I can try to write one for you.
Hey can you Explain the code above piece by piece so I can have a understanding of whats going on I would like to get the weight part added into a diff section and work on the monster area as well thanks
Hope this helps. If the data is in the same format, just load the data and call ProcessList the same way. <?PHP // Load the data $Data=file_get_contents('idnum2itemdesctable.txt'); // The explode splits data apart by a set delimiter and returns an array. $DataArray=explode("\n#",str_replace('\r\n','\n',$Data)); // Replace \r\n with \n if it was a windows saved file...This just for consistancy. array_pop($DataArray); // Go back one to delete the empty item $List=ProcessList($DataArray); // Whatever ProcessList returns is what $List is set to /* Example of usage */ echo $List[501]; // Return the item in the array with the key id 501 /* End of example */ /* Process the list! */ // Function which will get us the individual items and return as an array function ProcessList($Data) { $List=array(); // Declare the array if(!is_array($Data))//If the given parameter is not an array... return array(); //Return an empty array foreach($Data as $DataItem)//Loop through each item in the array { $FirstPound=strpos($DataItem,'#');//Find the first pound $Num=intval(substr($DataItem,0,$FirstPound));//Take the value from 0 to the first pound and make it a number. if(is_numeric($Num))//If the result is a number $List[$Num]=substr($DataItem,$FirstPound+1);//Take everything from the first # onward. } return $List; // Return the array } ?> PHP: