Seite 1 von 1

Recent Posts

Verfasst: 02.10.2005 22:47
von roadrunnr80
Hallo.

Ich verwendete bisher einen Code um mir die letzten Topics in denen etwas gepostet wurde auf einer externen Seite (außerhalb von phpbb) anzuzeigen.
Das wollte nun erweitern, indem ich mir den Poster anzeigen lassen will, und später noch die ersten 30 Zeichen des entsprechenden Beitrages.

Daher erweiterte ich den Code, aber es funktioniert nicht recht. Mir wird zwar keine Fehlermeldung ausgespuckt, aber er liefert mir auch nicht den Usernamen des Posters.
Daher die Frage: was ist falsch an der SQL-Syntax? Oder ist im PHP Code was falsch?

Der Code:

Code: Alles auswählen

<?

error_reporting(E_ALL);
require_once("class2.php");

echo"<a href=\"./Forum/index.php\">Neues aus dem Forum...</a>";
echo" <table width=\"100%\">";
echo"   <tr>";
echo"     <th align=\"left\">Thema</th><th align='left'>Post von</th><th align='left'>Replies</th><th align='left'>Aufrufe</th><th align=\"left\">Forum</th>\n"; //<th align=\"left\">Datum</th>
echo"   </tr>";


define('ENDLINE', "\n");

include ('./Forum/config.php');
include ('./Forum/include/constants.php');

$queryString = 'SELECT topic_views, topic_replies, topic_id, topic_title, topic_last_post_id, forum_name, dsoneforum_forums.forum_id, post_id
FROM dsoneforum_topics, dsoneforum_forums, dsoneforum_posts_text
WHERE dsoneforum_topics.forum_id = dsoneforum_forums.forum_id AND dsoneforum_topics.forum_id !=4
GROUP BY topic_id
ORDER BY topic_last_post_id DESC
LIMIT 3 ';

$result = mysql_query($queryString);
$today = time();

while($resultArray = mysql_fetch_array($result))
{
if(strlen($resultArray[topic_title]) > 25) 
{
    $resultArray[topic_title] = substr($resultArray[topic_title],0,22)." ..";

}
echo'   <tr> ' . ENDLINE;
echo'     <td><a href="/Forum/viewtopic.php?t=' . $resultArray[topic_id] . '#' . $resultArray[topic_last_post_id] . '">' . $resultArray[topic_title]. '</a>&nbsp;&nbsp;</td>' . ENDLINE;;
$sql = "SELECT t.*, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time 
       FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2
       WHERE t.forum_id != 4
           AND t.topic_poster = u.user_id
           AND p.post_id = t.topic_first_post_id
           AND p2.post_id = t.topic_last_post_id
           AND u2.user_id = p2.poster_id 
       ORDER BY t.topic_type DESC, t.topic_last_post_id DESC 
       LIMIT 0,3";

$res = mysql_query($sql);
$resarray = mysql_fetch_array($res);
echo"     <td>" . $resarray[username] . ENDLINE;;
echo'     <td>' . $resultArray[topic_replies] . ENDLINE;;
echo'     <td>' . $resultArray[topic_views] . ENDLINE;;
echo'     <td><a href="/Forum/viewforum.php?f=' . $resultArray[forum_id] . '">' . $resultArray[forum_name] . '</a>&nbsp;&nbsp;</td>' . ENDLINE;;
echo'   </tr>' . ENDLINE;
}
echo "</table>";
echo "<br>";
echo "<br>";

?>

Verfasst: 02.10.2005 23:12
von fanrpg
btw mach mal nach mysql_query nen die(mysql_error()); also

Code: Alles auswählen

$res = mysql_query($var) or die(mysql_error());
und hier ist ein fehler diese:

Code: Alles auswählen

$resultArray[topic_views]
müssen so geschrieben werden:

Code: Alles auswählen

$resultArray['topic_views']

Verfasst: 03.10.2005 07:55
von roadrunnr80
fanrpg hat geschrieben:btw mach mal nach mysql_query nen die(mysql_error()); also

Code: Alles auswählen

$res = mysql_query($var) or die(mysql_error());
Danke :)
Der Fehler lag darin, das die Variablen TOPICS_TABLE etc. nicht erkannt wurden, obwohl die Constants.php eingebunden ist.
Nach ersetzen der Variablen mit den entsprechenden Tabellennamen funktioniert die ganze Sache jedenfalls soweit, das er mir einen Usernamen anzeigt. Allerdings zeigt er mir immer denselben Usernamen an... :-?

Verfasst: 03.10.2005 10:54
von fanrpg
hier musst du noch eine andere WHERE Bedingung einbauen, sonst ist klar das der immer den selben Usernamen anzeigt:

Code: Alles auswählen

$sql = "SELECT t.*, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time 
       FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u, " . POSTS_TABLE . " p, " . POSTS_TABLE . " p2, " . USERS_TABLE . " u2 
       WHERE t.forum_id != 4 
           AND t.topic_poster = u.user_id 
           AND p.post_id = t.topic_first_post_id 
           AND p2.post_id = t.topic_last_post_id 
           AND u2.user_id = p2.poster_id 
       ORDER BY t.topic_type DESC, t.topic_last_post_id DESC 
       LIMIT 0,3";

