calmchess Posted June 11, 2013 Share Posted June 11, 2013 I'm using the following code to search an array but my echo statement(echo $indexnum0) doesn't return the correct value after the delete statement runs. What is a better way to write this so that the indexnum0 variable always retruns the index value. If I remove the delete statement everything works well. Thanks for your time. --calmchess $isfound = in_array("empty",$currentrooms0); if($isfound==true){ $indexnum0 = array_search("empty",$currentrooms0); //index to remove sent on ajax callback $arrayid0=$currentrooms0[$indexnum0]; $q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='$arrayid0'")or die(mysqli_error($conn0)); if(mysqli_affected_rows($conn0)>0){ echo $indexnum0; } }else{ echo "not empty"; } Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/ Share on other sites More sharing options...
mac_gyver Posted June 11, 2013 Share Posted June 11, 2013 it's likely that your code is being requested twice and you are seeing the output from the second execution, but since you didn't share what your data is and what you saw or what you expected to see from that data, this is just a guess. how about posting sample data in $currentrooms0 and what incorrect value you see when you echo $indexnum0 so that we have the same knowledge about what you saw in front of you? Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435333 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 its ok I just take care of it with a for loop instead of using a handy php method to return the index number. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435341 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 BTW above is returning the index value not the index position. I was confused about what array_search returns. My mistake. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435342 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 <?php $conn0=mysqli_connect("localhost","calmchess","ptoK4ptoK4","book"); if($conn0){ $q0=mysqli_query($conn0,"select _current_created_rooms from room_attributes")or die(mysqli_error($conn0)); while($row0 = mysqli_fetch_array($q0)){ $currentrooms0[]=$row0['_current_created_rooms']; } if(!isset($currentrooms0)){//used in the case of only 1 user logging out $q1=mysqli_query($conn0,"delete from room_attributes where id > 1")or die(mysqli_error($conn0)); echo "not empty"; return; } $numindexes0= array_count_values ($currentrooms0); if(!isset($numindexes0["empty"])){ echo "not empty"; return;//just return here because all rooms might be created wouldn't want to delete them all } if(count($currentrooms0)==1 && $numindexes0["empty"]==1|| count($currentrooms0)==2 && $numindexes0["empty"]==2){ $q2=mysqli_query($conn0,"delete from room_attributes where id>1")or die(mysqli_error($conn0)); return; } $isfound = in_array("empty",$currentrooms0); if($isfound==true){ for($i=0; $i<count($currentrooms0);$i++){ if($currentrooms0[$i]="empty"){ echo $i; $q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='$currentrooms0[$i]'")or die(mysqli_error($conn0)); break } } }else{ echo "not empty"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435343 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 newest working code <?php $conn0=mysqli_connect("localhost","calmchess","ptoK4ptoK4","book"); if($conn0){ $q0=mysqli_query($conn0,"select _current_created_rooms from room_attributes")or die(mysqli_error($conn0)); while($row0 = mysqli_fetch_array($q0)){ $currentrooms0[]=$row0['_current_created_rooms']; } if(!isset($currentrooms0)){//used in the case of only 1 user logging out $q1=mysqli_query($conn0,"delete from room_attributes where id > 1")or die(mysqli_error($conn0)); echo "not empty"; return; } $numindexes0= array_count_values ($currentrooms0); if(!isset($numindexes0["empty"])){ echo "not empty"; return;//just return here because all rooms might be created wouldn't want to delete them all } if(count($currentrooms0)==1 && $numindexes0["empty"]==1|| count($currentrooms0)==2 && $numindexes0["empty"]==2){ $q2=mysqli_query($conn0,"delete from room_attributes where id>1")or die(mysqli_error($conn0)); return; } $isfound = in_array("empty",$currentrooms0); if($isfound===true){ for($i=0; $i<count($currentrooms0);$i++){ if($currentrooms0[$i]=="empty"){ echo $i-1; $q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='$currentrooms0[$i]'")or die(mysqli_error($conn0)); break; } } }else{ echo "not empty"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435344 Share on other sites More sharing options...
mac_gyver Posted June 11, 2013 Share Posted June 11, 2013 do you have a state-able goal for that code? most of that code doesn't do anything or is repeating something the code already did in a previous step. you are even throwing more code in because you are using a _fetch_array() statement and getting both numerical and associative indexes in your data. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435350 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 LOL i guess now you understand why i didn't show all the code. The point here now is $i doesn't echo $i if the delete statement is there somehow the delete statement kills the echo. If i comment out the delete statment the one near $i then everything works fine. I just need it to delete and then echo $i so that some ajax that is calling this php returns "echo's" the index number and changes an image within the javascript callback. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435368 Share on other sites More sharing options...
mac_gyver Posted June 11, 2013 Share Posted June 11, 2013 you need to define what you want that block of code to do and if you want us to help, share that definition. what are the input(s), what processing do you want, and what are the output(s). the posted code has no external input (other than being requested.) best i can tell is you want to find if there are any row(s) with 'empty' as a value and delete one or all of them. you can do that with one query. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435373 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 lines destroys the echo statement I've verified it 3 diffrent ways.I think its time for somebody else to help. I need to isolate the delete statement from the echo statment. anybody have any ideas on how i can accomplish that? $q3=mysqli_query($conn0,"delete from room_attributes where _current_created_rooms='empty' order by id desc limit 1")or die(mysqli_error($conn0)); echo $i-1; Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435379 Share on other sites More sharing options...
mac_gyver Posted June 11, 2013 Share Posted June 11, 2013 since you haven't stated what processing and result you do want and under what conditions you want it, no one can help with the problem. your posted code and statements in the thread doesn't tell us that information. if your code demonstrated what you were trying to accomplish, your code would be working and you wouldn't be posting on a help forum. defining the specific and detailed inputs, processing, and output/result is a key first step in all programming and clearly communicating that definition would be required if you want someone to help you. Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435382 Share on other sites More sharing options...
Solution calmchess Posted June 11, 2013 Author Solution Share Posted June 11, 2013 i already told you what I"m trying to accomplish I need to echo $i for ajax.....the code is appopriate. Please just stop answering Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435384 Share on other sites More sharing options...
calmchess Posted June 11, 2013 Author Share Posted June 11, 2013 final solution to getting $i to echo is if($i-1>0){ echo $i-1; }else{ echo 0; } Quote Link to comment https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/#findComment-1435389 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.