digitalgod Posted July 10, 2006 Share Posted July 10, 2006 hey guys,I'm kind of stumped, I'm trying to display the results of a query in a custom order, here's an example of what I mean.for instance you haveNews 1News 2News 3News 4News 5(those headlines are fetched from the db)and I choose the headlines via checkboxes in this orderNews 4News 2News 3News 5News 1when I submit the form how can I have them display in that order? Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/ Share on other sites More sharing options...
emehrkay Posted July 10, 2006 Share Posted July 10, 2006 you could put the results in an array then parse the array based on the predefined order Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55750 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Please post the source for your form. You should be passing back to the processing script some identifying data so you can get the correct new items..Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55752 Share on other sites More sharing options...
willfitch Posted July 10, 2006 Share Posted July 10, 2006 Assuming the checkboxes are sent as an array, you could use ksort(), sort(), asort() and a variety of other array sorters. Try this URL http://us2.php.net/sort Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55753 Share on other sites More sharing options...
.josh Posted July 10, 2006 Share Posted July 10, 2006 is your order passed as an array from your form? just loop through it and echo the sql results using the form's value as the key.example: if $newslist is an array of your news that you got from the database and $newsorder is your array that holds the order, from your form:[code]foreach($newsworder as $val) { echo $newslist[$val] . "<br>";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55757 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 well here's what my form looks like[code]<td width="151">Select News :</td> <td width="442"><? if ($num_rows < 1) { echo 'There are no new News'; } else { for ($i=0;$i<$num_rows;$i++) { $ID = mysql_result($result,$i,"headline"); $NA= mysql_result($result,$i,"headline"); echo '<input type ="checkbox" name="NA[]" value="'.$ID.'"> '.$ID.''; } }?></td>[/code]and here's how it gets processed[code]$NAList = implode ("','", $_POST['NA']);[/code][quote author=emehrkay link=topic=100095.msg394643#msg394643 date=1152560093]you could put the results in an array then parse the array based on the predefined order[/quote]that's what I'm trying to do, getting the order Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55759 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Unless you have a way for the user to specify the order they want to read the articles, they will be processed in the order they are listed on the form.You need to give the user a method of indicating the order.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55769 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 so I should add a dropdown that displays from 1 to how many news item are being showed so that the user can select what order he wants?and if I do that how do I resort the array or is there a way that they can be placed in the array in the correct order when it gets processed? Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55770 Share on other sites More sharing options...
.josh Posted July 10, 2006 Share Posted July 10, 2006 simply checking the 2nd checkbox 1st, 4th checkbox 2nd, etc.. is not going to submit it in that order, unless you do some javascripting. offhand, all i can think of is to make a series of radio buttons or something. like1 2 3 4 5o o o o o item 1o o o o o item 2o o o o o item 3o o o o o item 4o o o o o item 5 Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55773 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 problem with the radio button is that I don't know in advance how many news articles are in the database, so if there are 20 of them the page won't look very nice if every single headline has 20 radio buttons next to it... Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55777 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 You can use a option list on each headline, put be prepared to state a rule that will be followed if 2 or more headlines have the same read order.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55778 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 for that I can usually javascript so that the form is checked before it gets processed.thanks guys for your help. Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55782 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 And what happens if your users have Javascript turned off?Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55783 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 you got me there :P But it's in an admin panel and only one user will have access to it... Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55811 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 for some reason I keep getting this errorWarning: Invalid argument supplied for foreach()[code]$NAList = implode ("','", $_POST['NA']);$orderList = implode ("','", $_POST['order']);foreach($orderList as $val) {echo $NAList[$val] . "<br>";}[/code]and this is my new form[code]for($i=1;$i<=$num_rows;$i++) {$new_i = $i--;$select_drop .= '<option value="' . $new_i . '">' . $i . '</option>';}<table width ="80%"><? if ($num_rows < 1) { echo 'There are no new News'; } else { for ($i=0;$i<$num_rows;$i++) { $ID = mysql_result($result,$i,"headline"); $NA= mysql_result($result,$i,"headline"); echo '<tr><td>'.$ID.'</td><td width="100%"><div align="left"><input type ="checkbox" name="NA[]" value="'.$ID.'"><select name="order[]">'.$select_drop.'</select></div></td></tr>'; } }?> </table>[/code]is there a reason why I keep getting this error? Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55841 Share on other sites More sharing options...
ShogunWarrior Posted July 10, 2006 Share Posted July 10, 2006 You need [b]explode("','", $_POST['order']);[/b] instead of [b]implode ("','", $_POST['order']);[/b] Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55846 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 that didn't work, I getNotice: Array to string conversion on line 191 which is the explodeand the only thing that gets echoed is a t.... don't know where that's coming from Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55852 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Before you do the implode, check to see if you have anything to test. Checkboxes are only returned to your processing script when they are checked.Put [code]<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>[/code] and see what is being returned to your script.Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55863 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 this is what I get with that line of code[code]Array( [process] => yes [size_limit] => 500 [NA] => Array ( [0] => test [1] => test2 ) [order] => Array ( [0] => 0 [1] => 1 ) [type] => Choose one [send] => Preview)[/code]and I changed my code to this[code]echo '<pre>' . print_r($_POST,true) . '</pre>'; $NAList = implode ("','", $_POST['NA']);$orderList = array();foreach($_POST['order'] as $k => $val) {$orderList[] = $val;}foreach($orderList as $v) {echo $NAList[$v] . "<br>";}[/code]and the echo gives mete Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55868 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 Why are you trying to index through a CSV string? Just use this:[code]<?phpecho '<pre>' . print_r($_POST,true) . '</pre>'; foreach($_POST['order'] as $k => $val) { echo $_POST['NA'][$val] . '<br>';}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55875 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 thanks Ken that works well but the reason why I did like that was so that I can query it after like so$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$NAList')") or die(query_error());how can I query the db with your way? Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55880 Share on other sites More sharing options...
kenrbnsn Posted July 10, 2006 Share Posted July 10, 2006 I don't know if using the IN() in the where clause will deliver the articles in the order you specify.To get the articles in the specified order, you need to loop through the array:[code]<?phpforeach($_POST['order'] as $k => $val) { $q = "select * from " . $prefix . "news where headline = '" . $_POST['NA'][$val] . "'"; $rs = mysql_query($q) or die("Problem getting article, query: $q<br>" . mysql_error());//// display article//}?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55885 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 this is harder than I expected... I don't think I can use it that way because I display the articles in another page with SESSIONS, this is what I did before[code]$NAList = implode ("','", $_POST['NA']);$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$NAList')") or die(query_error());$_SESSION['add_result'] = $result;$tmpl->add_template("newsletter_preview");[/code]and in newsletter_preview I have[code]while ($row=mysql_fetch_array($_SESSION['add_result'])) {// display articles}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55888 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 ok how about adding an extra row in the db for the order but how can I UPDATE the db using your code[code]foreach($_POST['order'] as $k => $val) { echo $_POST['NA'][$val] . '<br>';}[/code]that way I can query the db and just sort it with the order row[code]mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$NAList') ORDER by orderList") or die(query_error());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55895 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 ok tried doing this but no matter what I do the values are always 1 and 0 in that order for $val[code]<?phpforeach($_POST['order'] as $k => $val) {echo $_POST['NA'][$val] . '<br>';echo $val;mysql_query("UPDATE " . $prefix . "news SET orderList='".$val."' WHERE headline='".$_POST['NA'][$val]."'") or die(query_error());}?>[/code]also is there a way to only process the fields that got checked, say I have 20 articles and I only check 5 of them and give those 5 an order, I don't want to process the order of the other fields Quote Link to comment https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/#findComment-55907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.