Jump to content

[SOLVED] foreach query help.


corillo181

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
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.