Hi, I am using PHP and mysql to create a website.
I am fairly new to PHP, and thus am grateful to anyone who helps!
Firstly I am running a homepage, that displays additional content if a user has logged in. I set a loggedin session in the log in page, which I have tested and works fine in other scripts.
I am displaying news on the homepage (which is stored in a mysql database), and this works fine when the user accesses the homepage without having logged on.
It displays the date sent, the subject, and who sent it and eventually have a link to a single file to view everything about the message, as well as the message itself.
This works fine when viewed by someone who has not logged in, and displays what it should. However when someone views the page having logged in (the weird thing is that I copied and pasted the code, only changing one filename to avoid requiring two files that declared the same function, escape_data), it displays the following error messages:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Users\Harry\Documents\Web development\wamp\www\Website\html\home.php on line 346
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Users\Harry\Documents\Web development\wamp\www\Website\html\home.php on line 518
Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource in C:\Users\Harry\Documents\Web development\wamp\www\Website\html\home.php on line 544
I have been stuck on this for a long time now, as I usually prefer to solve problems myself if I can.
Here is my main code (I have not shown the code for the other files referred to, as I have used them for other things and now that they work): -
-
-
-
<?php
-
-
$page_title = 'Homepage';
-
$loggedin = $_SESSION['loggedin'];
-
if ($loggedin != NULL) //then run non logged in homepage
-
{
-
include ('./includes/header.html');
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnews.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message FROM users ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>
-
-
-
<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">//here is where I insert the code for a link to allow someone to view the message stored in the database, it
-
// could be stored in the url I suppose, but then I will need to formulate code which would allow me to read it and create a page displaying from whom
-
// the message was from, the subject, the time sent, and the full message itself
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
include ('./includes/footer.html');
-
} //end of if not logged in
-
-
-
-
-
-
-
else { //start of else logged in
-
-
-
$page_title = 'Homepage';
-
-
-
require_once ('.../mysql_connectusers.php');
-
-
-
include ('./includes/headerloggedin.html');
-
$u = $_SESSION['username'];
-
-
-
-
$query = "SELECT ID,
-
email FROM users WHERE
-
(username='$u')";
-
-
$result = mysql_query ($query) or
-
trigger_error("Query: $query\n<br />
-
MySQL Error: " . mysql_error());
-
-
-
$row = mysql_fetch_array($result, MYSQL_NUM);
-
-
mysql_free_result($result);
-
-
-
mysql_close();
-
-
-
-
if ($email == NULL) {
-
echo '<p> You need to set your email address
-
<a href="./set_emailloggedin.php">here</a> in order that if you forget your password at some point in the future it can be sent to you.</p>';
-
}
-
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnewswithoutfunction.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message FROM users ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>
-
-
-
<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">
-
</td>//here is where I insert the code for a link to allow someone to view the message stored in the database, it
-
// could be stored in the url I suppose, but then I will need to formulate code which would allow me to read it and create a page displaying from whom
-
// the message was from, the subject, the time sent, and the full message itself
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
-
-
// Include the HTML footer file.
-
-
include ('./includes/footer.html');
-
-
-
} //end of else logged in
-
?>
-
Thanks to anyone who helps,
Harry
3 1765
I have updated the code slightly (adding in a line for the table, and adding to the queries) which shouldn't affect the rest of the script, however now I get the error messages displayed before just when logged in (see previous post)!
Here is the new code -
-
-
<?php
-
-
$page_title = 'Homepage';
-
$loggedin = $_SESSION['loggedin'];
-
if ($loggedin != NULL) //then run non logged in homepage
-
{
-
include ('./includes/header.html');
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnews.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message, newsid FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>
-
-
-
<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">//here is where I insert the code for a link to allow someone to view the message stored in the database, it
-
// could be stored in the url I suppose, but then I will need to formulate code which would allow me to read it and create a page displaying from whom
-
// the message was from, the subject, the time sent, and the full message itself
-
-
<a href="viewmessage.php?id=' . $row['newsid'] . '">View news item</a>
-
-
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
include ('./includes/footer.html');
-
} //end of if not logged in
-
-
-
-
-
-
-
else { //start of else logged in
-
-
-
$page_title = 'Homepage';
-
-
-
require_once ('.../mysql_connectusers.php');
-
-
-
include ('./includes/headerloggedin.html');
-
$u = $_SESSION['username'];
-
-
-
-
$query = "SELECT ID,
-
email FROM users WHERE
-
(username='$u')";
-
-
$result = mysql_query ($query) or
-
trigger_error("Query: $query\n<br />
-
MySQL Error: " . mysql_error());
-
-
-
$row = mysql_fetch_array($result, MYSQL_NUM);
-
-
mysql_free_result($result);
-
-
-
mysql_close();
-
-
-
-
if ($email == NULL) {
-
echo '<p> You need to set your email address
-
<a href="./set_emailloggedin.php">here</a> in order that if you forget your password at some point in the future it can be sent to you.</p>';
-
}
-
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnewswithoutfunction.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message, newsid FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>
-
-
-
<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">
-
</td>//here is where I insert the code for a link to allow someone to view the message stored in the database, it
-
// could be stored in the url I suppose, but then I will need to formulate code which would allow me to read it and create a page displaying from whom
-
// the message was from, the subject, the time sent, and the full message itself
-
-
-
<a href="viewmessage.php?id=' . $row['newsid'] . '">View news item</a>
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
-
-
// Include the HTML footer file.
-
-
include ('./includes/footer.html');
-
-
-
} //end of else logged in
-
?>
-
Thanks to anyone who helps,
Harry
I have further edited the code, and it is back to the state (error wise) of the first script.
Here it is -
-
-
<?php
-
-
$page_title = 'Homepage';
-
$loggedin = $_SESSION['loggedin'];
-
if ($_SESSION['ID'] >= 1) //then run non logged in homepage
-
{
-
include ('./includes/header.html');
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnews.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message, newsid FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>
-
-
-
<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">' . $row['message'] . '
-
-
-
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
include ('./includes/footer.html');
-
} //end of if not logged in
-
-
-
-
-
-
-
else { //start of else logged in
-
-
-
$page_title = 'Homepage';
-
-
-
require_once ('.../mysql_connectusers.php');
-
-
-
include ('./includes/headerloggedin.html');
-
$u = $_SESSION['username'];
-
-
-
-
$query = "SELECT ID,
-
email FROM users WHERE
-
(username='$u')";
-
-
$result = mysql_query ($query) or
-
trigger_error("Query: $query\n<br />
-
MySQL Error: " . mysql_error());
-
-
-
$row = mysql_fetch_array($result, MYSQL_NUM);
-
-
mysql_free_result($result);
-
-
-
mysql_close();
-
-
-
-
if ($email == NULL) {
-
echo '<p> You need to set your email address
-
<a href="./set_emailloggedin.php">here</a> in order that if you forget your password at some point in the future it can be sent to you.</p>';
-
}
-
-
?>
-
-
<p><h3>News</h3></p>
-
<?php
-
-
-
require_once ('../mysql_connectnewswithoutfunction.php');
-
// Number of records to show per page:
-
-
$display = 7;
-
-
-
// Determine how many pages there are.
-
-
if (isset($_GET['np']))
-
{ // Already been determined.
-
-
-
$num_pages = $_GET['np'];
-
}
-
-
else { // Need to determine.
-
-
-
// Count the number of records
-
-
$query = "SELECT COUNT(*) FROM news ORDER BY datesent ASC";
-
-
$result = @mysql_query ($query);
-
-
$row = mysql_fetch_array ($result, MYSQL_NUM);
-
-
$num_records = $row[0];
-
-
-
// Calculate the number of pages.
-
-
if ($num_records > $display)
-
{ // More than 1 page.
-
-
$num_pages = ceil ($num_records/$display);}
-
-
-
else {
-
-
$num_pages = 1;
-
}
-
-
-
-
} // End of np IF.
-
-
-
// Determine where in the database to start returning results.
-
-
-
if (isset($_GET['s']))
-
{
-
$start = $_GET['s'];
-
}
-
-
else
-
{
-
$start = 0;
-
}
-
-
-
-
// Default column links.
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
-
// Determine the sorting order.
-
-
-
if (isset($_GET['sort']))
-
{
-
-
// Use existing sorting order.
-
-
-
switch ($_GET['sort']) {
-
-
-
case 'lna':
-
-
$order_by = 'datesent ASC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lnd";
-
-
break;
-
-
-
case 'lnd':
-
-
$order_by = 'datesent DESC';
-
-
$link1 = "{$_SERVER['PHP_SELF']}?sort=lna";
-
-
break;
-
-
-
case 'fna':
-
-
$order_by = 'fromwhom ASC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fnd";
-
-
break;
-
-
case 'fnd':
-
-
$order_by = 'fromwhom DESC';
-
-
$link2 = "{$_SERVER['PHP_SELF']}?sort=fna";
-
-
break;
-
-
-
case 'dra':
-
-
$order_by = 'subject ASC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=drd";
-
-
break;
-
-
-
case 'drd':
-
-
$order_by = 'subject DESC';
-
-
$link3 = "{$_SERVER['PHP_SELF']}?sort=dra";
-
-
break;
-
-
default:
-
-
$order_by = 'datesent DESC';
-
-
break;
-
}
-
-
-
-
// $sort will be appended to the pagination links.
-
-
$sort = $_GET['sort'];
-
-
}
-
else { // Use the default sorting order.
-
-
$order_by = 'datesent DESC';
-
-
$sort = 'drd';
-
}
-
-
-
-
-
-
-
-
-
// Make the query.
-
-
$query = "SELECT datesent, fromwhom, subject AS dr, message, newsid FROM news ORDER BY $order_by LIMIT $start, $display";
-
$result = @mysql_query ($query);
-
// Run the query.
-
-
-
-
//Table header.
-
-
echo '<table align="center" cellspacing="0" cellpadding="5">
-
<tr>';
-
-
echo'';
-
-
-
echo '<td align="left"><b><a href="' . $link1 . '">Date sent</a></b></td>
-
<td align="left"><b><a href="' . $link2 . '">From whom</a></b></td>
-
<td align="left"><b><a href="' . $link3 . '">Subject</a></b></td>
-
<td align="left"><b>Message</b></td>
-
-
</tr>
-
';
-
-
-
-
// Fetch and print all the records.
-
-
$bg = '#eeeeee';
-
// Set the background color.
-
-
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
-
-
$bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee');
-
// Switch the background color.
-
-
echo '<tr bgcolor="' .
-
$bg . '">
-
-
-
-
-
-
-
-
-
-
-
<td align="left">' . $row['datesent'] . ' </td>
-
<td align="left">' . $row['fromwhom'] . '</td>
-
<td align="left">' . $row['dr'] . '</td>
-
<td align="left">
-
<td align="left">' . $row['message'] . '
-
-
-
-
-
</td>
-
</tr>
-
';
-
}
-
-
echo '</table>';
-
-
mysql_free_result ($result); // Free up the resources.
-
-
mysql_close(); // Close the database connection.
-
-
// Make the links to other pages, if necessary.
-
if ($num_pages > 1) {
-
-
echo '<br /><p>';
-
// Determine what page the script is on.
-
$current_page = ($start/$display) + 1;
-
-
// If it's not the first page, make a Previous button.
-
if ($current_page != 1) {
-
echo '<a href="home.php?s=' . ($start - $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Previous</a> ';
-
}
-
-
// Make all the numbered pages.
-
for ($i = 1; $i <= $num_pages; $i++) {
-
if ($i != $current_page) {
-
echo '<a href="home.php?s=' . (($display * ($i - 1))) . '&np=' . $num_pages . '&sort=' . $sort .'">' . $i . '</a> ';
-
} else {
-
echo $i . ' ';
-
}
-
}
-
-
// If it's not the last page, make a Next button.
-
if ($current_page != $num_pages) {
-
echo '<a href="home.php?s=' . ($start + $display) . '&np=' . $num_pages . '&sort=' . $sort .'">Next</a>';
-
}
-
-
echo '</p>';
-
-
} // End of links section.
-
-
-
// Include the HTML footer file.
-
-
include ('./includes/footer.html');
-
-
-
} //end of else logged in
-
?>
-
mmmm strange post.
I've seen this happen a few times.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
2 posts
views
Thread by SAN CAZIANO |
last post: by
|
1 post
views
Thread by Avi |
last post: by
|
4 posts
views
Thread by Danny |
last post: by
|
7 posts
views
Thread by tuchka |
last post: by
|
5 posts
views
Thread by Janiv Ratson |
last post: by
|
1 post
views
Thread by John Dalberg |
last post: by
|
17 posts
views
Thread by Gladiator |
last post: by
|
6 posts
views
Thread by B. |
last post: by
|
2 posts
views
Thread by Ming |
last post: by
| | | | | | | | | | | |