22. april 2008 - 21:02Der er
28 kommentarer og 1 løsning
Gå til næste side
Ja så er jeg her lige igen :)
Har en kode på min side hvor der vises nogle nyheder, men jeg har nogle problemer med at den viser at der er f.eks. 4 sider med nyheder, selvom der kun er skrevet en enkelt nyhed i databasen.
// below is the code needed to show page numbers
// count how many rows we have in database $query = "SELECT COUNT(id) AS numrows FROM guestbook"; $result = mysql_query($query) or die('Error, query failed. ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows'];
// how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); $nextLink = '';
// show the link to more pages ONLY IF there are // more than one page if($maxPage > 1) { // this page's path $self = $_SERVER['PHP_SELF'];
// we save each link in this array $nextLink = array();
// create the link to browse from page 1 to page $maxPage for($page = 1; $page <= $maxPage; $page++) { $nextLink[] = "<a href=\"$self?side=news&page=$page\">$page</a>"; }
// join all the link using implode() $nextLink = "Go to page : " . implode(' » ', $nextLink); }
// close the database connection since // we no longer need it include 'library/closedb.php';
Kan nogen evt. se hvad der gør at jeg får denne fejl:
dennis Nielsen 22.04.2008 sdfsdfg sg s df gdf gdfg
Dennis Nielsen 09.04.2008 Fatal error: Cannot redeclare vislidt() (previously declared in /hsphere/local/home/briston/blog.danziger.dk/index.php:230) in /hsphere/local/home/briston/blog.danziger.dk/index.php on line 230
Som i kan se så viser den fint den allernyeste post
<?php // include the database configuration and // open connection to database include 'library/config.php'; include 'library/opendb.php';
?>
<?php
// ======================= // Show guestbook entries // =======================
// how many guestbook entries to show per page $rowsPerPage = 5;
// by default we show first page $pageNum = 1;
// if $_GET['page'] defined, use the value as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
// counting the offset ( where to start fetching the entries ) $offset = ($pageNum - 1) * $rowsPerPage;
// prepare the query string $query = "SELECT id, name, email, url, message, DATE_FORMAT(entry_date, '%d.%m.%Y') ". "FROM news ". "ORDER BY id DESC ". // using ORDER BY to show the most current entry first "LIMIT $offset, $rowsPerPage"; // LIMIT is the core of paging
// execute the query $result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
// if the guestbook is empty show a message if(mysql_num_rows($result) == 0) { ?> <p> Guestbook is empty </p> <?php } else { // get all guestbook entries while($row = mysql_fetch_array($result)) { // list() is a convenient way of assign a list of variables // from an array values list($id, $name, $email, $url, $message, $date) = $row;
// change all HTML special characters, // to prevent some nasty code injection $name = htmlspecialchars($name); $message = htmlspecialchars($message);
// convert newline characters ( \n OR \r OR both ) to HTML break tag ( <br> ) $message = nl2br($message); ?> <table width="360" border="0" cellpadding="0" cellspacing="0" class="Main_Text"> <tr> <td width="211" align="left"> <a href="mailto:<?=$email;?>" class="email"> <?=$name;?> </a> </td> <td width="149" align="right"><small> <?=$date;?> </small></td> </tr> <tr> <td colspan="2">
<?php function VisLidt($sText, $iCount){ #Fjerner kommaer da det ikke ser så smart ud med et komma til sidst i en sætning
<?php // if the visitor input her homepage url show it if($url != '') { // make the url clickable by formatting it as HTML link $url = "<a href='$url' target='_blank'>$url</a>"; ?> <br> <small>Homepage : <?=$url;?></small>
<?php } ?> <br /> <img src="dots.gif" /> </td> </tr> </table> <br> <?php } // end while
// close the database connection since // we no longer need it include 'library/closedb.php';
Du kan IKKE oprette en funktion inde i en while-løkke. Vend dig til at lave funktionsoprettelser i starten af din side, eller i en anden fil. I dit tilfælde så opretter du funktionen VisLidt hver gang while-løkken kører, og det kan man ikke, da en funktion kun kan oprettes en gang.
Men hvad skal jeg så skrive istedet for $message? får skriver jeg det et andet sted på siden, aner siden jo ik hvor den skal finde de oplysninger $message skulle indeholde...
<?php function VisLidt($sText, $iCount){ #Fjerner kommaer da det ikke ser så smart ud med et komma til sidst i en sætning
Men kan bare ik forstår hvordan den kode så skal se ud :/ her er lige hvad der er indtil videre
<?php // include the database configuration and // open connection to database include 'library/config.php'; include 'library/opendb.php';
?>
<?php
// ======================= // Show guestbook entries // =======================
// how many guestbook entries to show per page $rowsPerPage = 5;
// by default we show first page $pageNum = 1;
// if $_GET['page'] defined, use the value as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
// counting the offset ( where to start fetching the entries ) $offset = ($pageNum - 1) * $rowsPerPage;
// prepare the query string $query = "SELECT id, name, email, url, message, DATE_FORMAT(entry_date, '%d.%m.%Y') ". "FROM news ". "ORDER BY id DESC ". // using ORDER BY to show the most current entry first "LIMIT $offset, $rowsPerPage"; // LIMIT is the core of paging
// execute the query $result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
// if the guestbook is empty show a message if(mysql_num_rows($result) == 0) { ?> <p> News table is empty </p> <?php } else {
// get all guestbook entries while($row = mysql_fetch_array($result)) { // list() is a convenient way of assign a list of variables // from an array values list($id, $name, $email, $url, $message, $date) = $row;
// change all HTML special characters, // to prevent some nasty code injection $name = htmlspecialchars($name); $message = htmlspecialchars($message);
// convert newline characters ( \n OR \r OR both ) to HTML break tag ( <br> ) $message = nl2br($message); ?> <table width="360" border="0" cellpadding="0" cellspacing="0" class="Main_Text"> <tr> <td width="211" align="left"> <a href="mailto:<?=$email;?>" class="email"> <?=$name;?> </a> </td> <td width="149" align="right"><small> <?=$date;?> </small></td> </tr> <tr> <td colspan="2">
<?php function VisLidt($sText, $iCount){ #Fjerner kommaer da det ikke ser så smart ud med et komma til sidst i en sætning
<?php // if the visitor input her homepage url show it if($url != '') { // make the url clickable by formatting it as HTML link $url = "<a href='$url' target='_blank'>$url</a>"; ?> <br> <small>Homepage : <?=$url;?></small>
<?php } ?> <br /> <img src="dots.gif" /> </td> </tr> </table> <br> <?php } // end while ?> <?php // close the database connection since // we no longer need it include 'library/closedb.php';
?> <?php } ?> </td> </tr>
</table> </td> </tr> </table>
<span class="style3">
<? } ?>
For jeg kan da ik bare hive
<?php // include the database configuration and // open connection to database include 'library/config.php'; include 'library/opendb.php';
?>
<?php
// ======================= // Show guestbook entries // =======================
// how many guestbook entries to show per page $rowsPerPage = 5;
// by default we show first page $pageNum = 1;
// if $_GET['page'] defined, use the value as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
// counting the offset ( where to start fetching the entries ) $offset = ($pageNum - 1) * $rowsPerPage;
// prepare the query string $query = "SELECT id, name, email, url, message, DATE_FORMAT(entry_date, '%d.%m.%Y') ". "FROM news ". "ORDER BY id DESC ". // using ORDER BY to show the most current entry first "LIMIT $offset, $rowsPerPage"; // LIMIT is the core of paging
// execute the query $result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
// if the guestbook is empty show a message if(mysql_num_rows($result) == 0) { ?> <p> News table is empty </p> <?php } else {
// get all guestbook entries while($row = mysql_fetch_array($result)) { // list() is a convenient way of assign a list of variables // from an array values list($id, $name, $email, $url, $message, $date) = $row;
// change all HTML special characters, // to prevent some nasty code injection $name = htmlspecialchars($name); $message = htmlspecialchars($message);
// convert newline characters ( \n OR \r OR both ) to HTML break tag ( <br> ) $message = nl2br($message); ?> <table width="360" border="0" cellpadding="0" cellspacing="0" class="Main_Text"> <tr> <td width="211" align="left"> <a href="mailto:<?=$email;?>" class="email"> <?=$name;?> </a> </td> <td width="149" align="right"><small> <?=$date;?> </small></td> </tr> <tr> <td colspan="2">
<?php function VisLidt($sText, $iCount){ #Fjerner kommaer da det ikke ser så smart ud med et komma til sidst i en sætning
<?php // if the visitor input her homepage url show it if($url != '') { // make the url clickable by formatting it as HTML link $url = "<a href='$url' target='_blank'>$url</a>"; ?> <br> <small>Homepage : <?=$url;?></small>
<?php } ?> <br /> <img src="dots.gif" /> </td> </tr> </table> <br> <?php } // end while ?> <?php // close the database connection since // we no longer need it include 'library/closedb.php';
Det er ikke så svært, du må kun erklærer en funktion én gang. Hvis du erklærer en funktion flere gange vil php give en fejl.
En funktionen erklæres sådanne: function VisLidt($sText, $iCount) { //NOGET KODE }
Når du så ligger sådan en kode inde i en while-løkke, som du har gjort, så erklæres funktionen hver gang while-løkken kører.
I stedet for at ligge koden i while-løkken skal du ligge den i toppen af din dokument, eller i en separat fil. Du kan foreksempel ligge den lige efter <?php i toppen af dit dokument.
Fandt jo faktisk aldrig ud af hovedspørgsmålet... hvorfor den på selve nyhedssiden viser at der er 4 sider, når der kun er 2 nyheder i databasen og de fylder ikke mere end en enkelt side...
<?php // include the database configuration and // open connection to database include 'library/config.php'; include 'library/opendb.php';
?>
<?php
// ======================= // Show guestbook entries // =======================
// how many guestbook entries to show per page $rowsPerPage = 8;
// by default we show first page $pageNum = 1;
// if $_GET['page'] defined, use the value as page number if(isset($_GET['page'])) { $pageNum = $_GET['page']; }
// counting the offset ( where to start fetching the entries ) $offset = ($pageNum - 1) * $rowsPerPage;
// prepare the query string $query = "SELECT id, name, email, url, message, DATE_FORMAT(entry_date, '%d.%m.%Y') ". "FROM news ". "ORDER BY id DESC ". // using ORDER BY to show the most current entry first "LIMIT $offset, $rowsPerPage"; // LIMIT is the core of paging
// execute the query $result = mysql_query($query) or die('Error, query failed. ' . mysql_error());
// if the guestbook is empty show a message if(mysql_num_rows($result) == 0) { ?> <p><br> <br> News table is empty </p> <?php } else { // get all guestbook entries while($row = mysql_fetch_array($result)) { // list() is a convenient way of assign a list of variables // from an array values list($id, $name, $email, $url, $message, $date) = $row;
// change all HTML special characters, // to prevent some nasty code injection $name = htmlspecialchars($name); $message = htmlspecialchars($message);
// convert newline characters ( \n OR \r OR both ) to HTML break tag ( <br> ) $message = nl2br($message); ?> <table width="550" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="80" align="left"> <?=$name;?> </td> <td align="right"><small> <?=$date;?> </small></td> </tr> <tr> <td colspan="2"> <?=$message;?><br />
<?php // if the visitor input her homepage url show it if($url != '') { // make the url clickable by formatting it as HTML link
?> <br> <?php } ?> <br /> <img src="dots.gif" /></td> </tr> </table> <br> <?php } // end while
// below is the code needed to show page numbers
// count how many rows we have in database $query = "SELECT COUNT(id) AS numrows FROM guestbook"; $result = mysql_query($query) or die('Error, query failed. ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows'];
// how many pages we have when using paging? $maxPage = ceil($numrows/$rowsPerPage); $nextLink = '';
// show the link to more pages ONLY IF there are // more than one page if($maxPage > 1) { // this page's path $self = $_SERVER['PHP_SELF'];
// we save each link in this array $nextLink = array();
// create the link to browse from page 1 to page $maxPage for($page = 1; $page <= $maxPage; $page++) { $nextLink[] = "<a href=\"$self?side=news&page=$page\">$page</a>"; }
// join all the link using implode() $nextLink = "Go to page : " . implode(' » ', $nextLink); }
// close the database connection since // we no longer need it include 'library/closedb.php';
Hvis du nu tager og kigger lidt efter på de svar som er kommet, så vil du finde et svar fra undertegnede, som fortæller at du vælger den forkerte tabel at finde hvor mange rækker der er. Men lige for at gentage mig selv: Kunne det ikke tyde på at du læser fra den forkerte tabel ??
$query = "SELECT COUNT(id) AS numrows FROM guestbook"; <-----
måske det ikke er guestbook men news ???
Som det er nu finder du antal af rækker i din gæstebog og ikke antallet af rækker i din nyhedstabel.
Lidt mere til det: // below is the code needed to show page numbers
// count how many rows we have in database $query = "SELECT COUNT(id) AS numrows FROM guestbook"; <<<----- $result = mysql_query($query) or die('Error, query failed. ' . mysql_error()); $row = mysql_fetch_array($result, MYSQL_ASSOC); $numrows = $row['numrows'];
ret: $query = "SELECT COUNT(id) AS numrows FROM guestbook"; til: $query = "SELECT COUNT(id) AS numrows FROM news";
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.