Jump to content

Help with LIKE


unplugged_web

Recommended Posts

I've got two tables, one has a list of categories in it and the other has a field with all of the categories that are active. So if categories 1, 3, 8, 90 and 70 are active for entry 'c' then that entry will have 1 3 8 90 70 in the category_id field. This is fine but I'm trying to display it using checkboxes and if the category id is in the category_id field then the box should be ticked. To do this I'm using, this ($category has ready been defined on the page):

<?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM styles");$style_id = $row['id'];while($row = mysql_fetch_array($result))echo "<br />". $row['name'] .": <input name=\"{$row['id']}\" type=\"checkbox\" ";if ( $category LIKE %$style_id% )echo "checked=\"checked\"";  echo "/>";mysql_close($con);?> 

But all I get is Parse error: syntax error, unexpected T_STRING If I change

if ( $category LIKE %$style_id% )

to

if ( $category != $style_id )

then only the last box is ticked. I'm really puzzled as to what's happening here and how I can fix it. I want all of the categories to be shown, but only the active ones to be ticked.

Link to comment
Share on other sites

  • Replies 52
  • Created
  • Last Reply

Top Posters In This Topic

PHP's equivalent is regular expressions. They're more costly than ordinary operations and require some new learning. There probably is a better way to design your system.
Trouble is it's a live database that's already been set up, I've got to find a way to make it work within the site I'm building.
You can use explode to split the string into individual IDs and then use in_array to check if a particular ID is in the array.
Sorry to ask but how do I do that, I'm not very good with array's and my php knowledge is only basic. Thanks Edited by thehappyappy
Link to comment
Share on other sites

For explode() function see : http://www.w3schools...ing_explode.aspand for in_array() function see : http://www.w3schools...ay_in_array.asp
So if I did something like this:
 <?php$category = $row['category'];$new_category (explode(" ",$category));?><?php$category = array($new_category); if (in_array($id,$category))  {  echo "Match found";  }else  {  echo "Match not found";  }?>

would that work? The full code is below

 <?php$category = $row['category'];$new_category (explode(" ",$category));?><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM styles");while($row = mysql_fetch_array($result))echo "<br />". $row['name'] .": <input name=\"{$row['id']}\" type=\"checkbox\" ";$category = array($new_category); if (in_array($id,$category))  {  echo "checked=\"checked\"";  }else  {  echo "checked=\"unchecked\"";  }  echo "/>";mysql_close($con);?>

Edited by thehappyappy
Link to comment
Share on other sites

No, it wouldn't work. Read your code line by line and try to execute it in your head.
Sorry to sound dumb but I don't understand why it won't work? The first part
 <?php$category = $row['category'];$new_category (explode(" ",$category));?>

Looks to me list it would get the value of 'category' from the database, then pass it through the explode function? So instead of being one long string it is then a series of values. (so 1 2 3 9 12 30 becomes 1, 2, 3, 9, 12, 30) The next part checks to see if the id of the style matches anything in the category field?Or is the problem with this line? It should be new_category instead?

 if (in_array($id,$category))

Or would this work better:

 <?php$category = $row['category'];$old_category (explode(" ",$category));$new_category array($old_category);?><?php$category = array($new_category); if (in_array($id,$new_category))  {  echo "Match found";  }else  {  echo "Match not found";  }?>

Edited by thehappyappy
Link to comment
Share on other sites

The first part of your code is trying to access the value of $row['category'] before you even queried the database.
Sorry I wasn't very clear, the category value has already been retrieved from an earlier query. The full code is:
 <?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM images WHERE id = $image");($row = mysql_fetch_array($result));mysql_close($con);?><?php $filename = $row['filename'];$title = $row['title'];$class = $row['class'];$category = $row['category'];?><form action="" method="get" name="Update_Image"><img src="/uploads/image/filename/<?php echo $filename ?>" alt="<?php echo $title ?>" title="<?php echo $title ?>" /><br />Image title: <input name="Title" value="<?php echo $title ?>"/><br />Image class (Portfolio, Artist or Bio): <input name="class" value="<?php echo $class ?>"/><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM styles");$style_id = $row['id'];while($row = mysql_fetch_array($result))echo "<br />". $row['name'] .": <input name=\"{$row['id']}\" type=\"checkbox\" ";$category = $row['category'];$old_category (explode(" ",$category));$new_category array($old_category);$category = array($new_category); if (in_array($id,$new_category))  {echo "checked=\"checked\"";   }else  {echo "checked=\"unchecked\"";   }echo "/>";mysql_close($con);?> </form>

Link to comment
Share on other sites

I notice you're making redundant connections. mysql_connect() only needs to be done once on each page. Anyways, have you tried the code to see if it works? At a glance I don't see anything particularily wrong with it.

Link to comment
Share on other sites

