Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/14213-displaying-results-in-a-specific-order/
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]
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
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?
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
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?
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
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
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
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]

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]
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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.