PHP - How To Order A Sql Table
by the order in which they were entered?
like i dont want to order by a specific row in my table, i want to list the data by which it was entered.
maybe i can add a row that enters column #s somehow??
Similar TutorialsI've got. Hi, I have a table set up with 10 items in it, I want to allow users to customize the order of the items when they are logged in. How can I call the items from the table in the order set by the user? Thanks for any help. This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321906.0 Hello
I have a mysql table like this:
id | user | car 1 | 1 | fiat 500 2 | 2 | vw polo 3 | 2 | vw golf 4 | 3 | renault clio 5 | 2 | fiat panda 6 | 3 | seat ibiza From this table how can i get a query that the result be like: user 2 - 3 cars user 3 - 2 cars user 1 - 1 cars Thanks Hi, I'm a researcher (and complete coding noob), and am planning a longitudinal study that requires e-mail follow-up with subjects taking an initial survey. For purposes of ethics/anonymity due to sensitive survey data, I'd like the acquired e-mails to be saved uncoupled from the survey responses; this is simple to deal with, and I use a basic PHP e-mail form, which injects the email address in a table in MySQL in a different server than the one used for the survey. The issue is that the e-mails are saved in the MySQL database in order of injection, thus it is still theoretically possible for me to link the e-mails back to the survey responses (which have a time stamp that I cannot remove). Ideally I would like not to be able (at all) to link the e-mails to the survey responses, and one way to do that (since I don't save the e-mail injection timestamps in MySQL) might be to have the e-mails saved in MySQL in a random order. Not sure if this is possible, and not even sure if this would be via PHP or MySQL side of things. The server is on godaddy and uses Starfield interface for MySQL but I cannot find an option for random insert/saving of table items (emails). They are saved in order of injection. Any solution for this? Thanks, What I'm trying to do here is generate a table based on a form in which the user selects two options. The first option tells the script which database entries to put in the table, the second option tells it how to arrange them. The first works perfectly, the second not at all - it doesn't produce an error, it just doesn't do anything. I've found a number of tutorials that seem to suggest that the problem is somewhere in my punctuation around ORDER BY '$POST[sort]' but I've been unable to find a solution that actually works. Any help would be much appreciated, my code is below. Thank you! mysql_select_db($database, $con); $result = mysql_query("SELECT * FROM main WHERE state='$_POST[state]' ORDER BY '$POST[sort]'"); if(mysql_num_rows($result)==0){ echo "<p align='left'>View Pantries by State</p><div id='stateform'> <form action='../admin/viewstate.php' method='post'> <select name='state' /> <option value='AL'>Alabama</option> <option value='AK'>Alaska</option> <option value='AZ'>Arizona</option> </select> <select name='sort' /> <option value='name'>Name</option> <option value='id'>Id</option> <option value='city'>City</option> <option value='zip'>Zip</option> <option value='timestamp'>Timestamp</option> </select> <input type='submit' value='Go'></input></form> </div>"; } else{ echo "<p align='left'>View Pantries by State</p> <div id='stateform'> <form action='../admin/viewstate.php' method='post'> <select name='state' /> <option value='AL'>Alabama</option> <option value='AK'>Alaska</option> <option value='AZ'>Arizona</option> </select> <select name='sort' /> <option value='name'>Name</option> <option value='id'>Id</option> <option value='city'>City</option> <option value='zip'>Zip</option> <option value='timestamp'>Timestamp</option> </select> <input type='submit' value='Go'></input></form> </div>"; echo "<div id='fptext'><span class='h1'>Food Pantries for " . $_POST['state'] . "</span><br><br></div>"; } echo "<table border='1' align='center' cellpadding='3' width='900px'> <tr> <th>ID</th> <th>Name</th> <th>Type</th> <th>Address</th> <th>State</th> <th>Phone</th> <th>E-mail</th> <th>Website</th> <th>Hours</th> <th>Requirements</th> <th>Additional Information</th> <th>Lat</th> <th>Lng</th> <th>Update</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['id'] . "</td>"; echo "<td>" . $row['name'] . "</td>"; echo "<td>" . $row['type'] . "</td>"; echo "<td>" . $row['address'] . "</td>"; echo "<td>" . $row['state'] . "</td>"; echo "<td>" . $row['phone'] . "</td>"; echo "<td>"; echo "<a href=mailto:".$row['email'].">".$row['email']."</a>"; echo "</td>"; echo "<td>"; echo "<a href=".$row['website']."\>".$row['website']."</a>"; echo "</td>"; echo "<td>" . $row['hours'] . "</td>"; echo "<td>" . $row['requirements'] . "</td>"; echo "<td>" . $row['additional'] . "</td>"; echo "<td>" . $row['lat'] . "</td>"; echo "<td>" . $row['lng'] . "</td>"; echo "<td><a href='../public/updatepage.php?id=".$row['id']."'>Update Pantry</a></td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>
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 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? 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 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! 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 posted this in mysql as well but maybe someone would know the php/mysql syntax to do this: How to re order records after a delete or move? I have table of navigational items. lets say under "about us" i have 5 sub links and i order them by a field called theorder. How can a re order them properly if one is deleted? orders are 1,2,3,4,5 and if i delete the record that is 3 then the order is 1,2,4,5 but i want it to now become 1,2,3,4. How do i do this properly? Thanks 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? Greetings! I am really stock on this situation. 1. I have a variable $uc_order_created. 2. I have a code like this: <b><?php echo t('Due Date:'); ?></b> </td> <td width="98%"> <?php $duedate = mktime(0,0,0,date("m"),date("d")+30,date("Y")); echo "".date("F jS Y", $duedate);?> 3. How do I code if I want the result of $uc_order_created get displayed as Due Date using the code above? Or other modifications? 4. The situation now, the code is working when printing the invoice but its not based on $uc_order_created. So, every time I will edit the Order because of the change of items among others the Due Date will also update. All I want that when Order is edited the value of Due Date will still remain based on the $uc_order_created. Thank you so much in advance for your help. I'm just trying to see if the order of my logic is correct or if there's something I can do differently. I'm also curious to know at what point I should get the 2 passwords that exists in the database for the user. These are two different passwords. Code: [Select] function login_submit() { $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean'); $this->form_validation->set_rules('remember', 'Remember me', 'integer'); $user_id = $this->users->get_user_id_by_username($this->input->post('username')); if ($user_id !== 0) { if ($this->kow_auth->is_max_login_attempts_exceeded($user_id)) { echo json_encode(array('error' => 'yes', 'message' => 'Your account is currently locked, we appologize for the inconvienence. You must wait 10 minutes before you can login again!')); } else { $user_status = $this->users->get_user_status($user_id); if ($user_status == 1) { echo json_encode(array('error' => 'yes', 'message' => 'Sorry you must verify your account before logging in!')); } elseif ($user_status == 3) { echo json_encode(array('error' => 'yes', 'message' => 'Your account has been suspended!')); } elseif ($user_status == 4) { echo json_encode(array('error' => 'yes', 'message' => 'Your account is currently banned!')); } elseif ($user_status == 5) { echo json_encode(array('error' => 'yes', 'message' => 'Your account has been deleted!')); } else { if ($this->form_validation->run()) { if ($this->kow_auth->login($this->form_validation->set_value('username'), $this->form_validation->set_value('password'), $this->form_validation->set_value('remember'))) { redirect(''); } else { echo json_encode(array('error' => 'yes', 'message' => 'Incorrect username and password combination!')); } } } } } else { echo json_encode(array('error' => 'yes', 'message' => 'Incorrect username and password combination!')); } } I teach 6 different class groups, named "A" to "F", respectively. Script:
<?php $tqs_admin_flag = "SELECT * FROM `flags`"; $tqr_admin_flag = mysqli_query($dbc, $tqs_admin_flag) or die(mysqli_error($dbc)); while($row_admin_flag = mysqli_fetch_array($tqr_admin_flag)){ //print_r($row_admin_flag); $tqs_admin_flag_thread = "SELECT * FROM `thread` WHERE `id` = '" . $row_admin_flag['thread_id'] . "' ORDER BY `date_created` DESC"; $tqr_admin_flag_thread = mysqli_query($dbc, $tqs_admin_flag_thread) or die(mysqli_error($dbc)); while($row = mysqli_fetch_assoc($tqr_admin_flag_thread)){ include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/admin_flag_thread.php"); } } ?>This in comparison works: $tqs_admin_flag_thread = "SELECT * FROM `thread` ORDER BY `date_created` DESC";I would appreciate suggestions for which reasons the above script (the way it is) may not work. With the above script the querying and printing on screen does happen, though the "ordering by DESC" does not happen. Edited by glassfish, 24 October 2014 - 11:38 AM. Hello, I have a little thing I am making, and it is a Toplist. People can vote for a user, and then there vote will go up. But people can only vote once per day on one IP address. I am having a problem ordering my front page by votes though. Here is my code for it to load the votes, and to display the user's on the list. <?php include 'vote.php' $list_q = mysql_query("SELECT * FROM toplist WHERE premium='1' && activated='1' && ban='0' && staff='0' ORDER BY '$voteAmount'") or die (mysql_error()); while($list_f = mysql_fetch_assoc($list_q)) { list ($error, $message, $voteAmount) = voteAmount(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, $list_f['id']); ?> <tbody style="" id="cat_1_e"> <!-- start: forumbit_depth2_forum --> <tr> <td class="trow2" align="center" valign="middle" width="1"></td> <td class="trow2" valign="middle"> <strong><a href="server.php?view=details&id=<?php echo $list_f['id'];?>"><?php echo capitalizeFirstCharacter($list_f['servername']);?></a></strong><div class="smalltext"><?php echo $list_f['shortdescription'];?></div> </td> <td class='trow1' valign='middle' align='left' style='white-space: nowrap'><span class='smalltext'><?php echo $list_f['revision'];?></span></td> <td class="trow2" valign="middle" align="right" style="white-space: nowrap"><font size="4px"><?php echo $voteAmount;?> Votes</font></td> </tr> <!-- end: forumbit_depth2_forum --> </tbody> <?php } ?> And here is the code to my vote.php file that it loads all of the data from. <?php function install($host, $user, $pass, $db) { $c = @mysql_connect($host, $user, $pass); if(mysql_select_db($db, $c)) { mysql_query('CREATE TABLE `'.$db.'`.`votes` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `ip` VARCHAR(50) NOT NULL, `serverId` INT(50) UNSIGNED NOT NULL, `date` DATETIME NOT NULL, PRIMARY KEY (`id`) ) ENGINE = InnoDB;'); return true; } else { return false; } } function voteAmount($host, $user, $pass, $db, $serverId) { $c = @mysql_connect($host, $user, $pass); if($c) { if(mysql_select_db($db, $c)) { if($getVotes = mysql_query('SELECT * FROM `'. $db .'`.`votes` WHERE serverId='. $serverId)) { if(mysql_num_rows($getVotes) >= 1) { $getVoteQ = mysql_query('SELECT COUNT(id) AS total FROM `'. $db .'`.`votes` WHERE serverId='. $serverId); $vote = mysql_fetch_assoc($getVoteQ); return array(0, "", $vote['total']); } else { return array(0, "", 0); } } else { if(install($host, $user, $pass, $db)) { return array(1, "The vote script has successfully been installed.<br>Please reload the page.", 0, 0); } else { return array(1, "Could not connect to DB.", 0); } } } else { return array(1, "Error connecting to DB.", 0); } } else { if(install($host, $user, $pass, $db)) { return array(1, "The vote script has successfully been installed.<br>Please reload the page.", 0, 0); } else { return array(1, "Could not connect to DB.", 0); } } } // list ($error, $message, $voteAmount) = voteAmount('HOST', 'USER', 'PASS', 'DB', $_SERVER['REMOTE_ADDR'], SERVER_ID); function votedToday($host, $user, $pass, $db, $hours, $ip, $serverId) { $c = @mysql_connect($host, $user, $pass); if($c) { if(mysql_select_db($db, $c)) { if($getVotes = mysql_query('SELECT * FROM `'. $db .'`.`votes` WHERE ip="'. $ip .'"')) { if(mysql_num_rows($getVotes) >= 1) { $dateQ = mysql_query('SELECT MAX(date) AS lastVoteDate FROM `'. $db .'`.`votes` WHERE ip="'. $ip .'"') or die(mysql_error()); $getDate = mysql_fetch_assoc($dateQ); $diffrence = time() - strtotime($getDate['lastVoteDate']); if (($diffrence / 60 / 60) >= $hours) { mysql_query('INSERT INTO `'. $db .'`.`votes` (`ip`, `serverId`, `date`) VALUES ("'. $ip .'", '. $serverId .', NOW())') or die(mysql_error()); return array(0, "Your vote has been added."); } else { return array(1, "You can only vote every ". $hours ." hours. So far ". number_format(($diffrence / 60 / 60), 2) ." hours have past."); } } else { mysql_query('INSERT INTO `'. $db .'`.`votes` (`ip`, `serverId`, `date`) VALUES ("'. $ip .'", '. $serverId .', NOW())') or die(mysql_error()); return array(0, "Your vote has been added."); } } else { if(install($host, $user, $pass, $db)) { return array(1, "The vote script has successfully been installed.<br>Please reload the page."); } else { return array(1, "Could not connect to DB."); } } } else { return array(1, "Error connecting to DB."); } } else { if(install($host, $user, $pass, $db)) { return array(1, "The vote script has successfully been installed.<br>Please reload the page."); } else { return array(1, "Could not connect to DB."); } } } // list ($error, $message) = votedToday('HOST', 'USER', 'PASS', 'DB', HOURS, $_SERVER['REMOTE_ADDR'], SERVER_ID); ?> Once again, I am trying to make it so the list is ordered by how many votes that server has. Here is an example of the votes database. Code: [Select] CREATE TABLE IF NOT EXISTS `votes` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `ip` varchar(50) NOT NULL, `serverId` int(50) unsigned NOT NULL, `date` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ; INSERT INTO `votes` (`id`, `ip`, `serverId`, `date`) VALUES (6, '::1', 4, '2010-08-07 11:44:44'); |