Verfasst: 03.10.2005 11:30
von roadrunnr80
Hm, führe ich den SQL-String im PHPMyAdmin aus, liefert er mir die richtigen Usernamen.

Verfasst: 03.10.2005 13:29
von roadrunnr80
Sorry für den Doppelpost, aber ich hab den Fehler nun selbst gefunden.
Die Ursache war das "$resarray = mysql_fetch_array($res);" nicht in der Schleife stand und somit auch nicht hochgezählt wurde. :roll:

Warum allerdings $table_prefix bei der ersten Abfrage funktioniert und bei der zweiten nicht (wird mir nichts zurückgeliefert...) ist mir nach wie vor ein Rätsel. :-?
Fall jemand auch sowas braucht hier der komplette Code:

Code: Alles auswählen

<?

error_reporting(E_ALL);
require_once("./class2.php");

echo"<div class='fcaption'><a href=\"./Forum/index.php\">Neues aus dem Forum...</a>";
echo" <table>";
echo"   <tr>";
echo"     	<th align='left' class='forumheader3'>Thema</th>
		<th align='left' class='forumheader3'>Lastpost</th>
		<th align='left' class='forumheader3'>Replies</th>
		<th align='left' class='forumheader3'>Aufrufe</th>
		<th align='left' class='forumheader3'>Zeit</th>
		<th align='left' class='forumheader3'>Forum</th>	\n";
echo"   </tr>";

define('ENDLINE', "\n");

include ('./Forum/config.php');
include ('./Forum/includes/constants.php');

$queryString = 'SELECT topic_views, topic_replies, topic_id, topic_title, topic_last_post_id, forum_name, dsoneforum_forums.forum_id, post_id
FROM '.$table_prefix.'topics, '.$table_prefix.'forums, '.$table_prefix.'posts_text
WHERE '.$table_prefix.'topics.forum_id = '.$table_prefix.'forums.forum_id AND '.$table_prefix.'topics.forum_id !=4
GROUP BY topic_id
ORDER BY topic_last_post_id DESC
LIMIT 3 ';

$result = mysql_query($queryString);
$today = time();

$sqlst = "SELECT t.*, u.username, u.user_id, u2.username as user2, u2.user_id as id2, p.post_username, p2.post_username AS post_username2, p2.post_time
       FROM dsoneforum_topics t, dsoneforum_users u, dsoneforum_posts p, dsoneforum_posts p2, dsoneforum_users u2
       WHERE t.forum_id != 4
           AND t.topic_poster = u.user_id
           AND p.post_id = t.topic_first_post_id
           AND p2.post_id = t.topic_last_post_id
           AND u2.user_id = p2.poster_id 
       ORDER BY t.topic_type DESC, t.topic_last_post_id DESC 
       LIMIT 0,3";

$res = mysql_query($sqlst) or die(mysql_error());;

while($resultArray = mysql_fetch_array($result))
{
if(strlen($resultArray[topic_title]) > 40) 
{
    $resultArray[topic_title] = substr($resultArray[topic_title],0,38)." ..";

}
$resarray = mysql_fetch_array($res);
$postdate = getdate($resarray[post_time]);
echo'   <tr> ' . ENDLINE;
echo'     <td style="background-color: #1c5fa8;"><a href="/Forum/viewtopic.php?t=' . $resultArray[topic_id] . '#' . $resultArray[topic_last_post_id] . '">' . $resultArray[topic_title]. '</a>&nbsp;&nbsp;</td>' . ENDLINE;;
echo'     <td style="background-color: #1c5fa8;"><center><a href="/Forum/profile.php?mode=viewprofile&u=' . $resarray[username] . '">' . $resarray[username] . '</a></center>' . ENDLINE;;
echo'     <td style="background-color: #1c5fa8;"><center>' . $resultArray[topic_replies] . '</center>' . ENDLINE;;
echo'     <td style="background-color: #1c5fa8;"><center>' . $resultArray['topic_views'] . '</center>' . ENDLINE;;
echo'     <td width="12%" style="background-color: #1c5fa8;"><center>' . $postdate[mday] .'.' . $postdate[mon] .'.' . $postdate[year] . ' ' . $postdate[hours] . ':' . $postdate[minutes] . '</center>' . ENDLINE;;
echo'     <td style="background-color: #1c5fa8;"><a href="/Forum/viewforum.php?f=' . $resultArray[forum_id] . '">' . $resultArray[forum_name] . '</a>&nbsp;&nbsp;</td>' . ENDLINE;;
echo'   </tr>' . ENDLINE;
}
echo "</table></div>";
echo "<br>";
?>
Muss natürlich an eure jeweiligen Tabellennamen angepasst werden.
Die Farben etc. dabei nicht vergessen.
Ein Anzeigebeispiel