Monkuar Posted February 9, 2012 Share Posted February 9, 2012 Is there anything in php that let's me SWAP 2 arrays? like let's say I have 3,2,1 in a array, is there a way I can swap the 2 and 1 if a statement is true? Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/ Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 Not really. Why would you want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316297 Share on other sites More sharing options...
smerny Posted February 9, 2012 Share Posted February 9, 2012 like this? if($statement_is_true){ $myArr = swapArray($myArr, $oneIndex, $anotherIndex); } function swapArray($arr, $x, $y) { $tmp = arr[$x]; $arr[$x] = $arr[$y]; $arr[$y] = $tmp; return $arr; } Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316299 Share on other sites More sharing options...
Monkuar Posted February 9, 2012 Author Share Posted February 9, 2012 like this? if($statement_is_true){ $myArr = swapArray($myArr, $oneIndex, $anotherIndex); } function swapArray($arr, $x, $y) { $tmp = arr[$x]; $arr[$x] = $arr[$y]; $arr[$y] = $tmp; return $arr; } hmm, Thanks but I dont think it will work because i need to dynamically figure out the $anotherIndex array, which I cant.. I have a field in my DB, 0,1,2 and like if somone presses my "down arrow" it needs to move the 0 down 1, so it's 1,0,2. But then what if they press it again? It needs to move that 0 down 1 so it's 1,2,0 I just can't figure out how to dynamically do this? Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316302 Share on other sites More sharing options...
smerny Posted February 9, 2012 Share Posted February 9, 2012 is it always the one after it? $myArr = swapArray($myArr, $oneIndex, $oneIndex +1); otherwise i'm still unsure what you mean Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316303 Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 I have a field in my DB, 0,1,2 and like if somone presses my "down arrow" it needs to move the 0 down 1, so it's 1,0,2. But then what if they press it again? It needs to move that 0 down 1 so it's 1,2,0 I just can't figure out how to dynamically do this? Are you doing that with AJAX? If so it would be easier to get the position of each element and then send it to PHP already formatted. Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316306 Share on other sites More sharing options...
Monkuar Posted February 9, 2012 Author Share Posted February 9, 2012 is it always the one after it? $myArr = swapArray($myArr, $oneIndex, $oneIndex +1); otherwise i'm still unsure what you mean On your last code was u missing a $? i added a $ $tmp = $arr[$x]; I have 3 sections, and each 1 has it's own name. Section 1 is 0 Section 2 is 1 Section 3 is 2 my code is $middle=array ( "0" => array ( "section" => $aboutme, ), "1" => array ("section" => $signature, ), "2" => array ("section" => $f, ), ); $order = array(0,1,2); print_r($middle); foreach ($order as $index) { $middlesection.= $middle[$index]['section']; } See how the array is 0,1,2 ? That is stored in a db field row for each users profile. I want them to beable to change the order of the sections to there choice. Kinda better explanation? not with ajax Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316309 Share on other sites More sharing options...
smerny Posted February 9, 2012 Share Posted February 9, 2012 so you're looking to update the database instead of just swapping in php right? so that their changes will persist? you have a field for priority or something in this table? what exactly is "down arrow", an image with a querystring link? Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316314 Share on other sites More sharing options...
KevinM1 Posted February 9, 2012 Share Posted February 9, 2012 I have a field in my DB, 0,1,2 If those numbers are supposed to represent ids, then you need to stop what you're doing and normalize your data. Relational databases like MySQL are not supposed to be used like spreadsheets. Sticking relationships between different tables into one column is a very bad idea, as you are starting to see. For a quick crash course, read through: http://mikehillyer.com/articles/an-introduction-to-database-normalization/ Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316321 Share on other sites More sharing options...
Monkuar Posted February 9, 2012 Author Share Posted February 9, 2012 so you're looking to update the database instead of just swapping in php right? so that their changes will persist? you have a field for priority or something in this table? what exactly is "down arrow", an image with a querystring link? Yep! exactly, it updates the database row "p" where ID = 1 (or w/e user is updating it) if (isset($ibforums->input['down'])){ $DB->query("UPDATE ibf_members set p='WATTOADDHERE ' WHERE id={$ibforums->member['id']}"); header("Location: ?i={$ibforums->input['i']}"); } See it will update that value p to 0,1,2 or whatever, I have a down arrow and a UP arrow on each field section for them to click it. I would add a "up" input also, but just lost on how to update it based on what they pressed and what the other arrays are... it's so confusing KevinM1, I will read that link, thank yu Quote Link to comment https://forums.phpfreaks.com/topic/256765-php-switch-command/#findComment-1316322 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.