Page 1 of 1

Help with sorting something

Posted: Sat Sep 03, 2005 3:19 pm
by cheerio
hey i'm just making a comment board heres the function that displays the old entries:

Code: Select all

function showold($file,$badwordsarray,$cleanword) {
switch($_GET[badlang]) {
	case off:
		$filter = 0;
	break;
	default:
		$filter = 1;
	break;
}

$contents_old = file_get_contents($file);
if($filter) {
echo "<font class='valid'>bad words filter is ON. click <a href='?badlang=off'>here</a> to turn it off</font>";
$contents_old = str_ireplace($badwordsarray,$cleanword,$contents_old);
} else {
echo "<font class='invalid'>bad words filter is OFF! click <a href='index.php'>here</a> to turn it on</font>";
}
$row = explode("|line|",$contents_old);

?>
<table class="old">
<?php
for($i = 0;$i < count($row);$i++)
{
list($name,$contact,$subject,$date,$comment) = explode("|#|",$row[$i]);
$color1 = "#DDDDDD";
$color2 = "#EEEEEE";
$style = ($i % 2==1) ? "$color1" : "$color2";

	echo '
			<tr>
				<td rowspan="2" class="leftold" title="Post #'.$i.'">'.$i.'</td>
				<td rowspan="2" class="leftold" ><font title="Poster\'s Name">'.$name.'</font><br>[ <font class="oldleft" title="Email/Sn of '.$name.'">'.$contact.'</font> ]</td>
				<td class="rightold" title="Subject of Post"><b>Subject:</b><i>'.$subject.'</i></td>
				<td class="rightold" title="Date/Time Posted"><b>Date/Time:</b><i>'.$date.'</i></td>
			</tr>
			<tr>
				<td class="rightold" style="background-color:#EEEEEE" colspan=2 title="'.$name.'\'s Comment">'.$comment.'</td>
			</tr>


				';

}
echo '</table>';
}
what i want it to do is sort by variable, $date but can't figure out how
thanks in advanced

Posted: Sat Sep 03, 2005 3:25 pm
by feyd
first thing, you'll have to store up the broken apart bits like so:

Code: Select all

$rows = explode("|line|",$contents_old);
foreach($rows as $row => $data)
{
  $rows[$row] = explode("|#|",$row[$i]);
}
then I'd suggest using usort() with your own function that will sort based on the date.

Posted: Sat Sep 03, 2005 3:39 pm
by cheerio
:'( i'm dont follow that too well..i don't know what to do next now :'(

Posted: Sat Sep 03, 2005 3:42 pm
by feyd
do you not understand the code, or the usort() part?

Posted: Sat Sep 03, 2005 3:43 pm
by cheerio
the code. I got confused at like the $rows[$row] line

Posted: Sat Sep 03, 2005 3:52 pm
by s.dot
$rows[$row] would be the KEY $row (which references the VALUE) of ARRAY $rows

Posted: Sat Sep 03, 2005 8:34 pm
by cheerio
so now how would i be able to echo like name, contact, comment, etc???? is it still possible for me to use the list command?

Posted: Sat Sep 03, 2005 8:42 pm
by feyd
if using a foreach, you can use list() with the value variable.

Posted: Sat Sep 03, 2005 8:57 pm
by cheerio

Code: Select all

$rows = explode("|line|",$contents_old);
?>
<table class="old">
<?php
$i = 0;
while($i < count($rows)) {
foreach($rows as $row => $data) {

$rows[$row] = explode("|#|",$row[$i]);
list($name,$contact,$subject,$date,$comment,$ip) = $data;

	echo '
				<tr>
					<td rowspan="2" class="leftold" title="Post #'.$i.'">'.$i.'</td>
					<td rowspan="2" class="leftold" ><font title="Poster\'s Name">'.$name.'</font><br>[ <font class="oldleft" title="Email/Sn of '.$name.'">'.$contact.'</font> ]</td>
					<td class="rightold" title="Subject of Post"><b>Subject:</b><i>'.$subject.'</i></td>
					<td class="rightold" title="Date/Time Posted"><b>Date/Time:</b><i>'.$date.'</i></td>
					<td class="rightold" title="IP of '.$name.'"><b>IP:</b><i>'.$ip.'</i></td>
				</tr>
				<tr>
					<td class="rightold" style="background-color:#EEEEEE" colspan=3 title="'.$name.'\'s Comment">'.$comment.'</td>
				</tr>


					';

$i++;
}
echo '</table>';
}
}


?>
i did something wrong in that script...it onlu echos the first letter of for any of the variable like name contact, comments, etc
btw thanks for alot of replies sorry i keep asking for more i just can't get it to work :'(
im kind of new to PHP so...

Posted: Sat Sep 03, 2005 9:34 pm
by feyd
the list and echo does not go inside that loop. That loop is for setting up the data so you can sort it. You need an additional loop that runs over $rows after being sorted.

Posted: Sun Sep 04, 2005 10:39 am
by cheerio
oh 8O