Jump to content


Photo

displaying results in a specific order


  • Please log in to reply
26 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 07:32 PM

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 have

News 1
News 2
News 3
News 4
News 5

(those headlines are fetched from the db)

and I choose the headlines via checkboxes in this order

News 4
News 2
News 3
News 5
News 1

when I submit the form how can I have them display in that order?

#2 emehrkay

emehrkay
  • Staff Alumni
  • Advanced Member
  • 1,214 posts

Posted 10 July 2006 - 07:34 PM

you could put the results in an array then parse the array based on the predefined order

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 07:35 PM

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

#4 willfitch

willfitch
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts
  • LocationNew Hope, PA

Posted 10 July 2006 - 07:36 PM

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
Zend Certified Engineer (ZEND001989)
www.willfitch.com

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 July 2006 - 07:40 PM

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:

foreach($newsworder as $val) {
   echo $newslist[$val] . "<br>";
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 07:43 PM

well here's what my form looks like

<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>

and here's how it gets processed

$NAList = implode ("','", $_POST['NA']);

you could put the results in an array then parse the array based on the predefined order


that's what I'm trying to do, getting the order

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 07:50 PM

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

#8 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 07:53 PM

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?

#9 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 July 2006 - 07:54 PM

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.  like

1 2 3 4 5
o o o o o  item 1
o o o o o  item 2
o o o o o  item 3
o o o o o  item 4
o o o o o  item 5

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#10 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 07:58 PM

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...

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 08:01 PM

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

#12 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 08:03 PM

for that I can usually javascript so that the form is checked before it gets processed.

thanks guys for your help.

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 08:04 PM

And what happens if your users have Javascript turned off?

Ken

#14 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 08:36 PM

you got me there :P But it's in an admin panel and only one user will have access to it...

#15 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 09:10 PM

for some reason I keep getting this error

Warning: Invalid argument supplied for foreach()
$NAList = implode ("','", $_POST['NA']);
$orderList = implode ("','", $_POST['order']);
foreach($orderList as $val) {
echo $NAList[$val] . "<br>";
}

and this is my new form

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>

is there a reason why I keep getting this error?

#16 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 10 July 2006 - 09:14 PM

You need explode("','", $_POST['order']); instead of implode ("','", $_POST['order']);
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#17 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 09:21 PM

that didn't work, I get

Notice: Array to string conversion on line 191    which is the explode

and the only thing that gets echoed is a t.... don't know where that's coming from

#18 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 09:34 PM

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
<?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?>
and see what is being returned to your script.

Ken

#19 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 09:39 PM

this is what I get with that line of code

Array
(
    [process] => yes
    [size_limit] => 500
    [NA] => Array
        (
            [0] => test
            [1] => test2
        )

    [order] => Array
        (
            [0] => 0
            [1] => 1
        )

    [type] => Choose one
    [send] => Preview
)

and I changed my code to this
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>";
}

and the echo gives me
t
e


#20 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 July 2006 - 09:47 PM

Why are you trying to index through a CSV string? Just use this:
<?php
echo '<pre>' . print_r($_POST,true) . '</pre>'; 
foreach($_POST['order'] as $k => $val) {
    echo $_POST['NA'][$val] . '<br>';
}?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users