PHP - Php Function Similar To Mysql Like?
I'm pretty sure there isn't, but I was wondering if there was a php function that would return partial matches similar to MySQL's LIKE, I've been searching for a while and haven't found one. If not, is there a class or something that someone knows of that will accomplish a like of search or a string comparison? I want to be able to compare two variables and return one of three results - Exact Match, Partial Match, No Match.
I suppose regex may do the trick but I'm a complete novice when it comes to regex and I have no idea where to even begin. Thanks for any suggestions. Similar Tutorialsim trying to run a query which will get posts from the database that have tags similar to the current ones. Unfortunatly the echoed result isnt displaying as it should. $query = $link->query("SELECT t.*, p.* FROM ".TBL_PREFIX."topics t JOIN ".TBL_PREFIX."posts p ON (p.p_tid = t.t_tid) WHERE t.t_name = '".decode_url($_GET['t_name'])."' LIMIT 1") or die(print_link_error()); $row = $query->fetch(PDO::FETCH_ASSOC); $tag_query = $link->query("SELECT p_name FROM ".TBL_PREFIX."posts WHERE MATCH (p_tags) AGAINST ('".$row['p_tags']."' IN BOOLEAN MODE) ")or die(print_link_error()); $result = $tag_query->fetch(); foreach($result as $key => $value) { echo 'Name = '.$result[$key]['p_name'].'<br />'; } i have done a dump of result and it shows: Code: [Select] $ => Array (2) ( ['p_name'] = String(12) "File Updates" ['0'] = String(12) "File Updates" ) but there should only be one item in this array(p_name). I dont know where the second is coming from. The query has pulled the right results, it just isnt displaying it right. hi My first language is not english so i hope you bear it i am new to php am am making my shopping Cart as my web engineering course i have made everything i all want to know how to i show similar products when a user click on any product details it open new page and shows a detail of that page i want to show similar products list below to details so can any tell me how to do it plzz kindly see the pic in atttachment i have posted snap shop of my page i put similar product statically i want to show it dynamically from tha data base kindly answer please regards Hello, I am looking for a push in the right direction. I would like to be able to view a website that is duplicated on many servers by specifying the IP address. Similar to how one would use the hosts file. I can do this using netcat nc 1.2.3.4 80 Host: example.com GET /contact-us.html <CR><CR> Can someone tell me how I would go about porting this to PHP? I would just like to set up a small page on my webserver where I can enter the domain and IP address and have the page displayed back to me. Thanks Colin How can I check if an array contains two items that are the same? For example ("apples", "bananas", "oranges") returns false But ("apples", "bananas", "bananas") returns true Hi Folks, i have been at this for ages and its now depressing me as i cannot figure out why i cant get it to work. I am using a re-hashed version of a facebook style wall post system where it limits the amount of replies, you click a button and gets the next set and so on until there are no more results to get. The original script runs ok but is not really suitable for what i need So to my script which is based loosley on the same principal. Ok i have a veiwmember.php page which uses pagination to limit the amount of wall posts to 5. I have included the various javascript files jquery and another which is needed. The function used is this one: Code: [Select] <script type="text/javascript" src="jquery.livequery.js"></script> <script type="text/javascript" src="jquery-1.2.6.min.js"></script> <script type="text/javascript"> //more records show $('a.more_records').livequery("click", function(e){ var next = $(this).attr('id').replace('more_',''); $.post("data.php?show_more_post="+next, { }, function(response){ $('#bottomMoreButton').remove(); $('#posting').append($(response).fadeIn('slow')); }); }); </script> The php code in veiwmember.php that gets the wall posts is: Code: [Select] Pagination code here <?php $getwallposts= ("SELECT * FROM wallposts WHERE recipientid='$memberid' ORDER BY id DESC LIMIT $offset, $rowsperpage"); $result = mysql_query($getwallposts,$con); while ($row2 = mysql_fetch_assoc($result)) { $messageid= $row2['id']; $senderid= $row2['senderid']; $wallpost= $row2['wallpost']; $timesent= $row2['timesent']; $timestamp = strtotime($timesent); $likes= $row2['likes']; $getname= ("SELECT fname, lname, image1 FROM memocp WHERE unid='$senderid'"); $nameresult= mysql_query($getname,$con); while ($rowws = mysql_fetch_array($nameresult)) { $fname= $rowws['fname']; $lname= $rowws['lname']; $wallpostprofilethumb= "{$rowws['image1']}"; if (empty($wallpostprofilethumb)) {$wallpostprofilethumb= "noimage.png";} } ?> <div class="wallpost"><div class="from"><div class"profilethumb"><img src="profilethumb/<?php echo $wallpostprofilethumb; ?>"></div><a href="veiwmember.php?membid=<?php echo "$senderid"; ?>"><?php echo "$fname $lname"; ?></a></div><div class="postbody"><?php echo txt2link($wallpost) ?><div class="timesent"><?php echo date('l jS F Y @ H:i a', $timestamp); ?></div></div><div class="likes"><form action="like.php" method="post"><?php if (isset($_GET['currentpage'])) {$currentpage= $_GET['currentpage']; echo "<input name=\"currentpage\" type=\"hidden\" value=\"$currentpage\">";} ?><input name="messageid" type="hidden" value="<?php echo "$messageid"; ?>"><input name="base" type="hidden" value="<?php echo "$memberid"; ?>"><input name="like" type="image" src="ima/like.jpg"><?php echo " $likes people like this"; ?></form></div></div> <?php include_once('data.php'); ?> <?php } rest of pagination The pagination sort of works, but only displays one result per page when it should show 5 results plus how ever many replies, but i can sort that out later, would prefer to get the replies working first. You will see that data.php is included which gets the replies to the wall posts. It should limit the amount of replies shown to five, and then if there is more, it will show a link to get more. If that link is clicked it should get the next five results and show them. However it shows the first five and the link if there are more results, but if the link is clicked, nothing happens. data.php Code: [Select] require ("database.php"); $next_records = 5; $show_more_button = 0; if(isset($_REQUEST['show_more_post'])) // more posting paging { // button has been clicked so we need to get next set of results $next_records = $_REQUEST['show_more_post'] + 5; $result = mysql_query("SELECT * FROM wallreplies2 WHERE messageid='$messageid' ORDER BY wallreplyid DESC limit ".$_REQUEST['show_more_post'].", 5"); // see if there are any more replies to get $check_res = mysql_query("SELECT * FROM wallreplies2 order by wallreplyid DESC limit ".$next_records.", 5"); $show_more_button = 0; // button in the end $check_result = mysql_num_rows($check_res); if($check_result > 0) { // if there any more results counted the button needs to be shown $show_more_button = 1; } } // if the button hasnt been clicked we need to get the first results else { // lets count the number of results that exist $countreplies= mysql_query("SELECT * FROM wallreplies2 WHERE messageid='$messageid'"); $countresult= mysql_num_rows($countreplies); // if there are more than 5 results if ($countresult > 5) { // the button needs to be shown $show_more_button = 1; } else { // if there are 5 or less replies in total, dont show the button $show_more_button = 0;} //there are only 5 or less replies to get so get the first results $result = mysql_query("SELECT * FROM wallreplies2 WHERE messageid='$messageid' ORDER BY wallreplyid DESC limit 0,5"); } //$replyresult = mysql_query($result,$con); while ($roww = mysql_fetch_array($result)) { $wallreplyid= $roww['wallreplyid']; $sendid= $roww['senderid']; $wallreply= $roww['wallreply']; $time= $roww['timesent']; $Timestamp = strtotime($time); $like= $roww['likes']; $getreplynames = ("SELECT fname, lname, image1 FROM memocp WHERE unid='$sendid'"); $nameresults = mysql_query($getreplynames,$con); while ($roww = mysql_fetch_array($nameresults)) { $fnames= $roww['fname']; $lnames= $roww['lname']; $wallreplyprofilethumb= "{$roww['image1']}"; if (empty($wallreplyprofilethumb)) {$wallreplyprofilethumb= "noimage.png";} } ?> <div class="wallreplies" id="record-<?php echo"$wallreplyid"; ?>"> <div class="wallreplywrap"> <div class="wallfrom"> <div class"profilethumb"><img src="profilethumb/<?php echo $wallreplyprofilethumb; ?>"></div> <a href="veiwmember.php?membid=<?php echo "$sendid"; ?>"><?php echo"$fnames $lnames"; ?></a> </div> <div class="wallmessbody"><?php echo txt2link($wallreply) ?></div> <div class="timesent"><?php echo date('l jS F Y @ H:i a', $Timestamp); ?></div> <div class="likes"><form action="like2.php" method="post"><?php if (isset($_GET['currentpage'])) {$currentpage= $_GET['currentpage']; echo "<input name=\"currentpage\" type=\"hidden\" value=\"$currentpage\">";} ?><input name="messageid" type="hidden" value="<?php echo "$wallreplyid"; ?>"><input name="base" type="hidden" value="<?php echo "$memberid"; ?>"><input name="like" type="image" src="ima/like2.jpg"><?php echo " $like people like this"; ?></form> </div> </div> </div> <?php } if($show_more_button == 1){ echo "<div class=\"bottomMoreButton\"> <a id=\"more_$next_records\" class=\"more_records\" href=\"javascript: void(0)\">Get more replies</a> </div>"; }?> <div class="wallreply"> <form action="wallreply.php" method="post"> <input name="sendrid" type="hidden" value="<?php echo "$loginuniqid"; ?>"> <input name="messageid" type="hidden" value="<?php echo "$messageid"; ?>"> <input name="base" type="hidden" value="<?php echo "$memberid"; ?>"> <?php if (isset($_GET['currentpage'])) {$currentpage= $_GET['currentpage']; echo "<input name=\"currentpage\" type=\"hidden\" value=\"$currentpage\">";} ?> <textarea class="newwallpostreply<?php echo $i++ ; ?>" name="wallreply" autocomplete="off" maxlength="1000" placeholder="Reply"></textarea> <span class="wallreply-holder<?php echo $p++; ?>"> <div class="button_block"> <input type="submit" class="post" value="Post"> <input type="submit" class="cancel" value="Cancel"> </div> </span> </form> </div> I know its a lot of info there, but if anyone can see the problem i would be greatful. I have a brain twister in a script i'm working on. It have a multidimensional array that hold values to populate a google charts js script. To make it work as I plan I need to merge the data from similar arrays together. I'll give an example and hopefully it will make more sense. Also, this page is being loaded over ajax so, the faster/smaller the better. Example Array: Code: [Select] Array ( [0] => Array ( [item] => 1 [quant] => 2 [price] => 12.5 [day] => 18 [onl] => ) [1] => Array ( [item] => 1 [quant] => 2 [price] => 12.5 [day] => 18 [onl] => ) [2] => Array ( [item] => 1 [quant] => 5 [price] => 12.5 [day] => 17 [onl] => 1 ) [3] => Array ( [item] => 1 [quant] => 12 [price] => 12.5 [day] => 19 [onl] => 1 ) [4] => Array ( [item] => 1 [quant] => 4 [price] => 12.5 [day] => 21 [onl] => ) ) So since array 0 and 1 both have data from sales on the 18th for the same item I would want to merge them into a single array and leave all others in the parent array the same. I apologize in advance if this doesn't make sense, I've been working too many hours. X| If anyone knows of a good solution for this or perhaps a better approach, that would be great. Thank You, E Hello, I need to be able to find what is inbetween 2 values in a string of text. For example, <?php $string="Hello and welcome to the site. Click [link]welcome.php[/link] here to continue to the site"; ?> So I need some method of searching the string to see what values are in between the [link] and [/link] which I will then deal with in my own way. I have a feeling that I could do this with explode, but I'm not really sure. Any advice? Thanks in advance are all of the following same, or some same or all different: $variable="" $variable=0 $variable=NULL and which of the above will satisfy if(empty($variable)) { blah!; } How to COUNT the number of records (pulled from a db)which contains the word "APPROVED". This code doesn't work though. $result1; $result2; $result3; $approvedApplicants = COUNT($result1=='Approved', $result2=='Approved', $result3=='Approved') echo ($approvedApplicants) This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=350182.0 So I have a basic search form that goes through and does a mySQL like '%$sanitizedInput%' to column in the table. The problem I have is that if I have two rows, one with a value "green" and the second "blue", and I do a search for "green", it shows the green result, if I do a search for "blue", it shows the blue result, but if i do a search for "green blue" it returns no results. Any ideas on how to fix this? I hope I'm not being a vampire here sucking the life out of you guys, but I can't find any resources in google that explains how to properly use this function in a mysql array. All it does is add a url for any urls added in plaintext. Code: --- <?php function hyperlink ($string) { $string = preg_replace('#(^|\s)([a-z]+://([^\s\w/]?[\w/])*)#is', '\\1<a href="\\2" target="_blank">\\2</a>', $string); $string = preg_replace('#(^|\s)((www|ftp)\.([^\s\w/]?[\w/])*)#is', '\\1<a href="http://\\2" target="_blank">\\2</a>', $string); $string = preg_replace('#(^|\s)(([a-z0-9._%+-]+)@(([.-]?[a-z0-9])*))#is', '\\1<a href="mailto:\\2">\\2</a>', $string); return $string; } // mysql connection $result = mysql_query("SELECT * FROM top_web_articles ORDER BY ID DESC"); echo "<table border='0'>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td class='newstitle' align='left' valign='top' bgcolor='#D6D6D6'>".$row['Category']."</td></tr>"; echo "<td class='newstitle' align='left' valign='top' bgcolor='#D6D6D6'>". $row['ArticleName'] ."</td></tr>"; echo "<td class='newstitle' align='left' valign='top' bgcolor='#D6D6D6'>"hyperlink(. $row['ArticleDescription'] .);"</td></tr>"; echo "<tr><td class='newstitle' align='left' valign='top' bgcolor='#D6D6D6'><a href='". $row['URL'] . "' target='_blank'><img src='http://www.thenewsguys.ca/images/read entire article.png' alt='' width='130' height='11' border='0' /></a></td>"; echo "</tr>"; echo "<td align='left' valign='top'> </td>"; } echo "</table>"; mysql_close($con); ?> --- Error: "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/doofyd5/public_html/trevor/playground/displayarticles.php on line 146" (Clearly an error where I'm abusing the heck out of the bolded line. Any ideas on how to properly code this? I have created a function to connect to a required database with a username and password. I have called this db_openConnection() Now for each function that requires a select from a database is it ok just to call the db_OpenConnection() first within the function or should i be keeping open a db connection (using persist, i think) rather than keep creating a connection. I know that the connection ends when you have finished a query any how. Not sure if this is the correct forum or if it should be mysql forum Hello PHPFreaks. I have one problem, that I've tried to fix for a day or two now. I need a function, that picks out a random row in a table, and returns one thing. I tried with, getting the max rows in the table, make a random number in the interval 0 to max rows, then select the random number, but it failed. I hope someone can help me out with this one? -Niixie This is my code, and I know it's pretty messy, because i fooled around with it for a day or two, so it's hard to find head or tail in it. Code: [Select] $query0 = sprintf("SELECT * FROM registertest"); $result = mysql_query($query0); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $nums = mysql_num_rows($result); $rand_nums = rand(0,$nums); $query1 = sprintf("SELECT question FROM registertest WHERE id='%i'", $rand_nums); $result1 = mysql_query($query1) or die(mysql_error()); if (!$result1) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Whole query: ' . $query; die($message); } $row = mysql_fetch_row($result1); mysql_free_result($result); mysql_free_result($result1); $_SESSION['registerquestion'] = $row; if(strlen($_SESSION['registerquestion'])!=0){return 1;}else{return 0;} Okay, I have been following this tutorial: http://www.freewebmasterhelp.com/tutorials/phpmysql/1 to achieve exactly what I wanted to get done. I also followed the way to have it formatted in tables and added extra columns that I needed, included an "Options" column which houses three links, including "edit" and "delete" Now, I have everything working fine but I am stumped on how to get the "edit" and "delete" links to work for each individual entry that is listed. I have to have these features so the entries can be edited and deleted without having to physically go into the MySQL database to do it. The tutorial explains how to do it in Step 6, but I am confused. I'm not quite sure where to place the code for the links, which are generated automatically every time a new entry is inputted into the database. Anybody available to help me out? Thanks! I had another thread about a problem I was having where it's not loading from the DB right aways. My solution is to make an update function and then have the id's calling it on the page. So the thing I am wondering is how to make a working update function. I have this so far: <?php include blah.... $uid = $_SESSION["user"][0]; $avatarid = $_SESSION["user"][12]; function update($str) { global $db_id; $query="select ".$str." from users where id=".$uid; $result=mysql_query($query, $db_id); $row=mysql_fetch_row($result); return $row[0]; } ?> and then it's being called on the profile page with <?php echo update($avatarid); ?>. I want the update() function to select avatar from users where id = userid and then return the value fresh so it's getting it new from the database every time they refresh the page. Then there won't be a problem with updating old avatar stuff. Currently gives me the error: Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/root/public_html/func.php on line 69 So what am I doing wrong here? thanks for anyone who can help Hi, Doing a Query SELECT * FROM table WHERE field BETWEEN low_number AND high_number If i have three rows with three numbers: IE: 27, 50, 80. and i run the query above, it returns a result of: 27 and 50. How can i get it to return a result of: 27,50, and 80.?? WHY: age range search: i want to return an age range of people = and between the age of say: 20 and 70 but i want to include the 20 and 70. How can i do this? Hello everybody, I'm trying to write some code similar to Odesk.com or DMOZ.ORG. I need to count the number of listings in each category. Look here "http://ibatty.org/browse.php" to get a better idea of what I'm doing. That is the page I'm trying to add this function to. I've been having major issues with the COUNT function. Code: [Select] $query = "SELECT category, COUNT(*) FROM postlisting WHERE category='billboards'"; $result = mysqli_query($dbc,$query); while($row = mysqli_fetch_array($result)) { echo $row[COUNT(category)]; } As you can see this works, but I'm trying to add State, Area code and Zip functuality to search through the data more precisely. This has been giving me problems though and the way I currently have it setup . . . I'd have 25+ SELECT COUNT() querys going on 1 page. (I don't know if that is very good?) 5$ via paypal to whoever can help me solve this. If I did not explain what I needed very well, please ask questions for me to clarify. Thank you in advance I need to display multiple files that have a similar file name, but there are timestamps added to the filename once its been modified. What could I use in PHP to display all these files? Right now I use a $path variable that looks into a SQL table to build the path to the folder that the files reside in.. ELSEIF($Recordmyrow['Applicatio'] == "pdf file") { IF($securityvalue != "P"){$display = false;} IF(file_exists(substr($path, 5))){} ELSE{$column4 = "<A href='javascript:mbox();'>UNAVAILABLE</A>";} } So there is a file called file1.pdf then another one called file1.mmddyyyy.pdf My code shows file1.pdf but not file1.mmddyyyy.pdf Is there some kind of wildcard I can use to get it to show all the files if they exist? |