PHP - Simple "order By" Problem
Simple for you, but I am very new to just messing around with mysql functions.
I have 1 table that tracks both the item id and the amount of views the item has.
The id column is called gId, and the views column is called gplays.
All I am trying to do is get it to show a list of the latest 100 items, but sort them as most viewed.
I've looked at online tutorials to googling the function, but can't find anything that works.
Here's my current code:
$results = $db->cachegetall(300, "SELECT gId id, gName name, gDescription descr, gThumb thumb, gplays plays FROM games ORDER BY gId DESC LIMIT 100");that just shows the latest 100. When I try ORDER BY gId DESC, gplays DESC LIMIT 100");I get nothing different. If I do ORDER BY gplays DESC LIMIT 100");I get the 100 results, but they are from all items, and I want to only get it from the latest 100 items. Can someone help me? I really have no idea what these functions are doing, other than the very basic tutorials I've read on the order by function. Similar TutorialsSELECT field1 FROM table1 ORDER BY field2, field3 However I need to add something in so it also orders by a value that is returned from a calculation: SELECT field1 FROM table1 ORDER BY field2, (if field 4=="" and field 5==1 then order these records in order), field3 So they are ordered by field2, then the calculation, then field3 "ORDER BY field" would return 1, 2 3 in that order However I want it to return 3, 1, 2 (there are actually no other possible responses) How would I do this? I'm just starting to learn PHP and I'm working on something that involves two files, one HTML and another PHP. The HTML file basically accesses the PHP file anyway though, to avoid repetition in the HTML code...the actual HTML form is hardcoded within the PHP file itself. The user will be able to enter the quantity of items they want, and either add them to their shopping cart (display their order back to them on the same page as the order form) or submit the order (display their order back to them without the order form visible). I already have the shopping cart portion done, so I'm really only having problems with two things: 1) If they place their order, how do I redisplay the information without the HTML order form visible and 2) If they already have an order in their shopping cart and they place their order, how do I carry over all the information in the cart to redisplay it (my variables all seem to reset after they add to their cart and display so I can't use the same function I used to retrieve it all in the first place)? Thanks in advance...I've been trying to figure this out for over an hour now and I just need a push in the right direction If you need anything else clarified let me know, I'll be lurking... Hello! I need to add ORDER BY name ASC to this code Code: [Select] foreach($db->query("SELECT * FROM company_subcategory WHERE category_id = ".(int) $category_id) as $row) { $r[] = new CompanySubCategory($row['subcategory_id'], $row);} Tried to do it like this, but it doesn't work Code: [Select] foreach($db->query("SELECT * FROM company_subcategory WHERE category_id = ".(int) $category_id ORDER BY name ASC) as $row) { $r[] = new CompanySubCategory($row['subcategory_id'], $row);} Any help much appreciated Thanks in advance Hello, Am trying to get my records in ascending order from the database using 'ORDER BY' but they come randomly. What might be the problem?
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. 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? Hello all, I have used the phpfreaks custom order list script to create, well, a custom order list, but for some reason the Delete section of the code is not reassigning the numbers correctly after an item is deleted. It deletes the record okay, but if I have: 1 2 3 4 and delete 3, then 4 should become 3 but 4 stays as 4 until it moves two places and then it becomes 3. I am sure its related to part of this code, but please let me know if you have any ideas. Cheers Pockitz // delete row from table $sql = "DELETE FROM images WHERE imgsequenceid = '$seqid'"; $result = mysql_query($sql) or die(mysql_error()); if($result){ echo "Image $seqid successfully deleted from $id"; }else{ echo "ERROR"; } // select the info, ordering by sequenceid $sql = "SELECT imgsequenceid, imgid FROM images ORDER BY imgsequenceid"; $result = mysql_query($sql) or die(mysql_error()); // initialize a counter for rewriting sequenceid $seqid = 1; // while there is info to be fetched... while ($r = mysql_fetch_assoc($result)) { $imgid = $r['imgid']; // update the sequenceid number to the one in the next number $sql = "UPDATE images SET imgsequenceid = '$seqid' WHERE imgid = '$imgid'"; $update = mysql_query($sql) or die(mysql_error()); // inc to next avail number $seqid++; } // end while } // end if del I've got. This is my website: www.wearenip.com/home.html This is the menu that should appear on the left hand side, but doesn't: www.wearenip.com/menu.php This is my server's output saying that php is running just fine: http://wearenip.com/phpinfo.php I'm using this code in the left side bar div: Code: [Select] <?php include("menu.php"); ?> The PHP file will not display from the HTML file no matter what I do. I've tried using every possible combination of file linking including /menu.php ./menu.php and even the full http://www.wearenip.com/menu.php etc etc etc I'm genuinely upset about this. Any help would be greatly appreciated. PS: I can't get any PHP file to properly include in any HTML file on any computer I have. Nothing displays whatsoever regardless of what is in the PHP file, even if it's just a text word. Doesn't matter if it's hosted on a PHP compatible internet server or just on a local server or just on the local machine itself. Nothing. so I have an xml string the following expression $xpath = $response->xpath('/atom:feed/atom:entry/atom:content'); returns the following array SimpleXMLElement Object ( [@attributes] => Array ( [type] => application/vnd.google-earth.kml+xml ) [Placemark] => SimpleXMLElement Object ( [name] => M1 [description] => SimpleXMLElement Object ( ) [Style] => SimpleXMLElement Object ( [IconStyle] => SimpleXMLElement Object ( [Icon] => SimpleXMLElement Object ( [href] => http://maps.gstatic.com/intl/en_ALL/mapfiles/ms/micons/blue-dot.png ) ) ) [Point] => SimpleXMLElement Object ( [coordinates] => -79.395018,43.645423,0.0 ) ) ) However what I'm trying to get are the coordinates, but neither $xpath = $response->xpath('/atom:feed/atom:entry/atom:content/Placemark'); OR $xpath = $response->xpath('/atom:feed/atom:entry/atom:content/Placemark/child::*'); work I even tried just loading atom:content into an array and going from there no luck. Any ideas. Oh. BTW this might help http://code.google.com/apis/maps/documentation/mapsdata/developers_guide_protocol.html#RetrievingFeatures It shouldn't be that difficult I just don't know how to do it myself. I'm using the following code for a simple navigation, i has been placed in the content of my home page, it works fine however i don't know how to set the home page. <div id="content"> <?php $page = $_GET['page']; $file = $page.".php"; if(file_exists($file)) { include($file); } else { include "error.php"; } ?> </div> SOLVED, sorry. ... but i can't work it out. all i want to do is; if ( $this_item exists within the table ) { do a } else { do b } for example, $this_item = 'john', and i want the script to do (a) if he's in the table, or (b) if he isn't. thank you! Ok I got this column "date_reg" on my table which has a "datetime" type. It's giving me a value like "2012-04-20 04:28:03" The problem is, when I'm trying to insert on that column using this format Code: [Select] <?php $datenow = date('m/d/Y h:i:s a', time()); ?> it's giving me a value of "0000-00-00 00:00:00" which is suppose to be the current date and time the user add on that table. Anyone knows how I can fix this? Hi guys.I've got a problem.I'm building my php browsergame and i'm stuck into the inventory page.I have a code that seems ok to me but it won't work.Here it is: $inventory = array(); $query = sprintf("SELECT id, item_id, quantity FROM user_items WHERE user_id = '%s'", mysql_real_escape_string($userID)); $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $item_query = sprintf("SELECT name FROM items WHERE id = '%s'", mysql_real_escape_string($row['item_id'])); $item_result = mysql_query($item_query); list($row['name']) = mysql_fetch_row($item_result); array_push($inventory,$row); } I've spotted the problem.The $inventory array is empty.I checked it with if(empty($inventory)) .Thanks in advance and keep in mind that I'm a noob yet. Hello, I have a simple issue with a code that uses includes. What I want to do is to include a bit of php code I made to my main pages so I don't have to edit each file that will contain the same included code. The problem with that included code is that contains variables that only the main files would know. Let me show you basically what I'm talking about. mainfile1.php Code: [Select] <?php $cat = "meow"; include "content.php"; ?> mainfile2.php Code: [Select] <?php $cat = "mew"; include "content.php"; ?> (mainfile3.php and so on...) content.php Code: [Select] echo '<center>My cat said '.$cat.' today!</center>'; I was expecting to see: My cat said meow today! and My cat said mew today! But when I loaded both pages it rendered like this: My cat said '.$cat.' today! HTML works properly but the variables are ignored and displayed as plain text. What I need to do so those variables don't get ignored? Thanks. Hi! I'm a little bit new to php and i'm having a stupid issue here with mysql_real_escape_string. I have a really simple text input field into an html form and I want to save the data into a SQL table. Sometime I'm trying to insert a name with a quote like : steve's ..So I escape the text with mysql_real_escape_string but the problem is that there's a \ also inserted into the DB (steve\'s)... Is there anyway to get rid of that? Thanks in advance! Hi, This is a image tooltip from database. but I have got the problem, Not Working! How can i fix it. <a href="index.php?MD=urundetay&resimid=<?php echo $row_uruns['resimid']; ?>"><img src="urun_resim/<?php echo $row_image['image']; ?>" width="150" border="0" /></a> Hello, i am trying to create a simple funtion that i can all upon to "turn off a website" i am calling the function by <?php siteonline(n); ?> and the function is function siteonline($msg){ $offlinecheck = mysql_query("SELECT * FROM acp") or die(mysql_error()); $siteoffline = mysql_fetch_array($offlinecheck); $ifsiteoffline = $siteoffline['site_offline']; $offline = $siteoffline['offline_msg']; if ($ifsiteoffline == "y") { echo("$offline"); die();} } now this will turn off the site but it shows no message what so ever and i can not figure out why can anyone help? Cheers, |