corillo181 Posted July 29, 2007 Share Posted July 29, 2007 i'm writting this function that takes a text and separate it by blank space and then check to see if that word is in the data base if it is, it echos out the word that is in the database if is not it echos out the original entered word. the problme is if it does not find the word in the data base it echos out array but it works fine if it finds the word in the database. <?php if($lan==='sp'){ $separate=explode(' ',$word); foreach($separate as $v){ $getWord="SELECT Sp FROM tra_translator WHERE En='$v'"; $query=mysql_query($getWord)or die(mysql_error()); $wordNum=mysql_num_rows($query); if($wordNum>0){ $word=mysql_fetch_array($query); echo $word['Sp']; }else{ echo $word; } } }else{ echo $word; } } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 29, 2007 Share Posted July 29, 2007 Try: <?php if($lan==='sp'){ $separate=explode(' ',$word); foreach($separate as $v){ $getWord="SELECT Sp FROM tra_translator WHERE En='$v'"; $query=mysql_query($getWord)or die(mysql_error()); $wordNum=mysql_num_rows($query); if($wordNum>0){ $word=mysql_fetch_array($query); echo $word['Sp']; }else{ echo $v; } } }else{ echo $v; } } ?> You were echoing $word. You should have been echoing $v, since this is the word being searched for in each iteration of your loop. Edit: The reason why it displayed array, is because your defined $word as an array: $word=mysql_fetch_array($query); Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 29, 2007 Author Share Posted July 29, 2007 sorry i forgot to use $v instead of word.. lol funny i was just writing that i notice that.. lol thanx anyways Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 29, 2007 Author Share Posted July 29, 2007 i don't want to make another topic so I'll ask here is there a way to take a sentence in which ever word is capitalized and only capitalized the first word? Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 Hmmm this is kind of ghetto, but this is all I can think of: $str = 'This Is A String.'; $str = strtolower($str); $tmp = substr($str, 0, 1); $newstr = strtoupper(substr($str, 0, 1)) . substr($str, 1, strlen($str)); echo $newstr; //This is a string. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 29, 2007 Author Share Posted July 29, 2007 well thats a good way but with lest code i did it this way. $txt=strtolower($txt); $txt[0]=strtoupper($txt[0]); echo $txt; Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 Hmmm I thought the syntax for that would've been: $txt{0}.... I was under the impression that could only read characters though, not be modified to reflect changes. Quote Link to comment Share on other sites More sharing options...
corillo181 Posted July 29, 2007 Author Share Posted July 29, 2007 $txt{0}? $txt[0] reads the first the first latter no matter how long the text is. and with strtolower i turn the whole txt to lower case. Quote Link to comment Share on other sites More sharing options...
corbin Posted July 29, 2007 Share Posted July 29, 2007 Odd.... They must do the same thing... Just tested this: <?php $str = 'Test String'; $str1 = strtolower($str); $str2 = strtolower($str); $str1[0] = strtoupper($str1[0]); $str2{0} = strtoupper($str1{0}); echo $str1 . ' ' . $str2; //output: Test string Test string ?> I didn't think you could modify things like that though (not sure where I picked that idea up...). Well, I guess I learn new things everyday ;p. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 29, 2007 Share Posted July 29, 2007 You should get used to using the square brackets for the string offset. Take a look at the minutes from the php meeting with regard to php 6. Curly braces wont work in php 6 for this sort of thing. http://www.php.net/~derick/meeting-notes.html#cleanup-for-vs Edit: Just noticed that two people from yahoo were present at the meeting..a quick google confirms that yahoo uses php as their backend. Quite surprised to see that! Quote Link to comment 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.