PHP - Order By Id, Not Alphabetical
I'm pulling a list from a DB and inserting it in a drop down menu. Each item on the list has a numerical id associated called marketid in the DB. It works, but it orders the list alphabetically. I need it to order by marketid which is the column name. What code do I need to add? Here is what I have now....
<option value="<?=$market['marketid']?>"><?=$market['name']?></option> Thanks in advance all!! Tony Similar TutorialsI have a string which is a list of user id's like "44/5/6/67/7/88/56/76/9/90/65/74/8/68". I explode this string and put them into an array, but now I have a problem of getting the users first names from the database by using the id's and putting them in alphabetical order. How can I do this? Thanks How do I present these documentaries like this:
0-9
10 Things You Need to Know About Sleep
20 Animals That Will Kill You
A
Ant Kingdom
Atkins Diet
B
Battle of the Brains
Body Talk
I don't even know where to start!
I'm not asking anyone to do this for me; I just need a push in the right direction.
Any help will be appreciated.
This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=309973.0 Hi all! I'm stuck in the midst of some code. In it I have an if function which operates based on whether a variable (a word) is the same as another variable. Here is my code: Code: [Select] <? $var1 = "chicago"; if($table_name === $var1){ $result = mysql_query("SELECT * FROM another_table WHERE username='$usernamee'"); while($row = mysql_fetch_array($result) ) { $var2 = $row['var2']; $var2_new = $var2 * 2; mysql_query("UPDATE another_table SET var2 = '$var2_new' WHERE username = '$usernamee'"); } } ?>I tried using =, == and ===, but did not get any of the results I expected. Could anyone advice me on the situation? Thanks in advance. Regards, Thauwa This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=356016.0
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. I got this code from a previous thread: Code: [Select] mysql_query("SET @rows = 0;"); $res = mysql_query("SELECT @rows:=@rows+1 AS view_rank,COUNT(id) AS views, credit_members_id FROM vtp_tracking GROUP BY credit_members_id ORDER BY views DESC"); $n = array(1 => 'st', 2 => 'nd', 3 => 'rd'); while($row = mysql_fetch_row($res)) { if ( $row[2] != $members_id ) continue; if ( substr($row[0], -1) < 4 ) { $row[0] .= $n[$row[0]]; } else { $row[0] .= 'th'; } echo ' You are in ' . $row[0] . ' place with ' . number_format($row[1]) . ' views.'; break; } Everything seems ok except it orders by the "credit_members_id" and not "views" as entered. Can someone explain why, and how to fix this? Hi there, just registered and in need of help, this looks a good place to start! I'm a php beginner so please bear with me I have a mysql database which holds 3 pieces of info id: match: 1: I'm trying to sort the results using ORDER BY match ASC but for some reason it's just not for having it... Can't work out if it's an error with my php code or my table. I can order by "ID" no problem but I when I try ordering alphabetically by "match" it won't. my code is: Code: [Select] mysql_connect("***.***.***") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); $query = "SELECT * FROM employees WHERE flag = 'tv' ORDER BY match ASC"; $result = mysql_query($query) or die ("Query failed"); $numrows = (mysql_num_rows ($result)); // loop to create rows if($numrows >0){ echo "<table width = 100% border = '0' cellspacing = '2' cellpadding = '0' >"; // loop to create columns $position = 1; while ($row = mysql_fetch_array($result)){ if($position == 1){echo "<tr>";} echo " <td align = 'center'><a href=\"http://www.website.eu/sport-stream/1/{$row['id']}.html\" target=_blank>{$row['match']} <br> <img src=\"{$row['8']}\" width=\"100\" height=\"70\" /> </a> </td> "; if($position == 4){echo "</tr> "; $position = 1;}else{ $position++;} }//while $end = ""; if($position != 1){ for($z=(4-$position); $z>0 ; $z--){ $end .= "<td></td>"; } $end .= "</tr>"; } echo $end."</table> "; }//if And here's a cap of my database: any help for a php n00b would be greatly appreciated! Hello, I have classes in a database with no set UNIX date, just the day like Wednesday and in two other columns the start and end dates. I want to be able to order by the day first and then by end_time but php orders the day column by spelling and not the day it holds in chronological order. Is there anyway to change the query to order the day column as a date? See the query below? Code: [Select] $query = "SELECT * FROM zumba_timetable WHERE end_time>'$current_time' ORDER BY day, end_time ASC LIMIT 0,1"; I need to make 0, which displays as POA appear at the ned of the list not at the begining, I currently just use order by price asc and 0 is at the beginning, i need to make 0 at the end Currently i have a query which at the end has this Code: [Select] order by (value/counter) desc"); which works fine, however i was wondering, if in the case that 2 rows have the same, is it possible to set a second value to order by? I have a database of assorted. Each row contains defined QUANTITIES for each column item. Example: columns for shirt, tie, pants, socks. A row would indicate a purchase of 2 shirts, 1 tie, 0 pants, 3 socks for customer A. Followed by a row indicate a purchase of 1 shirts, 1 tie, 4 pants, 2 socks for customer B, etc. Now I want to sort EACH item, group them by quantity, and get an ordered list by quantity, that provides me with something like this: 1 shirt customer B 2 shirt customer C 3 shirt customer F 3 shirt customer H 4 shirt customer D, etc. I am using this code for my query: $query = "SELECT brisket, COUNT(brisket) FROM pass GROUP BY brisket ORDER BY brisket ASC"; but it is not organizing the info in a correlated manner. Help? 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, Really need a hand from someone mysql query is pretty basic. I have been ask to add a random order to this query. $query = "SELECT rd.user_id FROM {$regions_data_table} rd WHERE rd.field_id = {$regions_field_id} AND rd.value LIKE %s AND rd.user_id NOT IN (SELECT um.user_id FROM {$wpdb->base_prefix}usermeta um WHERE ((um.meta_key='wp_ul_disabled' AND um.meta_value=1) OR (um.meta_key='wp_ul_inactive' AND um.meta_value=1)) GROUP BY um.user_id)"; I have tried so many options but cannot seem to get it to work i am trying to insert order by rand(). Any help please? Evening everyone I wonder if you can help me with the below code <table width="823" cellspacing="0" id="mytable" summary="User Transactions Summary"> <caption>Transaction Summary</caption> <tr> <th width="136" class="nobg" scope="col" abbr="Merchant Name">Merchant Name</th> <th width="154" scope="col" abbr="Order Value">Order Value</th> <th width="145" scope="col" abbr="Comm">Cash Back</th> <th width="278" scope="col" abbr="Status">Status</th> </tr> <?php $result = mysql_query("SELECT * FROM transactions WHERE u_id = '$u_id' ORDER BY order_date DESC LIMIT 10 "); echo mysql_error() ; while($row = mysql_fetch_assoc($result)){ $m_name = $row['m_name']; $orderval = $row['order_val']; $status = $row['order_status']; $m_affid = $row['m_affid']; $query = mysql_query(" SELECT percent FROM merchants WHERE m_affid = '$m_affid' LIMIT 1"); $num = mysql_num_rows($query); $i=0; while ($i < $num) { $per=mysql_result($query,$i,"percent"); $i++; } $com1 = $orderval / 100; $com2 = $com1 * $per; $u_comm = round($com2,2); ?> <tr> <th scope="row" abbr="<?php echo $m_name;?>" class="spec"><?php echo $m_name;?></th> <td class="alt">£<?php echo $orderval ;?></td> <td class="alt">£<?php echo $u_comm ;?></td> <td class="alt"><?php echo $status ;?></td> </tr> <?php }?> </table> Basically it is meant to simply show fields from a database in a table. If there is only 1 entry for a u_id it works fine, however if their is more than one then the $u_comm value shows the same for all results, which is for the last result/row found. Hope this makes sense! Hi all, I have a script that is essentially a log file of all preious versions of an article (versioning) You can see the script I am talking about in action at: http://danielrhyshardy.com/AWT/forumadmin.php What I would like to do is, instead of displaying the title of each instance (you will see there are two instances named "apple", as they are two previous versions of an article), I would like to display that title once, and then all of the instances of that title be displayed below. I hope that makes sense. Here is the code Code: [Select] <? $sql = "SELECT id,title,message,date_added FROM messages2 ORDER BY id "; $result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR); while(list($id,$title,$message,$date_added)=mysql_fetch_row($result)){ echo '<div style="color:#bb0000;width:250px;text-align:center;float:left;margin-left:15px;margin-bottom:15px;padding-bottom:20px;"><b>'.$title.'<br>'.$date_added.'</b> <input id="'.$message.'" type="radio" name="admin[]" value="'.$id.'" onclick="javascript:document.form22.car.value=this.id" > </font></div>'."\n"; } ?> I would use something like Code: [Select] if ($title = "apple") { but this would obviously not work for new posts, unless I wanted to constantly update my code. I am sure there is a simple way to achieve this. Perhaps someone can tell me!? Thanks in advance Dan I am attempting to order the results of a query using the get value but they wont order. here is the form: echo "<form name=\"order_form\" action=\"{$site_root}/index.php\" method=\"GET\">"; echo "<input type=\"hidden\" value=\"{$forum_id}\" name=\"forum\" />"; echo "<select name=\"order_by\" style=\"margin-left:5px;\">"; echo "<option value=\"asc\" name=\"order_option\" />Order By: Ascending</option>"; echo "<option value=\"desc\" name=\"order_option\" />Order By: Descending</option>"; echo "</select>"; echo "<select name=\"sort_by\" style=\"margin-left:5px;\">"; echo "<option value=\"topic_name\" name=\"sort_option\" />Sort By: Topic Name</option>"; echo "<option value=\"topic_poster\" name=\"sort_option\" />Sort By: Topic Author</option>"; echo "<option value=\"topic_time_posted\" name=\"sort_option\" />Sort By: Time Posted</option>"; echo "<option value=\"topic_views\" name=\"sort_option\" />Sort By: Topic Views</option>"; echo "<option value=\"topic_replies\" name=\"sort_option\" />Sort By: Topic Replies</option>"; echo "<option value=\"topic_last_poster\" name=\"sort_option\" />Sort By: Last Poster</option>"; echo "<option value=\"topic_last_post_time\" name=\"sort_option\" />Sort By: Last Post Time</option>"; echo "</select>"; echo "<input type=\"submit\" value=\"Order\" />"; echo "</form>"; here is the query: $topic_info_query = $db->query("SELECT f.forum_id, f.forum_name, m.user_id, m.user_username, m.user_group, t.thread_topic_id, t.topic_name, t.topic_poster, t.topic_time_posted, t.topic_views, t.topic_replies, t.topic_last_poster, t.topic_last_post_time, t.topic_locked, t.topic_sticky, t.topic_edited, t.topic_last_poster_id, t.topic_last_poster_group, t.topic_icon FROM ".DB_PREFIX."topics as t LEFT JOIN ".DB_PREFIX."members as m ON t.topic_poster = m.user_username LEFT JOIN ".DB_PREFIX."forums as f ON t.forum_id = f.forum_id WHERE t.forum_id = '$forum_id' '".$sort_by . ' ' . $order_by."'") and here is where $sort_by and $order_by are defined: if (isset($_GET['order_by'])) { $order_by = mysql_real_escape_string($_GET['order_by']); $order_by = strtoupper($order_by); } if (isset($_GET['sort_by'])) { $sort_by = 'ORDER BY t.'.mysql_real_escape_string($_GET['sort_by']); } when i echo '".$sort_by . ' ' . $order_by."' which is how it appears in the query i get: t.topic_last_post_time DESC which is exactly right. But the results are not being sorted. The echoed variables change as does the url but no sorting happens. any ideas? Can someone please explain to me what's the importance of node order? Does certain nodes that appear first or last have impact on traversing time? Any suggestions much appreciated! Currently I have where the User can search for records, then the records are displayed on a page. I then have a drop down box to where the user can Order by 2 different categories to make it easier for the user to see things but for some reason it is not working. Here is my code. Oh and the error I get is mysql_num_rows(): supplied argument is not a valid MySQL result resource in <?php $qry_str="SELECT * FROM timeslip WHERE 1 "; if($_POST['Initials']) { $Initials=$_POST['Initials']; $qry_str.="and Initials='$Initials' "; } if($_POST['Identifier']) { $Identifier=$_POST['Identifier']; $qry_str.="and Identifier LIKE '%$Identifier%' "; } if($_POST['Type']) { $Type=$_POST['Type']; $qry_str.="and Type LIKE '%$Type%' "; } if($_POST['Terms']) { $Terms=$_POST['Terms']; $qry_str.="and Terms LIKE '%$Terms%' "; } if($_POST['Memo']) { $Memo=$_POST['Memo']; $qry_str.="and Memo LIKE '%$Memo%' "; } if($_POST['date1']) { $date1=$_POST['date1']; $date2=$_POST['date2']; $start=date('Y-m-d', strtotime($date1)); $end=date('Y-m-d', strtotime($date2)); $qry_str.="and Date >= '$start' and Date <= '$end' "; } if($_POST['order1'] && $_POST['order2']) { $order1=$_POST['order1']; $order2=$_POST['order2']; $qry=$_POST['qry']; $qry_str="$qry ORDER BY $order1 ASC, $order2 ASC "; } if($_POST['order1']) { $order1=$_POST['order1']; $qry=$_POST['qry']; $qry_str="$qry ORDER BY $order1 ASC"; } if($_POST['order2']) { $order2=$_POST['order2']; $qry=$_POST['qry']; $qry_str="$qry ORDER BY $order2' ASC "; } $result=mysql_query($qry_str); $count=mysql_num_rows($result); if ($count>0){ ?> <form action="" method="post"> <input type="hidden" value="<?=$qry_str?>" name="qry"/> Order Results By <select name="order1" > <OPTION value="<?=$order1?>"><?=$order1?></OPTION> <OPTION value="Identifier">Identifier</OPTION> <OPTION value="Type">Type</OPTION> <OPTION value="Terms">Terms</OPTION> <OPTION value="Date">Date</OPTION> <OPTION value="Cost">Cost</OPTION> </select> Then <select name="order2" > <OPTION value="<?=$order2?>"><?=$order2?></OPTION> <OPTION value="Identifier">Identifier</OPTION> <OPTION value="Type">Type</OPTION> <OPTION value="Terms">Terms</OPTION> <OPTION value="Date">Date</OPTION> <OPTION value="Cost">Cost</OPTION> </select> <input type="submit" name="submit"> </form> |