Jump to content

string comparison


euel

Recommended Posts

Hi! gud pm guys!

I have a simple problem which i can't seem to workout so i kinda need your expert help!  :)

I'm trying to compare a variable to a string in my db,

the variable and the entry on my db has a format of (car - branch),

I made this simple function to do that but it doesn't seem to compare properly..

 

  function get_car_code($branch)
{
global $connection;
	$query = "SELECT code ";
	$query .= "FROM car_code ";
	$query .= "WHERE branch ="  . '"$branch"';
	$query .= " LIMIT 1";
	$result_set = mysql_query($query, $connection);
	confirm_query($result_set);
	return $result_set;

}

 

when its called back it doesn't return anything.. I'm thinking its the white spaces (or I'm wrong) but i needed them so i cant removed them..

Any suggestions? Thanks in advance!

 

Link to comment
https://forums.phpfreaks.com/topic/254863-string-comparison/
Share on other sites

Holy Mother of Christ! -_-

it seems i solved my stupid problem after a short sleep..

i was editing the wrong php file while looking for changes on the wrong php page.... soo lame..

Anyways just for the record, i tried your code harrisweed and it works perfectly! thanks!

 

Can i ask another help? its kinda related to this..or will i put it in another topic?

Anyways here it is:

I have a user table with a field name of branch where it contains a string like this - > car1 - branch1, car2 - branch2, car3 - branch3...

I need to make it an array so i can separate each (car - branch) so i did this code:

 

function display_user_branches($user_id, $branch = NULL)
{
$branches = get_all_branch($user_id);  
$all_branches = mysql_fetch_array($branches);
$split_branches = preg_split("/,/", $all_branches['brand'], -1, PREG_SPLIT_NO_EMPTY);
	foreach($split_branches as $value)
	{
		echo "<option value='{$value}' ";
		if($value == $branch)
		{
		echo " selected";
		}	
		echo ">$value</option>";
	}
}

 

and is being called by this:

 

echo "<td>";
echo "<select name='branch' >";
	display_user_branches($reserved['users_id'], $reserved['branch']);									
echo "</select>";
echo "</td>";	

 

everything works fine, it displays all car - branch in each option except for the comparison to make it selected.

Can't seem to figure out what's wrong.. 

Again $value and $branch has a format of Car - Branch like my first solved problem..

Maybe i messed up some simple thingy..

Any advice? Thanks in advance!!!!

 

 

Link to comment
https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306950
Share on other sites

At forst glance it looks like a white space problem. try:

function display_user_branches($user_id, $branch = NULL)
{
$branches = get_all_branch($user_id);  
$all_branches = mysql_fetch_array($branches);
$split_branches = preg_split("/,/", $all_branches['brand'], -1, PREG_SPLIT_NO_EMPTY);
	foreach($split_branches as $value)
	{
		echo "<option value='{$value}' ";
		if(trim($value) == trim($branch))
		{
		echo " selected=\"selected\"";
		}	
		echo ">$value</option>";
	}
}

 

I just changed your selected statement too.  selected on it's own isn't standards complient.

Link to comment
https://forums.phpfreaks.com/topic/254863-string-comparison/#findComment-1306955
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.