Jump to content


Photo

displaying results in a specific order


  • Please log in to reply
26 replies to this topic

#21 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 09:56 PM

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?

#22 kenrbnsn

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

Posted 10 July 2006 - 10:03 PM

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:
<?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
//
}
?>

Ken

#23 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 10:10 PM

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

$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");

and in newsletter_preview I have
while ($row=mysql_fetch_array($_SESSION['add_result'])) {
// display articles
}



#24 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 10:21 PM

ok how about adding an extra row in the db for the order but how can I UPDATE the db using your code

foreach($_POST['order'] as $k => $val) {
    echo $_POST['NA'][$val] . '<br>';
}

that way I can query the db and just sort it with the order row

mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$NAList') ORDER by orderList") or die(query_error());


#25 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 11:01 PM

ok tried doing this but no matter what I do the values are always 1 and 0 in that order for $val

<?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());
}
?>

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

#26 kenrbnsn

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

Posted 11 July 2006 - 04:00 AM

Can you post the code you're currently using? I think you're trying too hard and you're getting lost in the forest...

Ken

#27 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 11 July 2006 - 06:56 PM

hey ken,

here's the code I'm currently using, it kind of works when you first decide on the order but if you go back and change the order it won't work anymore.

Here's what I got
<?php
//echo '<pre>' . print_r($_POST,true) . '</pre>'; 
$NAList = implode ("','", $_POST['NA']);
$DAList = implode ("','", $_POST['DA']);
$type = $_POST['type'];
$dj = $_POST['dj'];
foreach($_POST['order'] as $k => $val) {
echo $_POST['NA'][$val] . '<br>';
echo $k;
mysql_query("UPDATE " . $prefix . "news SET orderList='".$k."' WHERE headline='".$_POST['NA'][$val]."'") or die(query_error());		
}
$_SESSION['add_result']=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$NAList') ORDER by orderList") or die(query_error());
?>

and in the newsletter_preview.php I got

<?php
$result = $_SESSION['add_result'];
 while ($row=mysql_fetch_array($result)) {
//display the articles in html
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users