Hallo ich möchte gern aus einer Datenbank die letzten 5 News Datensätze auf meiner Index.php anzeigen lassen. Wobei die Ausgabe einen Link bekommen soll der anklickbar ist.Ergo, einen Link zur Vollansicht der News.
Wie kann ich das realisieren oder wo kann ich mir das Wissen anlernen. Gibt es einfache Muster Tutorials dafür ? Die man recht simple verstehen kann ?
Da ich noch Anfänger bin und die Fachausdrücke nicht kenne poste ich mal ein Bild damit dann Licht ins Dunkel kommt.
Ich lese den Inhalt der Tabelle News mit dem Script aus
<?php
error_reporting(E_ALL);
include ("checkuser.php");
include ("config/config.php");
require 'libs/Smarty.class.php';
$db_link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
mysql_select_db(MYSQL_DATABASE) OR die(mysql_error());
$tpl = new smarty;
$query = "SELECT
userid,
username,
currenttitel,
lasttitel
FROM
lex_user
WHERE
userid = '".$_SESSION['userid']."';";
$userqry = mysql_query($query) OR die(mysql_error());
$user = array(); // leeres Arrayelement erzeugen
while($row = mysql_fetch_assoc($userqry))
{
$user[] = $row;
}
$query = "SELECT
titel,
sektion,
time_create
FROM
lexikon
ORDER BY
time_create DESC;";
$newsqry = mysql_query($query) OR die(mysql_error());
$news = array(); // leeres Arrayelement erzeugen
while($row = mysql_fetch_assoc($newsqry))
{
$news[] = $row;
}
$tpl = new smarty;
$tpl->assign('user', $user); // In Smarty speichern
$tpl->assign('news', $news); // In Smarty speichern
$tpl->display('index.tpl');
?>
Und lasse das Ergebnis in der Index.tpl so aussehen:
http://www.max-3d.de/hp/images/phpresource/anklick.gif
Ich danke schonmal im vorraus :)
gruenspan
25-05-2006, 13:04
einer Datenbank die letzten 5 News Datensätze auf meiner Index.php anzeigen lassen.
Benutze LIMIT um die Anzahl der ausgegebenen Datensätze auf eine festgelegte Menge zu beschränken.
Wobei die Ausgabe einen Link bekommen soll der anklickbar ist.Ergo, einen Link zur Vollansicht der News.
Wie wäre es denn so oder ähnlich...
echo "<a href='dieSeitemitdenvollständigenNews.php'>".$ausgabe['username']."</a>";
Ich habs nun so gemacht aber er gibt nicht das aus was er sollte.
Index.php
<?php
error_reporting(E_ALL);
include ("checkuser.php");
include ("config/config.php");
require 'libs/Smarty.class.php';
$db_link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
mysql_select_db(MYSQL_DATABASE) OR die(mysql_error());
$tpl = new smarty;
$query = "SELECT
userid,
username,
currenttitel,
lasttitel
FROM
lex_user
WHERE
userid = '".$_SESSION['userid']."';";
$userqry = mysql_query($query) OR die(mysql_error());
$user = array(); // leeres Arrayelement erzeugen
while($row = mysql_fetch_assoc($userqry))
{
$user[] = $row;
}
$query = "SELECT
titel,
sektion,
time_create,
id
FROM
lexikon
ORDER BY
time_create DESC;";
$newsqry = mysql_query($query) OR die(mysql_error());
$news = array(); // leeres Arrayelement erzeugen
while($row = mysql_fetch_assoc($newsqry))
{
$news[] = $row;
}
$tpl = new smarty;
$tpl->assign('user', $user); // In Smarty speichern
$tpl->assign('news', $news); // In Smarty speichern
$tpl->display('index.tpl');
?>
Index TPL
<link href="lexikon.css" rel="stylesheet" type="text/css" />
{include file="header.tpl"}
<table align="center" width="800" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="20" height="20" valign="top" background="./templates/index/main_16.jpg"><img src="./templates/index/main_1.jpg" width="20" height="20"></td>
<td colspan="3" valign="top" background="./templates/index/main_index.jpg"><!--DWLayoutEmptyCell--> </td>
<td width="258" valign="top" background="./templates/index/main_041.jpg"><!--DWLayoutEmptyCell--> </td>
<td width="20" valign="top" background="./templates/index/main_3.jpg"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td rowspan="4" valign="top" background="./templates/index/main_side_left.jpg"><!--DWLayoutEmptyCell--> </td>
<td height="48" colspan="4" valign="middle"><p><span class="textklein1">Hallo{foreach from=$user item=user}</span><span class="textkleins"><strong>{$user.username}</strong></span><span class="textklein1">{/foreach}<strong>,</strong></span></p>
<hr />
<br> </td>
<td rowspan="4" valign="top" background="./templates/index/main_side_right.jpg"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td width="209" height="21" align="center" valign="middle" class="textkleinro">Die letzten Einträge </td>
<td width="20" rowspan="2" valign="top"><!--DWLayoutEmptyCell--> </td>
<td colspan="2" rowspan="2" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td height="84" valign="top" class="info">{foreach from=$news item=news}
ID: {$news.id}<br />
Beitrag: <a href="test.php?titel={$news.titel}">{$news.titel}</a>
<br />
Sektion : {$news.sektion}<br />
Datum : {$news.time_create}<br />
<br />
{/foreach}</td>
</tr>
<tr>
<td height="104" colspan="4" valign="top"><!--DWLayoutEmptyCell--> </td>
</tr>
<tr>
<td rowspan="2" valign="top"><img src="./templates/index/main_4.jpg" width="20" height="20"></td>
<td height="19" colspan="4" valign="top" background="./templates/index/main_042.jpg"><!--DWLayoutEmptyCell--> </td>
<td rowspan="2" valign="top"> <img src="./templates/index/main_6.jpg" width="20" height="20"></td>
</tr>
<tr>
<td height="1"></td>
<td></td>
<td width="273"></td>
<td></td>
</tr>
</table>
{include file="footer.tpl"}
Es wird nun die test.php aufgerufen
<?php
include 'libs/Smarty.class.php';
include 'config/config.php';
$db_link = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS);
mysql_select_db(MYSQL_DATABASE) OR die(mysql_error());
$sql = "
SELECT
titel, sektion, inhalt
FROM
lexikon
WHERE
titel = '".$_GET['titel']."'
LIMIT 1";
$mysql = mysql_query($sql) or die ( mysql_error()); # ohne das kanns ja nicht gehen^^
$n1 = mysql_fetch_assoc($mysql); # auch ohne das, geht es nicht^^
//-------------Index TPL Neu anlegen--------------//
$smarty = new Smarty;
//------------------------------------------------//
$smarty->assign('n1',$n1);
# auch das muss sein!
$smarty->display('test.tpl');
?>
Ausgabe in der test.tpl
{include file="header.tpl"}
<table align="center"width="520" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="520" height="120" align="center" valign="top"><p class="info">
{foreach item=n1 from=$n1}
<br>
{$n1.sektion}<br>
{$n1.id}<br>
{$n1.titel}<br>
{$n1.inhalt}
{/foreach}
</td>
</tr>
</table>
{include file="footer.tpl"}
Er wirft mir nun eine Buchstabenfolge aus mit der ich nichts anfangen kann.
G
G
G
G
p
p
p
p
R
R
R
R
Wo liegt mein Fehler? Bin am verzeifeln und komme langsam durcheinander mit den ganzen abfragen :(
Fehlererkennung:
Die test.tpl muss ohne Foreach geschrieben werden :)
{include file="header.tpl"}
<table align="center"width="520" border="0" cellpadding="0" cellspacing="0">
<!--DWLayoutTable-->
<tr>
<td width="520" height="120" align="center" valign="top"><p class="info"><br>
{$n1.sektion}<br>
{$n1.id}<br>
{$n1.titel}<br>
</td>
</tr>
</table>
{include file="footer.tpl"}