PHP - Can't Find A Solution...updating Order Mysql With Array
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! Similar TutorialsI have a user table that holds email addresses and for testing purposes I am trying to replace every email address with an email address defined in an array. I would like to randomly choose an email address from the array and update the table with this address. When I run the following code it randomly chooses a email address from the array but then updates every row with this one email address. Can you someone please let me know what I am doing wrong. Thanks in advance. Code: [Select] $query = "SELECT * FROM user '"; $result = mysql_query($query); $input = array('email1', 'email2', 'email3', 'email4', 'email5', 'email6', 'email7'); $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $rand_keys = ""; $rand_keys = array_rand($input, 2); $replaceStr = $input[$rand_keys[0]]; mysql_query("UPDATE user SET email = '$replaceStr'"); } Hi all, I would like to ask you if somebody could have a better idea to build this function. The current one is working, but I feel this built a bit in an amateur manner. Feel free to add your ideas so we can learn more about PHP and how to deal with arrays.
I have an array: $arrayVideoSpecs = Array( Array( 'aspect' => '4:3', 'density' => '442368', 'resolution' => '768x576' ), Array( 'aspect' => '4:3', 'density' => '307200', 'resolution' => '640x480' ), Array( 'aspect' => '16:9', 'density' => '2073600', 'resolution' => '1920x1080' ), Array( 'aspect' => '16:9', 'density' => '121600', 'resolution' => '1280x720' ) );
and I want an array as output grouped by video aspect ratio where the key is the pixel density and the value is the video resolution, namely like that for aspect ratio 16:9 ... Array ( [2073600] => 1920x1080 [121600] => 1280x720 )
Then I coded this function which is working but seems amateur ... function groupAndExtractByAspect($array, $groupBy, $aspect, $density, $resolution) { $groupByAspect = Array(); foreach ($array as $value) { $groupByAspect[$value[$aspect]][] = Array($value[$density], $value[$resolution]); } $arrayClean = Array(); foreach ($groupByAspect as $key => $value) { if ($key == $groupBy) { $arrayClean[$key] = $value; } } foreach ($arrayClean as $aspectGroup) { $arrayOutput = Array(); for ($i = 0; $i <= count($aspectGroup); $i++) { $densityIsValid = false; $resolutionIsValid = false; if (!empty($arrayClean[$groupBy][$i][0])) { $density = $arrayClean[$groupBy][$i][0]; $densityIsValid = true; } if (!empty($arrayClean[$groupBy][$i][1])) { $resolution = $arrayClean[$groupBy][$i][1]; $resolutionIsValid = true; } if (($densityIsValid === true) && ($resolutionIsValid === true)) { $arrayOutput[$density] = $resolution; } } } return $arrayOutput; }
The usage is as follow ... $showArray = groupAndExtractByAspect($arrayVideoSpecs, '16:9', 'aspect', 'density', 'resolution'); echo '<pre>'; print_r($showArray); echo '</pre>';
Thank you very much for your ideas! Mapg Hi all. I am currently trying to build a site that will allow users to manage a self build house project. This is my first php MySQL project but I am getting to grips with it ok. I am currently building this for my own benefit at the moment as I am about to undertake a self build. I have a few questions to put to you guys. This is where I am at the moment I have a database table named materials which contains a list of materials , quantities, unit of measurement, id. My site has a user control panel that has separate forms for each stage of the build where the user enters the quantities of the materials, and the data is displayed as an array for each stage. So, heres question 1. I want to take the data and use it to generate a form which will be completed by several merchants.... so it will display similar to the array but will be a form with a text box for price where the merchant will insert a price. i.e <?php mysql_connect ("localhost", "test", "test") or die ('I cannot connect to the database becuase: ' . mysql_error()); mysql_select_db ("matquant"); // query $query = mysql_query("SELECT * FROM `quantites` ORDER BY `id` ASC"); // results while ($row = mysql_fetch_array($query)) { echo "<br />" .$row ['id']. " " .$row ['material']. " " .$row['quantity']. " " .$row['unit']. "<br />";} echo mysql_error(); ?> I want to add a text input box form which will allow the merchant to add a price to each item that is added to the array. so there the merchant will be able to give me a price for all the items in my list. Heres where I get stuck.... as I am constantly adding to the list I have no fixed amount of items so the form for the merchants needs to be built dynamically, ie each time I add a new material, I want the merchants form to get a new input. I am not very good at describing my problems but I am hoping that someone can help me out from the information given. Thats the first question. i like to get issues sorted in small bites. the second is the next big bite, I want to send the form above to several merchants, so I intend to have a user database with a user for each merchant, and link the tables so that each merchant can provide a price for each material, ideally the form would highlight the materials that have not had a price yet. I'm looking for advice on the best method of doing this basically but if I can get the first question sorted that would be great.. I have the test site local but can upload it it to the server if it will help you see what im trying to do. Any help that anyone can give will be greatly appreciated. Hi guys, I'm using the API for Remember the Milk through a PHP class that I found online called PHP-RTM. Anyway after so much headaches (the authentication for the Remember the Milk API is one of the hardest I've ever come across) I've finally connected everything and gotten to the stage that I have some raw data inside an array. The next step is to work with that data. Basically I want to access the names of the task in a complex multi demensional array and I have no idea how to do this. I have done a var_dump of the array I am working with to show you the complexity. Just remember that in the complex var_dump below there are only 4 tasks: array(2) { ["stat"]=> string(2) "ok" ["tasks"]=> array(2) { ["rev"]=> string(31) "nxeir6vy9pcgcggoss4wwwooc8cwo8w" ["list"]=> array(3) { [/tt][list][li][tt]=> array(2) { ["id"]=> string( 8) "13927305" ["taskseries"]=> array(2) { [/tt][/li][li][tt]=> array(11) { ["id"]=> string(9) "104056511" ["created"]=> string(20) "2011-02-02T12:48:48Z" ["modified"]=> string(20) "2011-02-02T12:48:48Z" ["name"]=> string(42) "Send confirmation email to luxury car hire" ["source"]=> string(13) "iphone-native" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156815180" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-02T12:48:48Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } [1]=> array(11) { ["id"]=> string(9) "104148150" ["created"]=> string(20) "2011-02-03T01:25:57Z" ["modified"]=> string(20) "2011-02-03T01:26:17Z" ["name"]=> string(40) "Confirm that post has been sent with DVD" ["source"]=> string(2) "js" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156951316" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-03T01:25:57Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } } [1]=> array(2) { ["id"]=> string( 8) "13925842" ["taskseries"]=> array(12) { ["id"]=> string( 8) "98105089" ["created"]=> string(20) "2010-12-19T11:25:25Z" ["modified"]=> string(20) "2011-02-02T13:01:42Z" ["name"]=> string(25) "Read for one hour per day" ["source"]=> string(13) "iphone-native" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["rrule"]=> array(2) { ["every"]=> string(1) "1" ["$t"]=> string(21) "INTERVAL=1;FREQ=DAILY" } ["tags"]=> array(0) { } ["participants"]=> array(0) { } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156576423" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-01T13:01:45Z" ["completed"]=> string(0) "" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } [2]=> array(2) { ["id"]=> string( 8) "17007566" ["taskseries"]=> array(11) { ["id"]=> string(9) "104017503" ["created"]=> string(20) "2011-02-02T05:45:04Z" ["modified"]=> string(20) "2011-02-03T02:29:55Z" ["name"]=> string(96) "Hook up watermark for both videos (check email) and send to Rachelle and Steve at compare quotes" ["source"]=> string(2) "js" ["url"]=> string(0) "" ["location_id"]=> string(0) "" ["tags"]=> array(0) { } ["participants"]=> array(1) { ["contact"]=> array(3) { ["id"]=> string(7) "2150286" ["fullname"]=> string(10) "Samuel Nam" ["username"]=> string(10) "samuel.nam" } } ["notes"]=> array(0) { } ["task"]=> array(9) { ["id"]=> string(9) "156754354" ["due"]=> string(20) "2011-02-02T13:00:00Z" ["has_due_time"]=> string(1) "0" ["added"]=> string(20) "2011-02-02T05:45:04Z" ["completed"]=> string(20) "2011-02-03T02:32:00Z" ["deleted"]=> string(0) "" ["priority"]=> string(1) "N" ["postponed"]=> string(1) "0" ["estimate"]=> string(0) "" } } } } } } Anyone know the code i can use to simply get the "name" keys from the complex array below. I've created a movie to show what I'm trying to achieve, please click below to view: http://screenr.com/XIY MOD EDIT: formatted text to get rid of errant smilies . . . [/list] Hello I have multiple stristr condition (300+) involved in my php script (in 1 script only). I would like to know if that is okay or should i move to other solution like Mysql table search data and than direct it to accordingly. Kindly suggest some thing... the 300 condition are stored in include files (every file there is condition i.e there are 300 files in a directory which is included.) Kindly suggest some thing efficient and fast. the title sounds quite ridiculous but i dont know how else to put it... i want to return results, but my site has got quite popular, quicker than i would have thought. i want to paginate the results. but i want the results to appear at random. but the results should not appear twice... can this even be done? any questions...? thanks! I have users in my database with many stored points ie age, gender, interests etc. about 20 points in all.
I have a table of products that I want to recommend only applicable products. They each have saved like minage, maxage, gender, interest etc for the ideal consumer. ie dress gender=f
This is the bare bones of what I have so far:
$result = mysql_query("SELECT * FROM user_table WHERE hash='$session_id'"); $rowuser = mysql_fetch_array($result); $result = mysql_query("SELECT * FROM products"); $rowproducts = mysql_fetch_array($result);So now I have $rowuser['gender'] = m , how do I remove all from $rowproducts where gender = f? Is there a best way to do this knowing I have about 20 points to go through before I am left with an array with just the best selection of products for this user in it? Hello 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 Hi, I'm a beginner at PHP and am I'm trying to update an array but cant get it to work: This is my 'rates' page, There are 6 boxes that are the headings, and 6 boxes per line below for the data, I can get the headings to save but not the data lines below: rates-edit.php Code: [Select] <?php error_reporting(E_ALL); ini_set('display_errors', 2); // Include the necessary files include_once '../inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Extract details from database $sql = "SELECT title1, title2, title3, title4, title5, title6 FROM rates WHERE id=1"; $stmt = $db->prepare($sql); $stmt->execute(); $e = $stmt->fetch(); $sql2 = "SELECT 1, 2, 3, 4, 5, 6 FROM ratestable ORDER BY id ASC"; $stmt2 = $db->prepare($sql2); $stmt2->execute(); $e2 = $stmt->fetch(); ?> <!DOCTYPE html> <html lang="en"> <head> <h3>Rates Table</h3> <form method="post" action="rates-editupdate.php" enctype="multipart/form-data"> <input type="text" name="title1" maxlength="10" value="<?php echo $e['title1'] ?>" class="rates" /> <input type="text" name="title2" maxlength="10" value="<?php echo $e['title1'] ?>" class="rates" /> <input type="text" name="title3" maxlength="10" value="<?php echo $e['title3'] ?>" class="rates" /> <input type="text" name="title4" maxlength="10" value="<?php echo $e['title4'] ?>" class="rates" /> <input type="text" name="title5" maxlength="10" value="<?php echo $e['title5'] ?>" class="rates" /> <input type="text" name="title6" maxlength="10" value="<?php echo $e['title6'] ?>" class="rates" /> <?php while($e2 = $stmt2->fetch()) { ?> <input type="text" name="1" maxlength="10" value="<?php echo $e2['1'] ?>" class="rates" /> <input type="text" name="2" maxlength="10" value="<?php echo $e2['2'] ?>" class="rates" /> <input type="text" name="3" maxlength="10" value="<?php echo $e2['3'] ?>" class="rates" /> <input type="text" name="4" maxlength="10" value="<?php echo $e2['4'] ?>" class="rates" /> <input type="text" name="5" maxlength="10" value="<?php echo $e2['5'] ?>" class="rates" /> <input type="text" name="6" maxlength="10" value="<?php echo $e2['6'] ?>" class="rates" /> <?php } ?> <input id="button-upload" class="button" type="submit" name="submit" value="Save Changes" /> <input id="button-upload" class="button" type="submit" name="submit" value="Add New Row" /> </form> </body> </html> This is the page that updates the database, I cant get the loop/foreach to work... rates-editupdate.php Code: [Select] <?php error_reporting(E_ALL); ini_set('display_errors', 2); // Include the necessary files include_once '../inc/db.inc.php'; // Open a database connection $db = new PDO(DB_INFO, DB_USER, DB_PASS); // Check if coming from a POST command and Save Changes // THIS BIT WORKS :) if($_SERVER['REQUEST_METHOD']=='POST' && $_POST['submit']=='Save Changes') { $sql = "UPDATE rates SET title1=?, title2=?, title3=?, title4=?, title5=?, title6=? WHERE id=1 LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute( array( $_POST['title1'], $_POST['title2'], $_POST['title3'], $_POST['title4'], $_POST['title5'], $_POST['title6'] ) ); $stmt->closeCursor(); // // THIS IS THE BIT THAT DOES NOT UPDATE: // $sql = "UPDATE ratestable SET 1=?, 2=?, 3=?, 4=?, 5=?, 6=? WHERE id=?"; $stmt = $db->prepare($sql); if(count($_POST['1']) > 0) { foreach($_POST AS $key => $val) { $stmt->execute(array( ($key), ($_POST['2'][$key]), ($_POST['3'][$key]), ($_POST['4'][$key]), ($_POST['5'][$key]), ($_POST['6'][$key]), ($val) ) ); } $stmt->closeCursor(); } // once updated return to rates page header('Location: rates-edit.php?success=1'); exit; } ?> thanks in advance Hi, Does anyone have any experience of using PHP to transfer data from an XML file to a MySQL database? For the last 5 weeks I have been trying to get the following code to work but I am still unable to do it. The code does copy the file from my root folder into another (I eventually plan to use external download onto my server). However, it comes up with the following error: "Cannot instantiate non-existent class: xmlreader in" which refers to this: $xmlReader = new XMLReader(); Does anyone have any experience of transferring data from an XML file to a database? I am using PHP 5 so I dont know why this doesn't work. <?php ini_set('display_errors', 1); error_reporting(-1); $host="hostname"; // $username="username"; // $password="password"; // $db_name="db"; // $tbl_name="productfeed"; // // Connect to server and select database. mysql_connect("$host", "$username", "$password")or ("no connection"); mysql_select_db("$db_name")or die("Database Connection Error"); $xmlReader = new XMLReader(); $filename = "datafeed_98057.xml"; $url = "[url=http://www.ukhomefurniture.co.uk/datafeed.xml]http://www.ukhomefurniture.co.uk/datafeed.xml[/url]"; file_put_contents($filename, file_get_contents($url)); $xmlReader->open($filename); while ($xmlReader->read()) { switch ($xmlReader->name) { case'prod': $dom = new DOMDocument(); $domNode = $xmlReader->expand(); $element = $dom->appendChild($domNode); $domString = utf8_encode($dom->saveXML($element)); $product = new SimpleXMLElement($domString); $product_code = $product->prod['id']; $image = $product->awImage; //insert query if(strlen($product) > 0) { $query = mysql_query("REPLACE INTO productfeed (image) VALUES ('$awImage')"); echo $awImage . "has been inserted </br>"; die('yes it works'); error_reporting(E_ALL); if (ini_get('display_errors')) { ini_set('display_errors', 1); } } break; } } ?> MOD EDIT: DB credentials removed. Hi, Apologies to keep going on about this but I have hit a brick wall over updating my database with XML. I have spent around 100 hours on this and so far I can only copy the file, I am unable so far to update my database with the information from the XML file. I would be very grateful for any help you can offer to fix this. Code: [Select] ----Code so far----> $xmlReader = new XMLReader(); $filename = "datafeed_98057.xml"; $url = "http://www.domain.co.uk/datafeed.xml"; file_put_contents($filename, file_get_contents($url)); $xmlReader->open($filename); while ($xmlReader->read()) { switch ($xmlReader->name) { case'prod': $dom = new DOMDocument(); $domNode = $xmlReader->expand(); $element = $dom->appendChild($domNode); $domString = utf8_encode($dom->saveXML($element)); $prod = new SimpleXMLElement($domString); $id = $prod->prod['id']; $description = $prod->name; $image = $prod->awImage; $fulldescription = $prod->desc; //insert query if(strlen($prod) > 0) { $query = mysql_query("REPLACE INTO productfeed (id, description, fulldescription, image) VALUES ('$id','$description','$image','$fulldescription') "); echo $id . "has been inserted </br>"; } else{echo ("This does not work </br>");} } This is an outline of the XML feed I am using, it only includes one item which I am using as a test: Code: [Select] - <merchant id="1829" name="Pinesolutions.co.uk"> - <prod id="39920873" pre_order="no" web_offer="no" in_stock="yes" hotPick="no" adult="no"> - <text lang="EN"> <name>Oakleigh Wall Mirror 60x90</name> <desc>Mirrors are useful anywhere in the house. Not only are they functional allowing you to see your reflection in order to look your best, they also add elegance to any room theyre placed in. We offer a variety of mirrors to fit your decorating tastes as well as wall space. The Oakleigh Wall Mirror radiates an elegant simplistic style, while offering a generous size glass. The Oakleigh Wall Mirror s frame is crafted from solid hardwood and is lacquered with a protective finish to guard against dust and unexpected stains. The Oakleigh Wall Mirror is versatile and can be hung portrait style at the top of a staircase or landscape. The Oakleigh Wall Mirrors light colour means it also complements all of the furniture in our Camden Painted Range because the range has ash tops. The Oakleigh Wall Mirror has fewer knots than traditional oak wood, but is built just as sturdy so you can be certain it will last you through the generations to come.</desc> </text> - <uri lang="EN"> <awTrack>http://www.awin1.com/pclick.php?p=39920873&a=98057&m=1829</awTrack> <awImage>http://images.productserve.com/preview/1829/39920873.jpg</awImage> <mLink>http://www.pinesolutions.co.uk/bedroom-furniture/mirrors/wall-mirrors/oakleigh-wall-mirror-60x90/</mLink> <mImage>http://media.pinesolutions.co.uk/images/products/903.333.3.4.jpg</mImage> </uri> - <price curr="GBP"> <buynow>40.00</buynow> </price> - <cat> <awCatId>424</awCatId> </cat> <brand /> </prod> </merchant> 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, I'm currently trying to grab data from my database for my chatbox, which when displayed, displays in an ascending order, when I call it to descend, it shows the messages ONCE in descending order, then just repeats but in random order.. Code: [Select] $sql = "SELECT * FROM `message` WHERE `chat_id` = 1 AND `message_id` > $last ORDER BY `message_id` DESC"; Database: message_id | name 1 RSX 2 RSX 3 RSX 4 RSX Code: [Select] RSX: 4 RSX: 3 RSX: 2 RSX: 1 RSX: 4 RSX: 3 RSX: 2 RSX: 4 RSX: 3 RSX: 4 It is ordering it correctly, just repeats, I'm using Ajax, could that be the problem, or is it within the PHP code? Edit: I read it wrong, it displays as shown under "output". Lets say I have this query: $sql = mysql_query("SELECT * FROM Member ORDER BY posts DESC LIMIT 10"); while ($get = mysql_fetch_array($sql)){ $row .= $get['id']; $posts .= $get['posts']; echo "$row - $posts<br />"; } Lets say I have two of rows, with posts fields of 84 and 803. When it displays it, it grabs the 84 row first, when I want the bigger number 803 first. Is there a way to fix this? I've got a data array that looks like this: $arraylist = ('00101001','10001010','00010100','01100101'); And another that looks like this: $arrayposition = (0,3); The idea is to loop through $arraylist and when the loop gets to the indexes/positions listed in the $arrayposition array, to change all of the 1's to 0's, then put the values back into a string variable. I have something like this at the moment: $chores = 1; $arraylist_new = ''; foreach ($arrayposition as $value) { if ($chores != '') { $oldvalnum = 0; foreach($arraylist as $string) { if ($value == $oldvalnum) { $string = str_replace("1", "0", $string); if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } else { if ($arraylist_new == '') {$arraylist_new = $string;} else {$arraylist_new .= ', ' . $string;} } $oldvalnum++; } } } echo $arraylist_new; The idea is for the output to be: 00000000, 10001010, 00000000, 01100101. I know the code is hinda hectic, but could someone please help?? Thanks in advance! Hello, I've been tasked with updating our code base from mysql to something that will work with php 7. It's older code that was written by 2 or 3 individuals before me and it's not at all object oriented. There are about 540 different files that I will end up having to edit to make it all up to date. I'm not a very experienced programmer myself, and I'm in need of some guidance. There are some files that have functions that call and use mysql queries three or four files deep and going through and editing one function leads me to having to update 4 or 5 other files which breaks stuff somewhere else down the road. I was wondering if anyone has some suggestions on ways to make this process less painful. The other thing is that by using PDO in a not OO style, it seems like I'm losing a lot of its benefits. Should I consider using MYSQLi instead of PDO considering the circumstances? Are there any resources that you could point me to in order to help me learn the skills I need to make it through this upgrade? Thanks for the help! How can I store data which is in a email to a MySQL db? Currently I'm receiving all enquirers in to my INBOX. I need a way to upload data which is in the mail direct from my email page to a MySQL db. is this possible? or what's the best way to approach this? Hey guys, I have a private message script but for some reason when its updating, it turns the value into a blank one from 1 to 3. I have no idea why, but it works with the old statement of deleting, but I changed it to instead update it to 3 because I want to keep the pm in archives. here is the delete PM part. <?php session_start(); header("Location:inbox.php"); $user = $_SESSION['username']; include 'db.php'; //We do not have a user check on this page, because it seems silly to, you just send data to this page then it directs you right back to inbox //We need to get the total number of private messages the user has $sql = mysql_query ("SELECT pm_count FROM users WHERE username='$user'"); $row = mysql_fetch_array ($sql); $pm_count = $row['pm_count']; //A foreach loop for each pm in the array, get the values and set it as $pm_id because they were the ones selected for deletion foreach($_POST['pms'] as $num => $pm_id) { //Delete the PM from the database mysql_query("UPDATE messages SET recieved='3' WHERE id='$pm_id' AND reciever='$user'"); // mysql_query("DELETE FROM messages WHERE id='$pm_id' AND reciever='$user'"); //Subtract a private message from the counter! YAY! //$pm_count = $pm_count - '1'; //Now update the users message count with the new value //mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$user'"); } ?> The commented out part at the end, is the old deleting parts but instead changed it to update value. Here is my form that shows checkboxes. I showed the whole code, where it has comments. <?php //This stuff and the while loop will query the database, see if you have messages or not, and display them if you do $query = "SELECT id, sender, subject, message FROM messages WHERE reciever='$user' AND recieved='1'"; $sqlinbox = mysql_query($query); //We have a mysql error, we should probably let somone know about the error, so we should print the error if(!$sqlinbox) { ?> <p><?php print '$query: '.$query.mysql_error();?></p> <?php } //There are no rows found for the user that is logged in, so that either means they have no messages or something broke, lets assume them they have no messages elseif (!mysql_num_rows($sqlinbox) ) { ?> <center><p><b>You havent read any messages</b></p></center> <?php } //There are no errors, and they do have messages, lets query the database and get the information after we make a table to put the information into else { //Ok, Lets center this whole table Im going to make just because I like it like that //Then we create a table 80% the total width, with 3 columns, The subject is 75% of the whole table, the sender is 120 pixels (should be plenty) and the select checkboxes only get 25 pixels ?> <center> <form name="send" method="post" action="delete.php"> <table width="80%"> <tr> <td width="75%" valign="top"><p><b><u>Subject</u></b></p></td> <td width="120px" valign="top"><p><b><u>Sender</u></b></p></td> <td width="25px" valign="top"><p><b><u>Select</u></b></p></td> </tr> <?php //Since everything is good so far and we earlier did a query to get all the message information we need to display the information. //This while loop goes through the array outputting all of the message information while($inbox = mysql_fetch_array($sqlinbox)) { //These are the variables we get from the array as it is going through the messages, we have the id of the private message, we have the person who sent the message, we have the subject of the message, and yeah thats it $pm_id = $inbox['id']; $sender = $inbox['sender']; $subject = $inbox['subject']; //So lets show the subject and make that a link to the view message page, we will send the message id through the URL to the view message page so the message can be displayed //And also let the person see who sent it to them, if you want you can make that some sort of a link to view more stuff about the user, but Im not doing that here, I did it for my game though, similar to the viewmsg.php page but a different page, and with the senders id //And finally the checkboxes that are all stuck into an array and if they are selected we stick the private message id into the array //I will only let my users have a maximum of 50 messages, remeber that ok? Because that's the value I will later in another page //Here is finally the html output for the message data, the while loop keeps going untill it runs out of messages ?> <tr> <td width="75%" valign="top"><p><a href="viewmsg.php?msg_id=<?php echo $pm_id; ?>"><?php echo $subject; ?></a></p></td> <td width="120px" valign="top"><p><?php echo $sender; ?></p></td> <td width="25px" valign="top"><input name="pms[]" type="checkbox" value="<?php echo $pm_id; ?>"></td> </tr> <?php //This ends the while loop } //Here is a submit button for the form that sends the delete page the message ids in an array ?> <tr> <td colspan="3"><input type="submit" name="Submit" value="Delete Selected"></td> <td></td> <td></td> </tr> </table> </center> <?php //So this ends the else to see if it is all ok and having messages or not } ?> So yeah, does anyone know why it updates it to a blank value, from the value 1 in recieved? Hi, when someone logs in to my page, i have a form displayed with names, address, etc...what i need to do is when i hit the update button for that information to go into the MYSQL table I have created and over write that information if a user updates...so how do i do that? |