Sleeper Posted March 17, 2011 Share Posted March 17, 2011 ok guys I've made a script that will allow me to move items and up and a list using weight. The page loads fine the first time, then when you click on the up or down arrow it swaps the weight values and wrights them to the data base. On the same page I have $data = mysql_query("SELECT * FROM columnleft ORDER BY weight ASC") or die(mysql_error()); while($info = mysql_fetch_array($data)) { $id[] = $info[id]; $weight[] = $info[weight]; } This is allowing me to loop in the upper and lower weights to the current item. So they can be changed. However I get a [17-Mar-2011 18:25:11] PHP Fatal error: [] operator not supported for strings in dir/page on line 80 Line 80 is the $weight[] = $info[weight]; So my guess is that its writing the weight to the database and trying to get an array at the same time? So it errors out. So if I'm right is there a way to prioritize so it will finish writing then load the rest? Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/ Share on other sites More sharing options...
Sleeper Posted March 17, 2011 Author Share Posted March 17, 2011 I should of clearly stated here, that the pages loads with no errors just fine, then when you click on the up or down arrows it dose right to the database the pages reloads to it self and then errors out on the weight array and goes white. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188935 Share on other sites More sharing options...
Eyewash01 Posted March 17, 2011 Share Posted March 17, 2011 Without seeing the rest of your code it's hard to tell, but for one I didn't think you could reference array items with string keys sans quotes - i.e. you have $info[weight] which should surely be $info["weight"] or $info['weight'] Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188938 Share on other sites More sharing options...
Sleeper Posted March 17, 2011 Author Share Posted March 17, 2011 although having $info['weight'] is definitely the proper way to do it and I have updated that cause I forgot about it, it dose work with out it. As I said the page loads fine and everything is brought into the array just fine. It's when I submit the form to change the weights around that it reloads the page, runs either if ($moveup == yes) { $weight=$_POST['weight2']; $weightb=$_POST['weight']; $data="UPDATE columnleft SET weight='".$weight."' WHERE ID='$_POST[id]' "; if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); $data="UPDATE columnleft SET weight='".$weightb."' WHERE ID='$_POST[id2]' "; if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); } if ($movedown == yes) { $weight=$_POST['weight2']; $weightb=$_POST['weight']; $data="UPDATE columnleft SET weight='".$weight."' WHERE ID='$_POST[id]' "; if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); $data="UPDATE columnleft SET weight='".$weightb."' WHERE ID='$_POST[id2]' "; if (!mysql_query($data,$con)) die('Error: ' . mysql_error()); } up or down depending on what you pick and it dose make the changes to the database. It then moves down the page to where it is asked to create the array again and that's when the error happens. I feel like its not able to create the array because the weight field is being changed at the beginning of the page. I can take the above coding out and put it on a second page like a success page and then come back to the original page and it works just fine, but I want it to stay on the same page. So it seems like I need a pause for it to finish writing before it tries to pull the info again. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188942 Share on other sites More sharing options...
Eyewash01 Posted March 17, 2011 Share Posted March 17, 2011 Once again I'm surprised those if statements are working, as surely it needs to read if ($moveup == "yes") (i.e. with quotes) - can't believe it would work otherwise. Anyhow, if you say the code is working when you put it on another page, then you can still do that and make it appear to the user that they are on the same page, just set the form action to a seperate processing page, include your moving code here, then at the bottom of the page, put header("Location: form.html"); // OR WHATEVER YOUR OTHER PAGE IS CALLED This way, to the user it appears they have never left the page. It also stops the annoying thing or users refreshing the page and the script running again. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188944 Share on other sites More sharing options...
Pikachu2000 Posted March 17, 2011 Share Posted March 17, 2011 It will work without the quotes, as long as there is no constant defined by the same name, and it's rather inefficient because php has to look for a constant by that name, then when it can't find one, treat the value as a string, and throw a warning. You'd see those warnings if you had error_reporting set to -1 or E_ALL and display_errors = On. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188951 Share on other sites More sharing options...
Sleeper Posted March 18, 2011 Author Share Posted March 18, 2011 I used the header redirect and that did the trick. Thanks. as far as the ' or " in the fields. I was instructed that as long as the value isn't spaced like (==This Value) and its wasn't an add or subtract type thing that it was fine to not have them. And I do have warnings on and I don't see them. but if its that big of a deal ill switch up my habit there. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188954 Share on other sites More sharing options...
Eyewash01 Posted March 18, 2011 Share Posted March 18, 2011 Yeah even if it's not producing an error, it is most certainly best practise to enclose values in quotes lol Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188957 Share on other sites More sharing options...
Pikachu2000 Posted March 18, 2011 Share Posted March 18, 2011 Oh, it's producing an error; it just isn't being echoed. It's slowing the script down, too. Quote Link to comment https://forums.phpfreaks.com/topic/230968-operator-not-supported-for-strings-error/#findComment-1188996 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.