Jump to content

Is there a better way?


Go to solution Solved by calmchess,

Recommended Posts

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";
}

Link to comment
https://forums.phpfreaks.com/topic/279034-is-there-a-better-way/
Share on other sites

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?

 

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

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

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.

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.

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.

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;

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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