PHP - Problem With Sorting Links
Hi Everyone,
I have a page on my website that displays a list of products from a mysql database in a table. Currently when the column titles are clicked, the data is sorted in order with the highest first. $sort = $_GET['sort']; if ($sort == 'price') { $query = mysql_query("SELECT * FROM products ORDER BY price DESC"); } else{ $query = mysql_query("SELECT * FROM products ORDER BY product"); } ... echo "<a href='products.php?sort=price'>Price</a>"; Does anybody know how i would go about setting it up so that if the sort link is clicked a second time, the data is re ordered from descending to ascending? Then if the click is clicked again it would change back to descending, and so on.....? Many thanks Similar TutorialsHello to all phpfreaks out there I need help with sorting some database columns. I ve been searching all day but i didnt found something nice to implement (or my mind is too tired to think right at this specific time) I have a table and inside it i display a picture and some other data.Above the table i inserted a div and inside it i have 5 links which when clicking on them i want to change the display order of the data being displayed in the table, and if clicked again to change again the state for ASC to DESC and so on... Is there any short solution that do the trick? With an array maybe? PS:I have also a pagination which i would like to keep the sorted order from the above table so not to mess around with the results. Thanks in advance I'm trying to figure out a way to incorporate multiple sorts on my php page. On the page of products, I want users to have three choices of links: one by date, one by price - low to high and one by price - high to low. I know how to perform ASC & DESC sorts in MySql but I'm not sure how to go about this sort with the three choices without creating more pages. I would really like it to stay with the same page. Is this even possible?? How would I go about it? Here is the section of code : $get_items_sql = mysql_query("SELECT id, thumb, username, sub_id, title, ROUND(price,2) AS price, date FROM product WHERE CURDATE() <= relist_date AND inactive IS NULL AND sold IS NULL AND cat_id = '1' ORDER BY date ") or die(mysql_error());; if (mysql_num_rows($get_items_sql) < 1) { $content = "<p><em>Sorry, no items in this category.</em></p>\n"; } else { $col = 0; $content .= "<ul>\n"; while ($items = mysql_fetch_array($get_items_sql)) { $item_url = "items3.php?id={$items['id']}&username={$items['username']}"; $item_title = stripslashes($items['title']); $item_price = $items['price']; $item_photo = $items['thumb']; $item_username = $items['username']; $item_date = $items['date']; $content .= ""; list($width) = getimagesize("image_files/{$item_photo}"); // set the maximum width of the image here $maxWidth = 100; if ($width > $maxWidth); $content .= "<table width=\"693\" border=\"0\" class=anotherfont><tr><td width=\"101\"> <a href=\"{$item_url}\"> <img alt=\"{$item_title}\" border=0 width=\"{$maxWidth}\" src=\"image_files/{$item_photo}\" /></a><td width=\"200\"> <a href=\"{$item_url}\">{$item_title}</a></td> <td width=\"109\">{$item_username}</td><td width=\"101\"> {$item_date}</td><td width=\"99\">\${$item_price} USD </td></tr></table>"; $content .= "\n"; 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 Hi all, I am currently returning candidates from a database initially just by the highest candidate id to the lowest... i.e. newest candidates to oldest. I have introduced a sort feature which works just fine, the problem is I am displaying my results over a few pages using pagination. The problem arises once I sort my results and then try to click onto the next page of results. Basically it is just resetting the sort as the sort relies on a button being pressed, which obviously isn't happening when the user is trying to get the next page of results. I'm not sure exactly which section of code would be relevant to this so I have posted the whole script... additionally if you need to take a look at it in action you can do so he http://www.beta.teachingagencies.co.uk/search_jobseekers.php As I say I know the problem is that when the page is changed the value is lost, but I don't have a clue how to solve the problem! Any help or advice would be appreciated. Many thanks, Greens85 Hi! I have an odd problem... I inserted a flash file in a php doc, and basically what happens is that on some computers, the link does not work without the www extension ... by that i mean if people go to http://mysite.com they will not be able to click the banner but if they put http://www.mysite.com it works... Very odd as the link is part of the flash movie and it only happens on some computers not all ... anyone would have an idea as to what could cause this ? Alternativly what can I do with this piece of code that does not seem to do the trick either Code: [Select] <div class="subtitulo"><a href="http://hostelsuites.com/hostel-banner.php?Buenos+Aires+Hostel+Suites+Florida&id_hostel=30&idioma=3&id_banner=2"><?php echo ucfirst($lang["expanish"]) ?></a><br /> <br/></div> Many thanks for any help ! I am having trouble get links to email when the Link Text does not match the link. <a href=\"http://mysite.org/?upfriday.php&e=$email&v=$v&r=no\">I do not need a ride this Friday</a> Just prints the text with no link, while: <a href=\"http://mysite.org/?upfriday.php&e=$email&v=$v&r=no\">http://mysite.org/?upfriday.php&e=$email&v=$v&r=no</a> Sends the link just fine. Is there a workaround for this? Or is this just a limitation of PHP? The thing is that I want to put 3 options in each email... each of which sends a different response to a script that will enter their response into the database. I'm afraid if the links are this long and non-descriptive, it will confuse my users. Thanks for any help. Hello guys i want your help in adfly clone script , i have it installed it but i have problem - shrinking links not working on main page, but working good when user logged in all server Requirements are ok my website: adglo.be script: mediafire vt: virustotal regards, Edited by ForexSon, 09 August 2014 - 06:49 PM. <html> <?php $id = $_GET['id']; $dbusername="web148-matt"; $dbpassword="matt"; $dbdatabase="web148-matt"; mysql_connect(localhost,$dbusername,$dbpassword); @mysql_select_db($dbdatabase) or die( "Unable to select database"); mysql_query("UPDATE count SET clicks=clicks+1 WHERE id='$id'"); $sql = mysql_query("SELECT link FROM count WHERE id='$id'"); $fetch = mysql_fetch_row($sql); $result = mysql_query("SELECT * FROM count"); while($row = mysql_fetch_array($result)) { echo "<a href=" .$row['link']. ">Link</a>"; } ?> <a href='http://www.google.com'>Google</a> <a href='/index.php?id=2'>link2</a> </html> Just a further question from a earlier post that was complete if i do a search and i want to order them from a drop box the if statement would it just look like Quote "select * from Table order by '".$_POST['order']."'" Or would this not work ? Thanx i need to sort my database results by the day they were added to the database. There is a column with the timestamp of when they were inserted but how do i group them by the day they were inserted so i can display them as such: Mon 14th: 5 records Tues 15th: 11 records etc. I have an array like this: $array[$row] = array ( 'name' = $name, 'discount' = $discount, 'price' = $price ); Now, I have about 200 of those in that array, and im trying to sort by the discount, highest 1st. How would I go about doing that? Thanks. Part of my code: Code: [Select] <?php $arr = array ("Christina", "Daniel", "Andreea); reset($arr); foreach ($arr as $username) { //more code here ... foreach ($stats as $result) { echo $username."'s points - ".$result."<br />"; } } ?> And gives this: Code: [Select] Christina's points - 14 Daniel's points - 45 Andreea's points 23 My question is: can you sort the the $result so it will show Code: [Select] Daniel's points - 45 Andreea's points 23 Christina's points - 14 Thank you I have a situation where I need to sort files on a screen based on the date that is embedded into the file name. The problem is that I am sorting based off the file name, and when I get my output the LEGACY file always comes out on top of the sort because it starts with a 0 meanwhile the rest have dates appended the the filename so I don't have a problem sorting those. Please help me if you can. <? //Special case for extra files IF($Recordmyrow['App'] == "(Printable)") { $display = false; $column3 = "Printable VER"; IF($securityvalue == "P") { if(substr($path, 0, 5) == "file:") { $filename = substr($path, 5); if(file_exists($filename)) { $basename = substr($filename, 0, strrpos($filename, '.')); $ext = ".pdf"; $allfiles = glob("{$basename}*.pdf"); rsort($allfiles, SORT_NUMERIC); foreach($allfiles as $file) { if($ext == ".pdf") { $column4 = "<a href=\"pdf.php?src={$file}\">VIEW</a><br />\n"; $codate = substr($file, 70, 8); $co = substr($file, 59, 1); if ($codate != "pdf") { $column3 = "Printable File - ".$codate; } elseif ($codate == "pdf") { $column3 = "Printable File - LEGACY"; } IF($lastGroup != $Recordmyrow['Grouping']) { $lastGroup = $Recordmyrow['Grouping']; ?> <TR> <TD colspan="4" align="center" bgcolor="#418765"><B><FONT color="white"><? echo $Recordmyrow['Grouping'] ?></FONT></B></TD> </TR> <? } ?> <TR> <TD class="display" align="center" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column1 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column2 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column3 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>" align="center"><B><? echo $column4 ?></B></TD> </TR> <? $rowcount++; } } } else { $column4 = "Not Available"; ?> <TR> <TD class="display" align="center" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column1 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column2 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>"><B><? echo $column3 ?></B></TD> <TD class="display" bgcolor="<?=changeColor($rowcount)?>" align="center"><B><? echo $column4 ?></B></TD> </TR> <? $rowcount++; } } } } Hello. I have a multi dimensional array that needs to be sorted. Array top25 [index][product id][number_of_purchases]. The index will contain a unique number for each product id. First product will be 1, second product will be 2, etc. The product id will be a id number like 34324ac9a89. The number of purchases will be how many times the item has been purchased. - End result: I want to print the 25 most purchased products in this format. product id - number of purchases a39acz - 503 a8cz9c - 480 zc8ac - 392 How do I do this? Thanks Happy New Year from a Newbie.
I have data in a MySql database table (let's call it data_table) which is input by users via a form. Their input consists of one or a list of items seperated by spaces. This ends up in one column (let's call it data_col) as in the following example format:
id data_col
1 cat
2 elephant giraffe
3 dog rabbit
4 snake cat bird
5 fish dog
.. etc.
I need help with a PHP script to read only this column into a string variable with which I can then (a) use explode to seperate it into an array containing the seperated words and removing the spaces, if any, at the same time and (b) use array_unique to remove any duplicated words in the array, before finally displaying the array as an A-Z ordered list as below.
bird
cat (duplicate removed)
dog (duplicate removed)
elephant
fish
giraffe
rabbit
snake
Many thanks for any help offered.
First of all I am very new to php. I have coded in asp for years but have made the switch so I kinda know what I want to do but I am unsure on how to achieve it. I have a css menu that I am trying to populate through a database. The parent tabs are in a standard list. The first child submenu is in sepearate unordered list and anymore child tabs are within that list. Like this. Code: [Select] <div id="ddtopmenubar" style="width: 960px"> <ul> <li><a href="?page=">HOME </a></li> <li><a href="?page=About" rel="">ABOUT </a></li> <li><a href="?page=Services" rel="SERVICES">SERVICES </a></li> <li><a href="?page=Governance" rel="GOVERNANCE">GOVERNANCE </a></li> <li><a href="?page=Events">EVENTS </a></li> <li><a href="?page=Contact">CONTACT </a></li> </ul> <ul id="SERVICES" class="ddsubmenustyle"> <li><a href="?page=Health">Health/Dental</a></li> <li><a href="?page=Identification">International Student Identification Card</a></li> <li><a href="?page=Employment">Student Employment</a></li> <li><a href="?page=Saver">Student Saver</a></li> <li><a href="#">Other Services</a> <ul> <li><a href="#">Emergency Student Loan Fund</a></li> <li><a href="#">Legal Services</a></li> <li><a href="#">Parking Permits</a></li> <li><a href="#">Homes4Students</a></li> </ul> </li> </ul> <ul id="GOVERNANCE" class="ddsubmenustyle"> <li><a href="?page=Council">Your Council</a></li> <li><a href="?page=Executive">Executive</a></li> <li><a href="?page=Committees">Committees</a></li> <li><a href="?page=Constitution">Constitution/By-Laws</a></li> <li><a href="?page=Minutes">Minutes</a></li> <li><a href="?page=Elections">Elections</a></li> </ul> </div> I am trying to populate this from a database list the results come ordered like this Code: [Select] 1.0 2.0 3.0 3.1 3.2 3.3 3.4 3.4-1 3.4-2 3.4-3 3.4-4 4.0 4.1 4.2 4.3 4.4 4.5 4.6 5.0 6.0 where the "." show the 1st childs and the "-" show the 2nd child I tried to do this by splting each line and determining if it is a zero or not if it was a zero i put that paticular one into an array to be used in the sub menu however this did not work out. Any ideas? i have an array from a database that looks like this (below) but what i want is for the array to be sorted assedningly by the start position so the team with start position 1 will be 1st and so on...if anyone can help me on what i should do please...thank you Code: [Select] Array ( [1] => Array ( [team_name] => A Team [team_nationality] => British [start_position] => 2 ) [2] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 1 ) [3] => Array ( [team_name] => C Team [team_nationality] => British [start_position] => 4 ) [4] => Array ( [team_name] => B Team [team_nationality] => British [start_position] => 3 ) ) This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=317320.0 I have an array that looks like this... Code: [Select] Array ( [0] => Array ( [0] => 2 [1] => Dylan Cross ) [1] => Array ( [0] => 5 [1] => Bob Smith ) [2] => Array ( [0] => 1 [1] => Sarah Park ) [3] => Array ( [0] => 7 [1] => Jane Knowles ) [4] => Array ( [0] => 6 [1] => Harry Hill ) [5] => Array ( [0] => 3 [1] => Jack Rock ) ) How do I sort this array so it is ordered alphabetically by the names so the result will be... Code: [Select] Array ( [0] => Array ( [0] => 5 [1] => Bob Smith ) [1] => Array ( [0] => 2 [1] => Dylan Cross ) [2] => Array ( [0] => 6 [1] => Harry Hill ) [3] => Array ( [0] => 3 [1] => Jack Rock ) [4] => Array ( [0] => 7 [1] => Jane Knowles ) [5] => Array ( [0] => 1 [1] => Sarah Park ) ) Thanks Hello! I have a 2D array that look as follows: $test["name"][0] = "arnold"; $test["name"][1] = "nate"; $test["name"][2] = "steve"; $test["id"][0] = 2; $test["id"][1] = 0; $test["id"][2] = 1; This means arnold has an ID of 2, nate has an ID of 0 and steve has an ID of 1. Now I want to sort the array based on the ID value, so I want to achieve the following result: $test["name"][0] = "nate"; $test["name"][1] = "steve"; $test["name"][2] = "arnold"; How would I achieve this? Help much appreciated. |