I notice you're making redundant connections. mysql_connect() only needs to be done once on each page. Anyways, have you tried the code to see if it works? At a glance I don't see anything particularily wrong with it.
I've tried it but just get an error saying: Parse error: syntax error, unexpected '{' in edit.php on line 8 If I remove that '{' then I get this: Parse error: syntax error, unexpected T_EXIT in edit.php on line 9 This is the entire page:
<?php $image = intval($_GET['id']);;?><?php include 'includes/db.php'?><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM images WHERE id = $image");($row = mysql_fetch_array($result));mysql_close($con);?><?php $filename = $row['filename'];$title = $row['title'];$class = $row['class'];$category = $row['category'];?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Untitled Document</title></head> <body><div><form action="" method="get" name="Update_Image"><img src="/uploads/image/filename/<?php echo $filename ?>" alt="<?php echo $title ?>" title="<?php echo $title ?>" /><br />Image title: <input name="Title" value="<?php echo $title ?>"/><br />Image class (Portfolio, Artist or Bio): <input name="class" value="<?php echo $class ?>"/><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  } mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM styles");$style_id = $row['id'];while($row = mysql_fetch_array($result))echo "<br />". $row['name'] .": <input name=\"{$row['id']}\" type=\"checkbox\" ";$category = $row['category'];$old_category (explode(" ",$category));$new_category array($old_category);$category = array($new_category); if (in_array($id,$new_category))  {echo "checked=\"checked\"";   }else  {echo "checked=\"unchecked\"";   }echo "/>";mysql_close($con);?></form></div></body></html>

Link to comment
Share on other sites

You should condense all those separate <?php ?> blocks into one. It will make debugging easier.Like this:

<?php$image = intval($_GET['id']);;include 'includes/db.php'$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }

First problem: extra semi-colon here: $image = intval($_GET['id']);;Missing semi-colon on this line: include 'includes/db.php'

Link to comment
Share on other sites

the issue appears to be your curly braces, as evidenced by the error message. Braces and parens should be easy to debug, whenever you open one, you need to close it. The error message give you a line number, so look at that line and the lines above and below it and work from there.

Link to comment
Share on other sites

You're not doing this correctly: $category = $row['category'];$old_category (explode(" ",$category));$new_category array($old_category);$category = array($new_category); First, you're missing some "=" signs in those 2 middle lines. Second, the explode function returns an array. You don't need to make that another array. Those lines get the value from $row['category'] and save that in $category, then (assuming you add the missing "=" sign) you explode $category and save the returned array into $old_category. Then you make a new array, add the returned array into it, and save that as $new_category. Then you make another new array, add the whole array structure into that, and save that back to $category. So $category ends up being an array that contains an array that contains an array with the exploded data. You just want the exploded data, you don't need to keep nesting it inside other arrays.

Link to comment
Share on other sites

the issue appears to be your curly braces, as evidenced by the error message. Braces and parens should be easy to debug, whenever you open one, you need to close it. The error message give you a line number, so look at that line and the lines above and below it and work from there.
Sorry I'm not sure where I'm going wrong here, I got the code to connect from this page
You're not doing this correctly: $category = $row['category'];$old_category (explode(" ",$category));$new_category array($old_category);$category = array($new_category);
So this is what I should be doing:
 $category = $row['category'];$old_category =  (explode(" ",$category));$new_category = array($old_category);

Edited by thehappyappy
Link to comment
Share on other sites

Just explode the string and save the returned array. You use in_array on the return value from explode. You don't need to add another line where you store the returned array in another array. explode already returns an array, you don't need to put that in another array.

Link to comment
Share on other sites

Just explode the string and save the returned array. You use in_array on the return value from explode. You don't need to add another line where you store the returned array in another array. explode already returns an array, you don't need to put that in another array.
I'm sorry I know I'm being really dumb here, but I still don't understand it. I just can't get my ahead around this. The entire code I've got is:
 <?php $image = intval($_GET['id']);?><?php include 'includes/db.php'; ?><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM images WHERE id = $image");($row = mysql_fetch_array($result));mysql_close($con);$filename = $row['filename'];$title = $row['title'];$class = $row['class'];$category = $row['category'];?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /><title>Untitled Document</title></head> <body><div><form action="" method="get" name="Update_Image"><img src="/uploads/image/filename/<?php echo $filename ?>" alt="<?php echo $title ?>" title="<?php echo $title ?>" /><br />Image title: <input name="Title" value="<?php echo $title ?>"/><br />Image class (Portfolio, Artist or Bio): <input name="class" value="<?php echo $class ?>"/><?php$con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM styles");while($row = mysql_fetch_array($result))echo "<br />". $row['name'] .": <input name=\"{$row['id']}\" type=\"checkbox\" ";$style_id = $row['id'];$category = $row['category'];$new_category =  (explode(" ",$category));if (in_array($style_id,$new_category))  {echo "checked=\"checked\"";   }else  {echo "checked=\"unchecked\"";   }echo "/>";mysql_close($con);?></form></div></body></html>

but that returns an error saying:Parse error: syntax error, unexpected '{' in edit.php on line 8If I remove that '{' then I get this:Parse error: syntax error, unexpected T_EXIT in edit.php on line 9

Link to comment
Share on other sites

What do the first few lines of your code look like now?
Thats now:
 <?php include 'includes/db.php'; $con = mysql_connect($host,$username,$password);if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($database, $con);$result = mysql_query("SELECT * FROM images WHERE id = $image");($row = mysql_fetch_array($result));mysql_close($con);$filename = $row['filename'];$title = $row['title'];$class = $row['class'];$category = $row['category'];$image = intval($_GET['id']);?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...