usort issue for news sorting

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
Jay Eff
Forum Newbie
Posts: 20
Joined: Sat Oct 26, 2002 11:35 am

usort issue for news sorting

Post by Jay Eff »

Here is the code I am working with:

Code: Select all

<?//Load News
$query1 = "SELECT * FROM `remnants` WHERE type='update' LIMIT 5";
$query2 = "SELECT * FROM `jayeffcomnews` WHERE type='update' LIMIT 5";
$result = mysql_query($query1);
$rows1  = mysql_fetch_array($result);
$result = mysql_query($query2);
$rows2  = mysql_fetch_array($result);
$rows   = array_merge($rows1,$rows2);
//Sort $rows array
function BypgID($left, $right) {
 return ($right[pgid] - $left[pgid]);
}
usort($rows, "MypgID");
//Print
foreach ($rows as $row) {
 $poster = $row[sub];
 $time   = $row[pgid];
 $time   = gmdate("F d, Y h\:i A",$time-18000);
 $title  = $row[pgtitle];
 $update = $row[page];
 $result2= mysql_query("SELECT * FROM `smilies` WHERE kind = 'user'");
 while($mrow = MYSQL_FETCH_ARRAY($result2)){
  $scode = $mrow["code"];
  $url   = $mrow["url"];
  $update= ereg_replace("$scode", "<img src="http://www.jay-eff.com/$url">", $update);
  $update= eregi_replace(":<", '<', $update);
  $update= eregi_replace(">:", '>', $update);
 }
 $update   = ereg_replace("\n","<br />", $update);
 ?>
 <b><? echo $time ?> - <? echo $title ?> (Poster by <a href="?who-<? echo $poster ?>"><? echo $poster ?></a>)</b><br />
 <? echo $update ?><br /><br />
 <?
}?>
And it is printing 2 entries from the jayeffcomnews table when there is only 1 entry in the table and around 30 in the remnants table. I want to load from jayeffcomnews and remnants and merge all the entries it loads into one array, and then sort that array by 'pgid' (Unix Timestamp), and once it is sorted: print.
Please help me with this without just showing me the manual page for it, I have looked through enough of php.net's manual to realize that I don't understand what they are saying. Just tell me what is wrong with my code.
Thank you in advance.

PS: I am running version 3 of mySQL, so giving me the UNION function doesn't work. :)
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Why dot you get mysql to sort the results in the query and then you can just print them out - should be easier that way.

Code: Select all

SELECT table.*, table2.* FROM table, table2 ORDER BY type
Jay Eff
Forum Newbie
Posts: 20
Joined: Sat Oct 26, 2002 11:35 am

Re: usort issue for news sorting

Post by Jay Eff »

Jay Eff wrote:Here is the code I am working with:

Code: Select all

<?//Load News
$query1 = "SELECT * FROM `remnants` WHERE type='update' LIMIT 5";
$query2 = "SELECT * FROM `jayeffcomnews` WHERE type='update' LIMIT 5";?>
I have to use "WHERE type='update'" and "ORDER BY pgid" and I cannot get it to work when loading two tables in one query.
Jay Eff
Forum Newbie
Posts: 20
Joined: Sat Oct 26, 2002 11:35 am

Post by Jay Eff »

Does anyone know how I can do this?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

other than the name of the function

Code: Select all

function BypgID($left, $right) {
return ($right[pgid] - $left[pgid]);
}
usort($rows, "MypgID");
being off and without running the code, it looks like it'd work.

quote your named array indices.
Jay Eff
Forum Newbie
Posts: 20
Joined: Sat Oct 26, 2002 11:35 am

Post by Jay Eff »

Okay, I fixed the function name error, and put quotes around the named array indices. Now here is my code:

Code: Select all

<?
//News module
//purpose: load information and print information out
//Connect to database
$dbh        = mysql_connect ("*******", "********", "********") or die ('I cannot connect to the database.');
$result     = mysql_select_db("*********");
//Load News
$query1 = "SELECT * FROM `remnants` WHERE type='update' LIMIT 5";
$query2 = "SELECT * FROM `jayeffcomnews` WHERE type='update' LIMIT 5";
$result = mysql_query($query1) or die ("remnants error load");
$rows1  = mysql_fetch_array($result) or die ("remnants error array");

$result = mysql_query($query2) or die ("jayeffcomnews error load");
$rows2  = mysql_fetch_array($result) or die ("jayeffcomnews error array");

$rows   = array_merge($rows1,$rows2) or die ("merge error");
//Sort $rows array
function BypgID($left, $right) {
 return ($right["pgid"] - $left["pgid"]);
}
usort($rows, "BypgID") or die ("usort error");
//Print
$n      = 0;
while ($row = $rows[$n] AND $n != 5) {
  $poster = $row["sub"];
  $time   = $row["pgid"];
  $time   = gmdate("F d, Y h\:i A",$time-18000);
  $title  = $row["pgtitle"];
  $update = $row["page"];
  $result2= mysql_query("SELECT * FROM `smilies` WHERE kind = 'user'");
  while($mrow = MYSQL_FETCH_ARRAY($result2)){
   $scode = $mrow["code"];
   $url   = $mrow["url"];
   $update= ereg_replace("$scode", "<img src="http://www.jay-eff.com/$url">", $update);
   $update= eregi_replace(":<", '<', $update);
   $update= eregi_replace(">:", '>', $update);
  }
  $update   = ereg_replace("\n","<br />", $update);
  ?>
  <b><? echo $time ?> - <? echo $title ?> (Poster by <a href="?who-<? echo $poster ?>"><? echo $poster ?></a>)</b><br />
  <? echo $update ?><br /><br />
  <?
  $n++;
}

?>
And what is printed:

Code: Select all

December 31, 1969 07:00 PM - 1 (Poster by 1)
1

December 31, 1969 07:00 PM - 1 (Poster by 1)
1

December 31, 1969 07:00 PM - 1 (Poster by 1)
1

December 31, 1969 07:00 PM - 1 (Poster by 1)
1

December 31, 1969 07:00 PM - 1 (Poster by 1)
1
This told me that the mySQL arrays returned have all values as 1. I checked into the mysql_fetch_arrays, and found that the error was there, but I have no clue what the error is and cannot find it in the manual. If I run a single mysql_fetch_array through a while statement as

Code: Select all

while ($row = mysql_fetch_array($rows1) {
//commands
}
it prints everything good. But now if I load the $rows1=mysql_fetch_array($result), I get all 1s as the values. I guess this is the issue, as everything else now works. Help please?


feyd | removed host/user/password
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try this:

Code: Select all

<?
//News module
//purpose: load information and print information out
//Connect to database
$dbh        = mysql_connect ("***********", "*********", "**********") or die ('I cannot connect to the database.');
$result     = mysql_select_db("***********");
//Load News
$query1 = "SELECT * FROM `remnants` WHERE type='update' LIMIT 5";
$query2 = "SELECT * FROM `jayeffcomnews` WHERE type='update' LIMIT 5";

$rows = array();

$result = mysql_query($query1) or die ("remnants error load");
while($rows[]  = mysql_fetch_assoc($result));

$result = mysql_query($query2) or die ("jayeffcomnews error load");
while($rows[]  = mysql_fetch_assoc($result));

//Sort $rows array
function BypgID($left, $right) {
return ($right["pgid"] - $left["pgid"]);
}

usort($rows, "BypgID") or die ("usort error");
//Print

$result = mysql_query("SELECT * FROM `smilies` WHERE kind = 'user'");
while($row = mysql_fetch_assoc($result))
{
  $smilies[] = $row['code'];
  $smiley_url[] = $row['url'];
}

function preSuffix(&$elem, $key, $bits)
{
  $elem = $bits[0] . $elem . $bits[1];
}

array_walk($smiley_url, 'preSuffix',array('<img src="http://www.jay-eff.com/','">'));

for ($n = 0, $limit = min(5,sizeof($rows)); $n < $limit; $n++)
{
  $row =& $rows[$n];
  $poster =& $row['sub'];
  $time   =& $row['pgid'];
  $time   = gmdate("F d, Y h\:i A",$time - 18000);
  $title  =& $row['pgtitle'];
  $update =& $row['page'];
  $update = ereg_replace($smilies, $smiley_url, $update);
  $update = eregi_replace(":<", '<', $update);
  $update = eregi_replace(">:", '>', $update);
  $update = ereg_replace("\n","<br />", $update);
  ?>
  <b><? echo $time ?>- <? echo $title ?>(Poster by <a href="?who-<? echo $poster ?>"><? echo $poster ?></a>)</b><br />
  <? echo $update ?><br /><br />
  <?
  $n++;
} 

?>
Last edited by feyd on Thu Jul 29, 2004 9:18 pm, edited 2 times in total.
Jay Eff
Forum Newbie
Posts: 20
Joined: Sat Oct 26, 2002 11:35 am

Post by Jay Eff »

Code: Select all

Fatal error: Cannot use &#1111;] for reading in /home/jay-effc/public_html/new/modules/newsfeeds.php on line 14
has been returned. I think it's closer to working.
Post Reply