Jump to content

unplugged_web

Members
  • Posts

    897
  • Joined

  • Last visited

Posts posted by unplugged_web

  1. 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

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

  3. I've removed the extra semi-colon and added one where it should be but I still get the same error. I'd prefer to have it all in one go but will I still be able to do the checkboxes if I do? Thanks

  4. 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>

  5. 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>

  6. 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";  }?>

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

  8. 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
  9. 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.

  10. I'm not sure why this isn't working. I have a table of entries, but I'd like to change the running order of the entries. I've created a field called 'priority' and I want the people in the back end to change the order of the entries themselves. So for example, if they move one entry up then the one above will automatically move down. I'm not getting any errors at all, just a blank page.The code I've written is:

     <?php$id = $_GET['id'];$direction = $_GET['direction']; con = mysql_connect("hostname", "user", "password");if  (!$con){die('Could not connect: ' . mysql_error());}mysql_select_db("table", $con); $sql = "SELECT priority FROM Reviews WHERE ID = $id";list($curpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error())); // Find new positionif ($direction > 0){// To be moved up$sql = "SELECT priority FROM Reviews WHERE priority < '$curpos' ORDER BY priority LIMIT 1";list($newpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error()));}else{// To be moved down$sql = "SELECT priority FROM Reviews WHERE priority > '$curpos' ORDER BY priority LIMIT 1";list($newpos) = mysql_fetch_array(mysql_query($sql) or die(mysql_error()));} if ($newpos){$sql = "UPDATE Reviews SET priority = $curpos WHERE priority = $newpos";mysql_query($sql) or die(mysql_error()); $sql = "UPDATE Reviews SET priority = $newpos WHERE ID = $id AND priority = $curpos";mysql_query($sql) or die(mysql_error()); if (!mysql_error() && mysql_affected_rows()) $msg = "Review moved"; }mysql_close($con);?>

    but this doesn't seem to do anything. I'd be grateful for any help. I have in working perfectly for another table in the same database, but even if I copy the code over and ust change the relevant fields it still doesn't work. The 'priority' field is set to not null and is an integer. Thanks for any help

×
×
  • Create New...