Jump to content

mctoys

Members
  • Posts

    14
  • Joined

  • Last visited

    Never

Everything posted by mctoys

  1. Oh wow - that's a thousand times easier than what I was trying to do!! Thanks so much!!
  2. Hmm Ok - so no immediate replies - maybe I'm asking the wrong questions! So i've been playing around with the script and would be happy to use the str_replace method if I can dynamically populate the array which handles the keywords and tags. Here is what I have: <?php $words = array('Ben','Tim','John'); $text = 'Me and my friends John Tim and Ben went to the park.'; $replace = array(); foreach($words as $word) { array_push($replace[$word], "<a href=\"#\">".$word."</a>"); } $newtext = str_replace(array_keys($replace), $replace, $text); ?> The problem is, my array_push doesn't work, it outputs this: Array ( [ben] => [Tim] => [John] => ) Me and my friends and went to the park. Any ideas?
  3. Hi Guys I'm sure this is really simple for you experts but it's proving to be my nemesis! I have an array of words: eg. $words = array('Apples','Bananas','Cherries'); and would like to search a string of text for these words and create link tags around them based on a simple template, <a href="#">$word_found</a> $text = "I love eating apples and bananas but hate cherries!"; So i would like to return a string like this: $text = "I love eating <a href=#>apples</a> and <a href="#">bananas</a> but hate <a href="#">cherries!</a>"; Now, I need it to do a case insensitive search, but just wrap tags around the words it finds, leaving the case as it was found in the string. I want to avoid using a function where I have to list all the replacements like this: function replace_text($text){ $replace = array('apples' => '<a href="#">apples</a>','bananas' => '<a href="#">bananas</a>'); $text = str_ireplace(array_keys($replace), $replace, $text); return $text; } because it seems very messy and long winded. Also it's not case insensitive. It seems like there must be a way of simply searching for the words in my array and wrapping tags around the found words. Please can anyone help!? Many Thanks Dan
  4. Thanks for the input - It sounds like this is exactly what I should be using, unfortunately I'd never even heard of stored procedures until now so I don't think I'll be able to write one anytime soon! I'll have to stick with my longhand way of doing it although it is quite slow already!
  5. That sounds absolutely ideal but I wouldn't know how to rewrite the function in MYSQL! Is it easy? The function is below: function changeKey($keyFrom,$amount) { // CHECK IF SPLIT CHORD $splitchord = strpos($keyFrom,'/'); if($splitchord == true) { $parts = explode("/",$keyFrom); return getChange($parts[0],$amount)."/".getChange($parts[1],$amount); } else { return getChange($keyFrom,$amount); } } // OUTPUT NEW CHORDS function getChange($chord,$amount2) { // SEPARATE KEY FROM SUFFIX $thekey = substr($chord,0,1); $flatsharp = substr($chord,1,1); if($flatsharp == 'b' || $flatsharp == '#') { $thekey = substr($chord,0,2); $suffix = substr($chord,2,10); } else { $suffix = substr($chord,1,5); } $keyResult = mysql_query("SELECT * FROM keynotes WHERE keynote = '$thekey'") or die(mysql_error()); while($row = mysql_fetch_assoc($keyResult)) { $keypos = $row['id']; } mysql_free_result($keyResult); $newkey = $keypos + $amount2; while($newkey > 12) { $newkey = $newkey -12; } $newKeyResult = mysql_query("SELECT * FROM keynotes WHERE id = '$newkey'"); while($row = mysql_fetch_assoc($newKeyResult)) { $thekey = $row['keynote']; } mysql_free_result($newKeyResult); return $thekey.$suffix; }
  6. Thanks everyone for their help - I solved the problem for all those who are interested! The answer was in drawing down all the songs from the chords table and all the associated changed keys from the userkeys table using a JOIN LEFT select statement. I then dumped the results in an array and added an extra key which was the result of my php function on the keynote and keychange fields. Finally I filtered the array to only display the results which contained the searched key. It's a bit convoluted but it works! For anyone interested the (rather sloppy) code is below! <?php function filter_by_value ($array, $index, $value){ if(is_array($array) && count($array)>0) { foreach(array_keys($array) as $key){ $temp[$key] = $array[$key][$index]; if ($temp[$key] == $value){ $newarray[$key] = $array[$key]; } } } return $newarray; } $songResult = mysql_query("SELECT chords.id,chords.title,chords.artist,chords.keynote,userkeys.songid,userkeys.keychange FROM chords LEFT JOIN userkeys ON chords.id = userkeys.songid"); $songArray = array(); $i = 0; while($row = mysql_fetch_assoc($songResult)) { $songArray[$i]['title'] = $row['title']; $songArray[$i]['newkey'] = changeKey($row['keynote'],$row['keychange']); $i++; } mysql_free_result($songResult); $newResults = filter_by_value($songArray, 'newkey', '$searchcriteria'); foreach($newResults as $song) { echo $song['title']." - ".$song['newkey']; }
  7. Yes, sort of - the problem is that there is a record in the userkeys for each song which the user has transposed, so I won't be searching the chords database for everything in 'Eb' rather I need to check what key the user has transposed each song to before I then return all the results in That key. The original keys might all be different but the user may have transposed them all to C# for example. I was trying to go down the JOIN route so that I could get an amalgamation of both tables but if I can't use a function within the SQL it sounds like I might have bitten off more than I can chew! Thanks for your input anyway.
  8. OK I have made a site for displaying song chords and each user can change the key of the song on the fly This key change is stored in a database so it always displays in that key for that user. The original song, chords and key are in one table and the user's preference of key for each song is in another table. My function takes the original key for example 'C#', and a value for the number of semitones that the song is transposed, and outputs the new note. for example changeKey('C#','2'); would return 'Eb'. I want to be able to list all the songs in a certain key. This list will be dynamic for each user as they have set their preferences for the key of each song. My select statement looks like this: $songResult = mysql_query("SELECT chords.id,chords.title,chords.artist,chords.keynote,userkeys.songid,userkeys.keychange FROM chords LEFT JOIN userkeys ON chords.id = userkeys.songid WHERE keynote = 'C' ORDER BY title"); What I need to do is perform my function on chords.keynote and userkeys.keychange to return the key which is being searched for. Complicated I know....
  9. OK - thank you Any suggestions as to how I might achieve the same goal?
  10. Thank you but It's not really what I need. The function I am actually using isn't just addition it's taking a text field, analysing it in comparison with the second variable and returning a relevant result from another database! What I need to know is how to get the syntax right on the example I have shown! Cheers Dan
  11. Hi Guys I don't know if this is possible but can someone point me in the right direction. I have a php function which takes two inputs and returns an output. for simplicity's sake let's say it's an addition function. What I want to do is use a mysql select statement to show all the rows from a database where field1 and field2 equal '3'. Here's the sort of thing I mean. function addNumbers($one,$two) { return $one + $two; } mysql_query("SELECT * FROM table WHERE 'addNumbers(field1,field2)' = '3'"); What I actually want to do is a lot more complex than this but I am trying to understand how to make the syntax work in simple terms first. Can anybody help? Many Thanks Dan
  12. Hiya Thanks so much it looks like exactly what I want although there is one mysql_error which comes up: Unknown column 'Distance' in 'where clause'. I'm not sure why this is as I can see you have brought that data out as Distance in the query. Any ideas? Many Thanks Dan
  13. How about in the individual Ad slots using $random+1 or $random+2 etc to ensure no duplication?
  14. Hi There I have a postcode database and a properties database. I also have a PHP function which calculates the distance between two Long and Lat Values which I can get from the postcode database. I am looking to search for properties which are within a certain radius of the inputted postcode. I can do this in a roundabout sort of way but am convinced their ought to be a better way of doing it! Here are snippets of what I have done (which works). Can anyone help me write a better Query to avoid having to select all the property data and only displaying the properties in range. // Get Values from posted form $distance = $_REQUEST['distance']; $postcode = $_REQUEST['keyword']; // Get Lat and Long values for postcode entered in form and store them to variables $pc_result=mysql_query("SELECT * FROM postcode_data WHERE postcode LIKE '$postcode' LIMIT 1"); while($row = mysql_fetch_assoc($pc_result)) { $longitude = $row['long_dec']; $latitude = $row['lat_dec']; } mysql_free_result($pc_result); // Get all property data $result=mysql_query("SELECT * FROM properties"); $num_results = mysql_num_rows($result); $propertyArray = array(); $i = 0; while($row = mysql_fetch_assoc($result)) { $propertyArray[$i]['id'] = $row['id']; $propertyArray[$i]['Photo'] = $row['Photo']; $propertyArray[$i]['Address'] = $row['Address']; $propertyArray[$i]['Postcode'] = $row['Postcode']; $i++; } } // Display all property data... foreach($propertyArray as $property) { // Get lat and long data for each property postcode in the database and store them to variables $distance_result=mysql_query("SELECT * FROM postcode_data WHERE postcode LIKE '".$property['Postcode']."' LIMIT 1"); while($row = mysql_fetch_assoc($distance_result)) { $distance_longitude = $row['long_dec']; $distance_latitude = $row['lat_dec']; } mysql_free_result($distance_result); // Get distance using external PHP function $property_distance = round(distance($distance_latitude, $distance_longitude,$latitude,$longitude, "M"),1); // Only display properties which are in radius if($property_distance < $distance) { ?> <div id="property"> <?php echo $property_distance; ?> miles<br /> <img src="<?php echo $property['Photo']; ?>" /> <p class="details"><?php echo $property['Address']; ?></p> </div> <?php } } ?> Very messy code - I also can't count the number of results in range! - HELP!
×
×
  • 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.