Help with sorting something

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Help with sorting something

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post by cheerio »

:'( i'm dont follow that too well..i don't know what to do next now :'(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do you not understand the code, or the usort() part?
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post by cheerio »

the code. I got confused at like the $rows[$row] line
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

$rows[$row] would be the KEY $row (which references the VALUE) of ARRAY $rows
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if using a foreach, you can use list() with the value variable.
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
cheerio
Forum Newbie
Posts: 18
Joined: Sat Aug 13, 2005 4:52 pm

Post by cheerio »

oh 8O
Post Reply