PHP - Help With Setting Order Of Array
i have checkboxes in a form with the name="array[]" and in the php i use the implode function to separate all the values of all that was checked with a comma....my question is... how can i set the order of the array a certain way... i'd like to ultimately have a field where if you check a box you can enter into the field 1, 2, 3, 4 and so on and when you submit the form it will set the order according to what was entered
thanks Similar TutorialsHello all, I need to make a rotating banner system for a client. They want about 5 banners to rotate with Javascript. However they want the visitor to sort of step through each one on each refresh. So if I have 5 banners and I go to the site, I see banner 1 and then the javascript rotates through each one. When I go back to the site, they want it to start with banner 2 and then the javascript will rotate through each one. Then on and on. Go back and see banner 3, then step through again. I have a nice Javascript image slider I'd like to use. It basically just requires the images in order. Like: <img src="1.jpg"/> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> I've thought about it and I think the best way to do this would just be using PHP and a cookie. So when the viewer first goes to the site, PHP checks for the cookie. If it exists, it pulls the starting image #. If it doesn't, it starts at 0 and sets the cookie. As for the images part, I figured an associate array would work well. The key would be what the cookie logic is based on, and then the image HTML would be in the array. So you visit the site without a cookie it would be... 0 => <img src="1.jpg"/> 1 => <img src="2.jpg"/> 2 => <img src="3.jpg"/> 3 => <img src="4.jpg"/> 4 => <img src="5.jpg"/> Then when you go back to the site, I need the array to be re-ordered like this: 0 => <img src="2.jpg"/> 1 => <img src="3.jpg"/> 2 => <img src="4.jpg"/> 3 => <img src="5.jpg"/> 4 => <img src="1.jpg"/> What's the best way to go about this array logic? My experience with manipulating data inside arrays is rather limited. Basically I need it to say "Start with key # and then reorder array based on that" Hi guys, I'm trying to sort an array. The array has information about listings. ID, name, info etc. An unrelated set of data also contains the listing ID's and I have made a function to sort this array and show an new order for the ID's. I now need to sort the original array by this new ID order... This snippet shows that the original listings array can be sorted by link_id: sort($this->links, $this->link->link_id); //This works. It sorts links by link ID! In a foeach loop I have created this: $sleeps_array[] = array("ID" => $link->link_id, "Sleeps" => $sleeps); // Makes an array of arrays of a custom field with listing ID. It makes this: array ( 0 => array ( 'ID' => '9', 'Sleeps' => '2', ), 1 => array ( 'ID' => '3', 'Sleeps' => '4', ), 2 => array ( 'ID' => '6', 'Sleeps' => '6', ), ) I can then sort this array of arrays with this function: function compare_sleeps($a, $b){ return strnatcmp($a['Sleeps'], $b['Sleeps']); } # sort alphabetically by name usort($sleeps_array, 'compare_sleeps'); It then gives me a sorted array according to "Sleeps" (as in, the number of people a property can cater for). It has the correct order of ID to then output but how do I organise the original array by ID with this new array? Sounds complicated, I hope you got that! Cheers, RJP1 I am trying to separate this information by the "-" symbol so that I can insert it. This output would be ideal. Any help today? 105-2-2-3-Cheerleading $value[0] should = 105 $value[1] should= 2 $value[2] should= 2 $value[3] should= 3 $value[4] should= Cheerleading I have tried many approaches and come up with notta 105-Athletics 105-1-Eagle Sports 105-2-Programs 105-2-1-Boys 105-2-1-1-Football 105-2-1-2-Cross Country 105-2-1-3-Golf 105-2-1-4-Basketball 105-2-1-5-Baseball 105-2-1-6-Track 105-2-1-7-Tennis 105-2-2-Girls 105-2-2-1-Volleyball 105-2-2-2-Golf 105-2-2-3-Cheerleading 105-2-2-4-Basketball 105-2-2-5-Softball 105-2-2-6-Track 105-2-2-7-Tennis 105-3-Coaches 105-4-FHSAA 105-5-Game Directions Thank you for looking. Hi, I've been trying to re-order an array and I can't seem to even do it let alone neatly. I've ran out of time and would appreciate any help! $array = array( '0' => array( 'filepath' => 'files/image_site1.png', 'title' => 'Website', ), '1' => array( 'filepath' => 'files/image_id.png', 'title' => 'Identitity', ), '2' => array( 'filepath' => 'files/image_site2.png', 'title' => 'Website', ) ) I'm looking to re-order the arrays within the parent array depending on whether the 'filepath's contain a sub string of '_id' or '_site'. I've been using: $type = '_site'; foreach ($array as $item){ if (preg_match("/$type/i", $item['filename'])){ //add this $item to the top of array, somehow... } } So with $type set to '_site', I'd like to re-arrange the array so both arrays containing _site1.png and _site2.png appear at the top of the parent array. I.e: $array('0' = array(...), '2' => array(...), '1' => array(...)); I hope I've explained it well enough. Thanks, For example for my Skill Tree:
array (size=9) 0 => array (size=2) 'Skill_Id' => string '1' (length=1) 'Skill_Points' => string '0' (length=1) 1 => array (size=2) 'Skill_Id' => string '2' (length=1) 'Skill_Points' => string '0' (length=1) 2 => array (size=2) 'Skill_Id' => string '3' (length=1) 'Skill_Points' => string '0' (length=1) 3 => array (size=2) 'Skill_Id' => string '4' (length=1) 'Skill_Points' => string '1' (length=1) 4 => array (size=2) 'Skill_Id' => string '5' (length=1) 'Skill_Points' => string '1' (length=1) 5 => array (size=2) 'Skill_Id' => string '6' (length=1) 'Skill_Points' => string '1' (length=1) 6 => array (size=2) 'Skill_Id' => string '7' (length=1) 'Skill_Points' => string '0' (length=1) 7 => array (size=2) 'Skill_Id' => string '8' (length=1) 'Skill_Points' => string '1' (length=1) 8 => array (size=2) 'Skill_Id' => string '9' (length=1)For the Skill_Id, it must be in order when checking server side. 1 through 9. (or 9 can be changed by me whenever). So, I have: $skillrange = range(1,$amountofskills); Which will output: array (size=9) 0 => int 1 1 => int 2 2 => int 3 3 => int 4 4 => int 5 5 => int 6 6 => int 7 7 => int 8 8 => int 9But, how do I check this order from my original array? I need to check My Skill_ID values in order from my original array and make sure they are 1 through 9. (So a user cannot tamper the data) Edit: I've come up with this solution since making this thread: $array1 = $skillidarray; $array2 = $skillrange; if (!empty(array_diff_assoc($array1, $array2))){ echo "Your skill tree data is out of order, please report this to an administrator."; }Something like this would work right? Edited by Monkuar, 11 October 2014 - 03:59 PM. Hi, I have this php code to display the url of the files and it works fine, it just has a weird order when it is echoed out. Here is the code: Code: [Select] <?php $date = date("Y-m-d"); $picarray = array(); $handle = opendir ('pqimages/'.$date.'/'); while (false !== ($file = readdir($handle))) { if($file != "." && $file != "..") { $picarray[] = "http://randomaydesigns.com/pqimages/".$date."/".$file.""; } } $json_array = json_encode($picarray); echo $json_array; ?>There are three files in the folder and they are 33,34,35.jpg it displays them like 33,35,34.jpg Any Recommendations? Thanks, GEORGE I'm somewhat new to OOP PHP, and I'm still learning, but basically I want searchcolumns() to set which MySQL columns that it'll search inside but I don't know where to go from what I've already coded. Here's the basic input to be sent to my classes file: $getusers = new database; $getusers->search($_GET['search']); $getusers->searchcolumns('username','account','email'); And to put it simple, I need my $where variable to output this: $where .= "(username LIKE '%$search%' OR account LIKE '%$search%' OR email LIKE '%$search%')"; Except replacing the already set columns with the ones that I assigned in the searchcolumns() array, and then adding an OR string if there's more than 1 column set. Here's the code as I have it at the moment, for obvious reasons, it doesn't work how I want it to: // Search columns function searchcolumns($columns) { $this->column = explode(", ", $columns); } // Search results function search($search) { $search = ($_POST['search']); if (isset($_GET['search']) && strlen($_POST['search']) > 1) { $searchresult = explode(" ", $search); $where = "WHERE"; foreach ($searchresult as $key => $search) { $where .= "($this->column LIKE '%$search%')"; if ($key != (sizeof($searchresult) - 1)) $where .= " AND "; } $this->where = $where; } } Sorry if this has been somewhat confusing to explain, but hopefully someone understands what I'm trying to achieve. the interface explains everything Code: [Select] #page1.html <html> <head> </head> <body> <table border='0' width='50%' cellspacing='0' cellpadding='0' > <form name=form1 method=post action="page2.php"> <?PHP $postData[] = array(); $postData[0] = '9'; $postData[1] = '8'; $postData[2] = '7'; $postData[3] = '6'; ?> <tr> <td><b>Choose your order</b></td> <td> </td> <td> <input type=checkbox name=scripts[] value='<?PHP $postData[0]; ?>'>pasta <br> <input type=checkbox name=scripts[] value='<?PHP $postData[1]; ?>'>burger <br> <input type=checkbox name=scripts[] value='<?PHP $postData[2]; ?>'>fries <br> <input type=checkbox name=scripts[] value='<?PHP $postData[3]; ?>'>chili dog <br> <br> </td> </tr> <tr><td align=center > <input type=submit value=Submit> <input type=reset value=Reset></td></tr> </form> </table> </body> </html> Code: [Select] #page2.php <?PHP if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (isset($_POST['scripts'])) { $cb = $_POST['scripts']; $numRows = count($cb); print "you have chosen the following order numbers:"; for($i=0; $i<$numRows; $i++){ print $cb[$i] . "<br>"; } } else{ print "you have'nt checked anything!"; } } ?> What do? I have this code Code: [Select] $nArr = array('A', 'B', 'C', 'D', 'E', 'F'); $counter = 3; while ($counter > 0) { $chunkedValues[$counter][0] = 1; for ($j = 0 ; $j < $counter ; $j++) { $chunkedValues[$counter][$j + 1] = $nArr[$j]; } $nArr = array_slice($nArr, $counter--); } var_dump($chunkedValues); that outputs: Code: [Select] array 3 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 2 => array 0 => int 1 1 => string 'D' (length=1) 2 => string 'E' (length=1) 1 => array 0 => int 1 1 => string 'F' (length=1) But i need this index structu Code: [Select] array 0 => array 0 => int 1 1 => string 'A' (length=1) 2 => string 'B' (length=1) 3 => string 'C' (length=1) 1 => array 1 => int 1 2 => string 'D' (length=1) 3 => string 'E' (length=1) 2 => array 2 => int 1 3 => string 'F' (length=1) I want to avoid loops with ceil. Any idea? thanks for your time. Hi, I have a loop which creates an array: $productions[] = array( 'url'=>get_the_permalink(), 'year'=>$production_year->name, 'title'=>get_the_title() ); When I output the results, the year is in the wrong order:
2019 What I can't work out is how to order 2014 - 2020 I tried ksort(array, SORT_STRING) and ksort(array, SORT_NUMERIC) I also tried natsort But still have the same issue - am I missing something? Thanks I wish to get only the last part of each array 'g' And then show non duplicated 'g' but in the same order that it was in the original array. Code: [Select] <?php $formarray = array( 'name' => array( 'i1'=> array('a'=>'a1', 'b'=>'b1', 'c'=>'c1', 'd'=>'d1', 'e'=>'e1', 'f'=>'f1', 'g'=>'g1'), 'i2'=> array('a'=>'a2', 'b'=>'b2', 'c'=>'c2', 'd'=>'d2', 'e'=>'e2', 'f'=>'f2', 'g'=>'g2'), 'i3'=> array('a'=>'a3', 'b'=>'b3', 'c'=>'c3', 'd'=>'d3', 'e'=>'e3', 'f'=>'f3', 'g'=>'g3'), 'i4'=> array('a'=>'a4', 'b'=>'b4', 'c'=>'c4', 'd'=>'d4', 'e'=>'e4', 'f'=>'f4', 'g'=>'g4'), ) ); foreach ($formarray as $newarray => $a) { ?><strong><?=$newarray;?></strong><br><? foreach ($a as $key => $k) { ?>"<?=$key;?>", <? foreach ($k as $b) { //if ($k['DBfield'] != "") { ?>"<?=$b;?>", <? //} } ?><br><? } //end of second foreach ?><br><br><br><? } //end of first foreach ?> Hi, I'm not necessarily looking for code examples for my problem, (they are welcome but its not my explicit goal). I'm looking more for advice on how to go about tackling the problem i have. what i already have Currently i have a search getting results from a MySQL database. The results are ordered by one field that signifies their importance. Many of these results have the same value in this one column, and i have a switch statement to distinguish different content/styles for the different values. (there are just over 10 different bands) So the most important results are shown first and with a different style to the ones below it. (some bands have the same styling) what i need I would like to be able to randomize the results but only randomize the results within their specified bands. e.g. lets say theres 100 results in band A and 100 in band B. I still want everything in band A to be listed above band B, but i would like to randomly change the order of the results in band A and band B, (in this case once a week). I need to save the order of this randomization (client/server side?) for a week, then run the script again to set a new order. Now I would still class myself as an amateur php'er so at this stage i dont even know how i would go about separating the bands and randomizing their results independantly, let alone causing this functionality to run only once a week. So can anyone suggest a way i might go about this? I'm pretty stuck right now. Cheers I wish to order my results by its occurrence inside the $status array. Basically all of the results with the status 'New' are to be listed first, then the results with the status Today, Tomorrow, Soon and so on. Is this possible, or is there alternatively a different approach to ordering my results how I desire? Code: [Select] $status = array('New', 'Today', 'Tomorrow', 'Soon', 'Future', 'Complete'); $display->get_results("SELECT * FROM todo ORDER by status DESC"); Hello dear friends, hello community, i want to parse a site http://www.aktive-buergerschaft.de/buergerstiftungsfinder Well therefore i have a true beginner-question regarding Array in order to work with the data: beginner question The results should be stored in a MySQL-Database: well you see. i have a Curl-Approach: and i want to work with the parsed data: and in this case i want to give over the data into an array (if viewing the output in web browser, do view-source to see structured array). Here the musings that led to the code: Depending on num dimensions, we could do, foreach($result as $k => $v) {echo $k." ".$v} to view and work with data (adding sub foreach loops accordingly; i.e. if $v is itself an array) see the results: What do you think: Code: [Select] $ch = curl_init("http://www.aktive-buergerschaft.de/buergerstiftungsfinder"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $buffer = curl_exec($ch); curl_close($ch); $result = explode(",", $buffer); foreach($result as $k => $v) {echo $k." ".$v} if $v is itself an array) // print_r($result); look forward to hear from you!! db1 okay I am using a jquery plugin to re-arrange images which are in mysql and they have their own "order_number" ... when i re-arrange stuff the new order is put into a hidden field in a form and i have it along with another hidden field with their current order... when submitted I combine these two fields in an array so the key is the original order and the value is the new order ....i have 12 images... Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 [10] => 10 [11] => 11 [12] => 12 ) this is what is what the array looks like... i have a foreach loop with a mysql query but things get kind of weird when you try to do it this way $num = $_POST['old_num']; $num = unserialize($num); $sort = $_POST['sortOrder']; $sort = explode(',', $sort); if ($_POST){ $combine = array_combine($num, $sort); foreach($combine as $key => $value){ mysql_query("UPDATE shirts SET order_number='$value' WHERE order_number='$key' ") or die(mysql_error()); } when you do this lets say for example we have the image with order number "1" get switched with the image order number "2"..... the database first updates with number 1 which becomes 2... then when it goes to number "2"... at this point... the first image gets switched back to number 1.... so 1 becomes 2 then the new 2 becomes 1 again and the other 2 becomes 1 also. I can not think of another way I could update mysql with the correct order numbers... If anyone has any ideas or other solutions I am open to all suggestions... thank you! Sample array: array('one','two','three','_zero'); How could I sort this array so that _zero moves to the front.. Any items that begin with an underscore should be moved to the front of the array to get something like so: _zero, one, two, three Thanks There's probably an obvious reason but I can't seem to find it... I start with the $_POST array, received from a form: Code: [Select] array(9) { ["Name"]=> string(3) "KTN" ["SQLServer"]=> string(24) "10.6.11.20\VSQLI028,1433" ["Username"]=> string(2) "GF" ["Password"]=> string(2) "GF" ["MasterDB"]=> string(11) "GFMaster_KN" ["Version"]=> string(3) "4.9" ["Prod"]=> string(1) "1" ["Monitored"]=> string(1) "0" ["button"]=> string(38) "updateColumnName=EnvironmentID;Value=1" } I get the button value from the array, and unset the button array value. Code: [Select] function load_POST($name) { //returns value and removes it from $_POST array. returns NULL if not existing. $debug = 0; if ( $debug == 1 ) { $backtrace = backtrace(); echo __FUNCTION__."()"; echo " <i>called by ".basename($backtrace[1]['file'])."</i><br/>\n"; } $post = NULL; if( array_key_exists($name, $_POST) ) { $post = urldecode($_POST[$name]); if ( $debug == 1 ) { echo "post $name, value: $post<br/>\n"; } } else { if ( $debug == 1 ) { echo "post $name: doesn't exist<br/>\n"; } } unset($_POST[$name]); return $post; } $_POST is now: Code: [Select] array(8) { ["Name"]=> string(3) "KTN" ["SQLServer"]=> string(24) "10.6.11.20\VSQLI028,1433" ["Username"]=> string(2) "GF" ["Password"]=> string(2) "GF" ["MasterDB"]=> string(11) "GFMaster_KN" ["Version"]=> string(3) "4.9" ["Prod"]=> string(1) "1" ["Monitored"]=> string(1) "0" } Then I create the object to assign the values to: Code: [Select] object(Environment)#1 (9) { ["EnvironmentID"]=> NULL ["Name"]=> NULL ["SQLServer"]=> NULL ["Username"]=> NULL ["Password"]=> NULL ["MasterDB"]=> NULL ["Version"]=> NULL ["Prod"]=> int(0) ["Monitored"]=> int(0) } So far so good Then, for each remaining $_POST value, I update the Object accordingly: First one, parametername: Name, parameter: KTN Code: [Select] object(Environment)#1 (10) { ["EnvironmentID"]=> NULL ["Name"]=> string(3) "KTN" ["SQLServer"]=> NULL ["Username"]=> NULL ["Password"]=> NULL ["MasterDB"]=> NULL ["Version"]=> NULL ["Prod"]=> int(0) ["Monitored"]=> int(0) ["ColumnName=EnvironmentID;Value=1"]=> object(stdClass)#3 (1) { ["ColumnName"]=> string(1) "1" } } And there we have the problem, for some reason the button value is added to the object somehow... Any ideas? Thanks in advance! Hey all.. I'm trying to pick up PHP by reading ebooks and follow tutorials. I am now busy with creating a thumbnailgallery from uploaded pics per user. I want to let the visitor scroll through a row of thumbnails by clicking next or previous. There are 3 different rows of thumbnails. Here's how it looks like: <<previous (row1 of 6thumbnails) next>> <<previous (row2 of 6thumbnails) next>> <<previous (row3..... ) next>> when page loads mysql fetches rows of information for the filenames...then there is a foreach row loop that does first a do ..while loop to build up the thumbnailrows...and then creates the navigation links previous and next who send the "page"+1 or "page"-1 to _GET array so that the thumbnail 7 until 12 will be shown. The problem i run into is that i want the main code page to remember at which "page" of scrolling through the thumnails he is. Now it is that when a user scrolls one row of thumbnails and stops at the 3rd "page" of it and then clicks on a different row to go to "page" 2 of that row that all the rows jump together back to "page" 1. I thought there may be a solution in storing the number of row and the number of page into an array but can't seem to find a solution.... Has anybody got experience in this? Help would be very welcome i'm totally stuck and cant read about it in tutorials/ebooks.... greets from holland.. GJ
Hi everyone. I'm very new into self learning programming. Presently I'm trying to develop a simple basic Robot that would only Place a Market Order every seconds and it will cancel the Order every followed seconds. Using the following library: It would place Trade Order at a ( Price = ex.com api * Binance api Aggregate Trades Price) I have already wrote the api to call for xe.com exchange rate with php <?php $auth = base64_encode("username:password"); $context = stream_context_create([ "http" => [ "header" => "Authorization: Basic $auth" ] ]); $homepage = file_get_contents("https://xecdapi.xe.com/v1/convert_from?to=NGN&amount=1.195", false, $context ); $json = json_decode($homepage, TRUE); foreach ($json as $k=>$to){ echo $k; // etc }; ?> And also for the Binance Aggregate Price in JavaScript
<script> var burl = "https://api3.binance.com"; var query = '/api/v3/aggTrades'; query += '?symbol=BTCUSDT'; var url = burl + query; var ourRequest = new XMLHttpRequest(); ourRequest.open('GET',url,true); ourRequest.onload = function(){ console.log(ourRequest.responseText); } ourRequest.send(); </script>
My problem is how to handle these two api responds and also the functions to use them to place a trade and cancel it. I've got. |