PHP - Moved: Sorting From Mysql Bug???
This topic has been moved to MySQL Help.
http://www.phpfreaks.com/forums/index.php?topic=306094.0 Similar TutorialsThis topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=317320.0 Ok, So I have a table with customers... But after you add one. The <select> that shows up is unorganized... Code: [Select] $con = mysql_connect("localhost","techportal","tech2196"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("innerlink_customers", $con); $result = mysql_query("SELECT cust_id, customer FROM customerinfo"); while($row = mysql_fetch_assoc($result)){ echo "<option value='" . $row['cust_id'] ."'>" . $row['customer'] . "</option>"; } This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=323002.0 This topic has used a waypoint to get to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=359489.0 How do I like it, you ask? Quite well. Besides the downtime Tuesday I haven't had any server problems, and the game itself is great. Very D2-esque but with very noticeable enhancements. This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=320518.0 Hi guys, this is my first post so be nice! I have an SQL select which returns the following cat_id cat_name parent_id 1 cat 1 0 2 cat 2 0 3 cat 3 0 4 cat 4 0 5 cat 5 1 6 cat 6 1 7 cat 7 0 The above shows that category 1 has 2 children. I have a script in place that basically filters the child entries into a new array, then I use two foreach loops to build a list of items with parent > child relationship. My code works, but is a mess and doesn't 'feel' as concise as it should be. What would be the best / most efficient way to build a child > parent list. (ideally i would want it to work on up to 4 tiers) Any help would be greatly appreciated. Cheers Dave okay so for my final project for my php class we have to build a video management system. I've got video upload, viewing, and user registration working, and the videos display themselves in my playlist using this code posted below. What I want to do is add a hyperlink above the code that when clicked by the user can sort the videos by a column in my mysql database and then display them in the format shown below (sort by views, or data posted, or alphabetically by title) Code: [Select] <div id="videoList"> <?php require_once('config.php'); $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db(DB_DATABASE, $con); $result = mysql_query("SELECT * FROM video_list"); while($row = mysql_fetch_array($result)) { echo "<div title=\"<b>title: </b>" . $row['title'] . "<br />"; echo "<b>user: </b>" . $row['username'] . "\">"; echo "<p title=\"description\"><span class=\""title\">" . $row['title'] . "</span><br/>"; echo "<br/>" . $row['info'] . "<br/>"; echo "<br/><a href=\"users/" . $row['username'] . "\" target=\""_blank\">visit my page</a></p>"; echo "<ul title=\"preview\"><li title=\"video/preview/" . $row['ogv_preview'] . "\">video/ogg</li>"; echo "<li title=\"video/preview/" . $row['mp4_preview'] . "\">video/mp4</li></ul>"; echo "<ul title=\"main\"><li title=\"video/main/" . $row['ogv_filename'] . "\">video/ogg</li>"; echo "<li title=\"main/preview/" . $row['mp4_filename'] . "\">video/mp4</li></ul>"; echo "<ul title=\"thumb\"><li title=\"video/thumb/" . $row['thumb'] . "\"></li></ul>"; echo "<ul title=\"poster\"><li title=\"video/poster/" . $row['poster'] . "\"></li></ul></div>"; } mysql_close($con); ?> </div> this generates a piece of html code that looks like this. 1 block of code for each video in the mysql database. i just need to be able to sort this code, can anyone help me out?? THANKS! Code: [Select] <div title="<b>title: </b>funny video<br /> <b>user: ijmcg</b>"> <p title="description"> <span class=""title">funny video</span><br/><br/>blah blah blah blah<br/><br/> <a href="users/ijmcg" target=""_blank">visit my page</a> </p><ul title="preview"> <li title="video/preview/01.ogv">video/ogg</li> <li title="video/preview/01.mp4">video/mp4</li></ul> <ul title="main"><li title="video/main/01.ogv">video/ogg</li> <li title="main/preview/01.mp4">video/mp4</li></ul> <ul title="thumb"><li title="video/thumb/01.jpg"></li></ul> <ul title="poster"><li title="video/poster/01.jpg"></li></ul> </div> I'm working on a store locator style program. I first get and sort all zip codes based on a their distance to an original location. I then use that zip code array to find all stores in the mysql database, but when I get the results, they're no longer arranged by distance from the original location.. and I can't just sort them ascending or descending based on their zip codes because that doesn't sort them correctly either. So I need to sort the data I get from the mysql database based on their "zip_code" column and in order from the $zip array.. Here's an example of what $zip looks like.. the zip code is the key and the distance from the original zip is the value [meaning the first key is the $originalZip]: Array ( [70601] => 0 [70616] => 1.12 [70629] => 1.2 [70612] => 1.24 [70602] => 1.31 [70615] => 1.88 [70609] => 3.41 [70605] => 4.4 [70669] => 4.62 [70606] => 4.9 [70611] => 7.23 [70607] => 9.68 [70663] => 9.74 ) and then I create the $WHERE variable by each key supplied: $WHERE = "WHERE zip_code='$originalZip'"; foreach ($zip as $key => $value) { if ($key == $originalZip) {} else { $WHERE .= " OR zip_code='$key'"; } } } Then I do the query [with a paginator]: $query = query("SELECT * FROM " . $db['prefix'] . "stores $WHERE LIMIT $from, $maxResults"); but when I print the data, it doesn't keep the correct sorting format.. if (mysql_num_rows($query) > 0) { for ($i = 0; $i<mysql_num_rows($query); $i++) { $storeData = mysql_fetch_array($query, MYSQL_ASSOC); // print store information here such as $storeData['name'] $storeData['address'] $storeData['zip_code'] } } Is there an easy way to sort the mysql results based on the $zip array? Say I have a table called artists with these fields (artistid, rank).
Then say I have an array called $rankadjustments with a value for each artist (like 1, 7, 3,-3, 5, 9, etc).
I am doing a MYSQL query that gets the info from artists table and sorts according to the rank field like this...
$sql = "SELECT * FROM artists ORDER BY rank";Easy enough. But what if I would like to bring that data back sorted by (rank + rankadjustment). For example, if the artist ranked 1st had a rank of "1" from mysql, but had a rankadjustment of "5" from the rankadjustment array, his "true" rank would be "6". Again, rankadjustment is NOT a field in my table, otherwise it would obviously be simple. It's calculated on the fly and stored in an array. FYI, the array has an index with their "artistid". For example, $rankadjustment[341][8] would be the artist with an artistid of "341" has a rank adjustment of 8. Is there a way to do this IN the query itself? It doesn't seem possible but wanted to find out for sure. If not, what is best way to do? I assume... Get the data sorted JUST by rank and put it all into an array, then create a new array that adds rank to rankadjustment and reorder by the "new" rank amount? I guess that wouldn't be too bad but again, wanted to make sure I'm not missing something that would allow me to do it all in the query (or just more efficiently). Thanks! Edited by galvin, 21 December 2014 - 09:13 PM. This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=310879.0 Hi - i need help with the fourth column named "jobnr" it has to be the highest number first and lowest at the bottum of the tabel. I tried different variation like "mysql_query("SELECT * FROM tabel ORDER BY jobnr DESC")" but with no success. I attached a picture og the working script output Code: [Select] <html> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/> </head> <style type="text/css"> .myclass { font-size: 8pt; font-face: Verdana; } </style> <body> <?php // Define variables $host="host"; // Host name $username="user"; // Mysql username $password="password"; // Mysql password $db_name="database"; // Database name $tbl_name="tabel"; // Table name // Connect to server and select databse mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // saetter db udtraek til UTF-8 endcoding mysql_set_charset('utf8'); // henter db data fra tabllen: jobpositons $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); // Define $color=1 $color="1"; echo '<table border=0" bordercolor="#f3f3f3" cellpadding="1" cellspacing="1">'; echo "<tr bgcolor='#00aeef'> <th>Jobtitel</th> <th>Sted</th> <th>Oprettet</th> <th>jobnr</th> </tr>"; // sortere sql db data og indsaetter i html tabel while($rows=mysql_fetch_array($result)) { // If $color==1 table row color = #ffffff if($color==1){ echo "<tr bgcolor='#ffffff'><td class='myclass'>".$rows['Jobtitel']."</td><td class='myclass'>".$rows['Sted']."</td><td class='myclass'>".$rows['oprettet']."</td><td class='myclass' align='right'>".$rows['jobnr']."</td> </tr>"; // Set $color==2, for switching to other color $color="2"; } // When $color not equal 1, use this table row color else { echo "<tr bgcolor='#f3f3f3'> <td class='myclass'>".$rows['Jobtitel']."</td><td class='myclass'>".$rows['Sted']."</td><td class='myclass'>".$rows['oprettet']."</td><td class='myclass' align='right'>".$rows['jobnr']."</td> </tr>"; // Set $color back to 1 $color="1"; } } echo '</table>'; mysql_close(); ?> </body> </html> This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=353173.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=316440.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308924.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=320124.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312323.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=344992.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=321649.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=358188.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=325846.0 |