Jump to content

Recommended Posts

Hello, I have just started in PHP to create a news system for the site I am developing. The idea is to display 3 of the latest news items on the home page, which I have done. But to save space, I have tried to create it so that only the titles are shown and when the user clicks on the title, the body of the story is shown. I have found the relevant code for this and put it into my page but the whole thing seams not to be working. All three of the title links open the top news story, but that is the smallest problem as the body of the news story is not being displayed, although the title is. I am also getting the following message at the bottom of the page:

 

PHP Notice: Undefined property: stdClass::$news_body in C:\Inetpub\wwwroot\index.php on line 140

 

Can anyone help please?

 

<head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><style type="text/css">
<!--
.style1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 36px;
}
.style2 {font-family: Arial, Helvetica, sans-serif; font-size: 24px; }
.style3 {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 18px;
}
a {
font-family: Arial, Helvetica, sans-serif;
}
-->
</style>

<style type="text/css">
<!--
.container {
font-family:verdana,arial,helvetica,sans-serif;
font-size:16px;
margin:4px 0;
}
.container img {
vertical-align:bottom;
}
.container span {
font-weight:bold;
cursor:pointer;
}
.off {
display:none;
}
.on {
display:block;
margin:10px 30px;
text-align:justify;
color:#003;
background-color:#eef;
}
-->
</style>

<script type="text/javascript">
<!--
window.onload=function() {
spn=document.getElementsByTagName('span');
for(c=0;c<spn.length;c++) {
spn[c].onclick=function() {
if(this.id!='') {
stuff(this.id.split('s')[1]);
}
}
}
}
function stuff(n) {

dvs=document.getElementsByTagName('div');
ims=document.getElementsByTagName('img');
info=document.getElementById('div'+n);
pic=document.getElementById('img'+n);

for(c=0;c<dvs.length;c++) {
if((dvs[c].className=='on')&&(dvs[c].id!='div'+n)) {
dvs[c].className='off';
}
}
for(c=0;c<ims.length;c++) {
ims[c].src='images/plus.GIF';
}
if(info.className=='on') {
info.className='off';
pic.src='images/plus.GIF';
}
else {
info.className='on';
pic.src='images/minus.GIF';
}
}

//-->
</script>

</head>
<body>
<p align="center"> </p>
<table width="80%" border="0" align="center">
  <tr>
    <td height="20"> </td>
  </tr>
  <tr>
    <td><p class="style2">Latest News:<br></p>

<?php
//includes
include('conf.php');

// Create database connection
$connection = mysql_connect($host, $user, $pass) or die ('Unable to connect');

// Select database
mysql_select_db($db) or die ('Unable to select database');

// Generate query and execute
$query = "SELECT news_id, news_title FROM news ORDER BY news_id DESC LIMIT 0,3";

$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// If records present
if (mysql_num_rows($result) > 0)
{
// Itterate through result set
// Print news items
while($row = mysql_fetch_object($result))
{
?>
<div class="container">
<span id="s0"><?php echo $row->news_title; ?></span>
<div id="div0" class="off">
<?php echo $row->news_body; ?>
</div>
<?php
}
}

// If no records present
// Display message
else
{
?>
<font size="-1">No records currently avaliable</font>
<?php
}

// Close database connection
mysql_close($connection);
?>

</td>
  </tr>
  <tr>
    <td> </td>
  </tr>
</table>
</body>
</html>

 

Thanks in advance,

 

- Chris -

Link to comment
https://forums.phpfreaks.com/topic/55926-solved-help-with-news-system/
Share on other sites

lol. It's amazing what you can miss even after spending an hour trawling through the code. Thank you very much for the help. However, I still have the problem of the three links opening just the top story, under the top story heading. I think that this may be more of a javascript problem but if anyone can help I would be very greatful.

 

P.S: I have uploaded the file to:  http://186.awardspace.com/index.php

Just took a quick look at the source on the page you uploaded and found:

 

<span id="s0">This is the latest post</span>
<div id="div0" class="off">
Hello world</div>
<div class="container">
<span id="s0">Title HERE</span>
<div id="div0" class="off">
this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post. this is the body of the post.</div>
<div class="container">
<span id="s0">fred</span>
<div id="div0" class="off">
bloggs</div>

 

All of the IDs are div0 or s0. Im assuming those numbers where supposed to be incremented. In which case, try:

 

<?php
$x = 0;
if (mysql_num_rows($result) > 0)
{
// Itterate through result set
// Print news items
while($row = mysql_fetch_object($result))
{
?>
<div class="container">
<span id="s<?php echo $x; ?>"><?php echo $row->news_title; ?></span>
<div id="div<?php echo $x; ?>" class="off">
<?php echo $row->news_body; ?>
</div>
<?php
             $x++;
}

}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.