Jump to content

Archived

This topic is now archived and is closed to further replies.

digitalgod

displaying results in a specific order

Recommended Posts

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?

Share this post


Link to post
Share on other sites
you could put the results in an array then parse the array based on the predefined order

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
for that I can usually javascript so that the form is checked before it gets processed.

thanks guys for your help.

Share this post


Link to post
Share on other sites
And what happens if your users have Javascript turned off?

Ken

Share this post


Link to post
Share on other sites
you got me there :P But it's in an admin panel and only one user will have access to it...

Share this post


Link to post
Share on other sites
for some reason I keep getting this error

Warning: 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?

Share this post


Link to post
Share on other sites
You need [b]explode("','", $_POST['order']);[/b] instead of [b]implode ("','", $_POST['order']);[/b]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 me
t
e

Share this post


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

Ken

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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]<?php
foreach($_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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
ok tried doing this but no matter what I do the values are always 1 and 0 in that order for $val

[code]
<?php
foreach($_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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.